如何在opencv中裁剪检测到的人脸图像java
how to crop the detected face image in opencv java
我在 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中将其保存为新的jpg图像java。
裁剪代码为
Rect rectCrop = new Rect(x, y, width, height);
Mat image_roi = image(rectCrop);
image_roi.copyTo(cropimage);
imwrite("cropimage.jpg",image_roi);
错误是
FaceDetector 类型未定义方法 image(Rect)
FaceDetector
类型未定义方法 imwrite(String, Mat)
请任何人帮助我解决这个问题。
谢谢
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);
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);
}}
如何裁剪多张脸? 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);
}}
试试这个。这个很好用。
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 );
}
}
int detectedFaces = faceDetections.toArray().length;
Rect rect_Crop=null;
// Draw a bounding box around each face.
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));
rect_Crop = new Rect(rect.x, rect.y, rect.width, rect.height);
Mat image_roi = new Mat(image,rect_Crop);
StringBuilder sb = new StringBuilder("outputimage");
sb.append(detectedFaces).append(".jpg");
Imgcodecs.imwrite(sb.toString(),image_roi);
detectedFaces--;
}
这适用于询问如何使用 openCV 裁剪多张面孔的人 java。
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);
}
这看起来是最聪明的方法。
我在 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中将其保存为新的jpg图像java。
裁剪代码为
Rect rectCrop = new Rect(x, y, width, height);
Mat image_roi = image(rectCrop);
image_roi.copyTo(cropimage);
imwrite("cropimage.jpg",image_roi);
错误是
FaceDetector 类型未定义方法 image(Rect) FaceDetector
类型未定义方法 imwrite(String, Mat)请任何人帮助我解决这个问题。
谢谢
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);
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);
}}
如何裁剪多张脸? 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);
}}
试试这个。这个很好用。
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 );
}
}
int detectedFaces = faceDetections.toArray().length;
Rect rect_Crop=null;
// Draw a bounding box around each face.
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));
rect_Crop = new Rect(rect.x, rect.y, rect.width, rect.height);
Mat image_roi = new Mat(image,rect_Crop);
StringBuilder sb = new StringBuilder("outputimage");
sb.append(detectedFaces).append(".jpg");
Imgcodecs.imwrite(sb.toString(),image_roi);
detectedFaces--;
}
这适用于询问如何使用 openCV 裁剪多张面孔的人 java。
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);
}
这看起来是最聪明的方法。