public IplImage correctGamma(IplImage img) { // get image intensity // img.applyGamma(d); IplImage gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); cvCvtColor(img, gray, CV_RGB2GRAY); BufferedImage buffImg = img.getBufferedImage(); BufferedImage buffGray = gray.getBufferedImage(); double grayArr[] = new double[gray.width() * gray.height()]; int counter = 0; for (int i = 0; i < gray.width(); i++) { for (int j = 0; j < gray.height(); j++) { grayArr[counter] = buffGray.getRGB(i, j); counter++; } } double imgSd = new StandardDeviation().evaluate(grayArr); double imgMean = new Mean().evaluate(grayArr); double y = 0; if (imgMean > 0.5) y = 1 + (Math.abs(0.5 - imgMean) / imgSd); else y = 1 / (1 + (Math.abs(0.5 - imgMean) / imgSd)); img.applyGamma(y); return img; }
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); } }