gpt4 book ai didi

java - 使用 openimaj 进行 Facecrop

转载 作者:行者123 更新时间:2023-12-02 03:05:58 27 4
gpt4 key购买 nike

裁剪 MBFImage

我使用出色的 openimaj 库 1.3.8 检测脸部,但我很难从原始图像中裁剪脸部并将裁剪后的脸部保存到某个文件中。裁剪后的面必须为 800x600 像素,因此我还需要进行一些缩放。

这一定是一个简单的任务,但 API 很大,我找不到适合这个用例的教程

也许你告诉我在哪里可以找到信息?

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.image.colour.RGBColour;
import org.openimaj.image.colour.Transforms;
import org.openimaj.image.processing.face.detection.DetectedFace;
import org.openimaj.image.processing.face.detection.FaceDetector;
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
import org.openimaj.math.geometry.shape.Rectangle;


public class App {
public static void main(String[] args) throws Exception, IOException {


final MBFImage image = ImageUtilities.readMBF(new File("d:\\java\\face\\bin.jpeg"));

FaceDetector<DetectedFace, FImage> fd = new HaarCascadeDetector(200);
List<DetectedFace> faces = fd.detectFaces(Transforms.calculateIntensity(image));
System.out.println("# Found faces, one per line.");
System.out.println("# <x>, <y>, <width>, <height>");

for (Iterator<DetectedFace> iterator = faces.iterator(); iterator.hasNext();) {
DetectedFace face = iterator.next();
Rectangle bounds = face.getBounds();

image.drawShape(face.getBounds(), RGBColour.RED);

// System.out.println(bounds.x + ";" + bounds.y + ";" + bounds.width + ";" +
// bounds.height);

}

DisplayUtilities.display(image);

}
}

最佳答案

已解决

        DetectedFace face = iterator.next();
Rectangle bounds = face.getBounds();

Point2d center = bounds.calculateCentroid();

// image.drawShape(bounds, RGBColour.RED);

// auf 800 x 600 bringen

float b = (bounds.width * 800) / 600;

MBFImage crop = image.extractCenter(
(int) center.getX() ,
(int) center.getY() ,
(int) bounds.width ,
(int) b ) ;


ResizeProcessor resize = new ResizeProcessor(800,600);
MBFImage small = crop.processInplace(resize);

DisplayUtilities.display(small);

关于java - 使用 openimaj 进行 Facecrop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com