我在java中使用opencv 2.4.10检测到了图像中的面部
我把我的面部检测代码。
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetector {
public static void main(String[] args)throws Exception {
int x,y,height,width;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
//CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath());
CascadeClassifier faceDetector=new CascadeClassifier();
faceDetector.load("C:/opencv-2.4.10/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
//System.out.println(""+faceDetector);
// Mat image = Highgui .imread(FaceDetector.class.getResource("D:/shekar.jpg").getPath());
Mat image = Highgui .imread("D:/Eclipse - New Juno/New Juno Projects/detectface/man1.jpg");
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
}
String filename = "china.jpg";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
现在我想裁剪检测到的面部并将其保存为opencv java中的新jpg图像。
裁剪代码
Rect rectCrop = new Rect(x, y, width, height);
Mat image_roi = image(rectCrop);
image_roi.copyTo(cropimage);
imwrite("cropimage.jpg",image_roi);
错误是
对于FaceDetector类型,方法图像(Rect)未定义 对于FaceDetector类型
,方法imwrite(String,Mat)是未定义的请有人帮我解决这个问题。
由于
答案 0 :(得分:11)
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import static org.opencv.highgui.Highgui.imwrite;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetector {
private static Mat cropImage;
public static void main(String[] args)throws Exception {
int x = 0,y = 0,height = 0,width = 0;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
//CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xm l").getPath());
CascadeClassifier faceDetector = new CascadeClassifier(Snapshot.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
Mat image = Highgui.imread("C:\\image.jpg");
faceDetector.detectMultiScale(image, face_Detections);
System.out.println(String.format("Detected %s faces", face_Detections.toArray().length));
Rect rect_Crop=null;
for (Rect rect : face_Detections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}
Mat image_roi = new Mat(image,rectCrop);
Highgui.imwrite("C:\\cropimage_912.jpg",image_roi);
}}
答案 1 :(得分:2)
OpenCV具有您感兴趣的感兴趣区域功能。如果您使用的是cv :: Mat,那么您可以使用以下内容。
// Take your Final Detected Image
image;
// These values need to be your determined face rect values
cv::Rect myROI(x, y,width, height);
// Crop the full image to that image contained by the rectangle myROI
// Note that this doesn't copy the data
cv::Mat croppedImage = image(myROI);
答案 2 :(得分:1)
试试这个。这个工作得很好。
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetection
{
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier();
faceDetector.load("D:\\OpenCv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
System.out.println ( "Working" );
// Input image
Mat image = Imgcodecs.imread("D:\\input.jpg");
// Detecting faces
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
// Creating a rectangular box showing faces detected
Rect rectCrop=null;
for (Rect rect : faceDetections.toArray())
{
Imgproc.rectangle(image, new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}
// Saving the output image
String filename = "Ouput.jpg";
Imgcodecs.imwrite("D:\\"+filename, image);
Mat markedImage = new Mat(image,rectCrop);
Imgcodecs.imwrite("D:\\cropimage.jpg",markedImage );
}
}
答案 3 :(得分:0)
如何裁剪多个面孔? dhvani。
for (Rect rect : face_Detections.toArray()) {
Core.rectangle(
image,
new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
}
Mat image_roi = new Mat(image,rectCrop);
Highgui.imwrite("C:\\cropimage_912.jpg",image_roi);
}}
答案 4 :(得分:0)
CarouselPage
这是给那些问如何使用openCV Java裁剪多个面孔的人的。
答案 5 :(得分:0)
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++) {
Rect rect = facesArray[i];
Imgproc.rectangle(frame, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2);
Mat submat = frame.submat(facesArray[i]);
imwrite("./face" + i + ".jpg", submat);
}
这似乎是最聪明的方法。