public static void detect(IplImage src) { CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(XML_FILE)); CvMemStorage storage = CvMemStorage.create(); CvSeq sign = cvHaarDetectObjects(src, cascade, storage, 1.5, 3, CV_HAAR_DO_CANNY_PRUNING); cvClearMemStorage(storage); int total_Faces = sign.total(); for (int i = 0; i < total_Faces; i++) { CvRect r = new CvRect(cvGetSeqElem(sign, i)); cvRectangle( src, cvPoint(r.x(), r.y()), cvPoint(r.width() + r.x(), r.height() + r.y()), CvScalar.RED, 2, CV_AA, 0); } cvShowImage("Result", src); cvSaveImage("D:\\asd\\a.jpg", src); cvWaitKey(0); }
public void generatePGMFromPic(String srcPath, String file, String destPath) throws Exception { String srcFilePath = srcPath + "/" + file; System.out.println("Loading image from " + srcFilePath); IplImage origImg = cvLoadImage(srcFilePath); // convert to grayscale IplImage grayImg = IplImage.create(origImg.width(), origImg.height(), IPL_DEPTH_8U, 1); cvCvtColor(origImg, grayImg, CV_BGR2GRAY); // scale the grayscale (to speed up face detection) IplImage smallImg = IplImage.create(grayImg.width() / SCALE, grayImg.height() / SCALE, IPL_DEPTH_8U, 1); cvResize(grayImg, smallImg, CV_INTER_LINEAR); // equalize the small grayscale IplImage equImg = IplImage.create(smallImg.width(), smallImg.height(), IPL_DEPTH_8U, 1); cvEqualizeHist(smallImg, equImg); CvMemStorage storage = CvMemStorage.create(); CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE)); System.out.println("Detecting faces..."); CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING); cvClearMemStorage(storage); int total = faces.total(); System.out.println("Found " + total + " face(s)"); for (int i = 0; i < total; i++) { CvRect r = new CvRect(cvGetSeqElem(faces, i)); cvSetImageROI( origImg, cvRect(r.x() * SCALE, r.y() * SCALE, r.width() * SCALE, r.height() * SCALE)); IplImage origface = cvCreateImage(cvSize(r.width() * SCALE, r.height() * SCALE), 8, 3); IplImage smallface = cvCreateImage(cvSize(120, 120), 8, 3); cvCopy(origImg, origface); cvResize(origface, smallface, CV_INTER_LINEAR); cvSaveImage(destPath + "/" + file + i + ".pgm", smallface); cvResetImageROI(origImg); } }