Reputation: 109
Crop an MBFImage
I detect a face using the superb openimaj library 1.3.8 but i struggle crop the face from the original image and save the cropped face to some file. The cropped faces must be in 800x600 px so i need some scaling as well.
It must be a simple task but the api is huge and i dont find a tutorial that fits this usecase
Maybe you show me where i can find information?
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);
}
}
Upvotes: 0
Views: 309
Reputation: 109
solved
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);
Upvotes: 0