Package detection.javacv

Source Code of detection.javacv.Detect

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package detection.javacv;

/**
*
* @author duo
*/

import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;

import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;
import javax.swing.ImageIcon;

public class Detect {

    protected int detectionResult;

    /**
     * Get the value of detectionResult
     *
     * @return the value of detectionResult
     */
    public int getDetectionResult() {
        return detectionResult;
    }

    /**
     * Set the value of detectionResult
     *
     * @param detectionResult new value of detectionResult
     */
    public void setDetectionResult(int detectionResult) {
        this.detectionResult = detectionResult;
    }

    public ImageIcon detectionExecute(String imgFileName, String cascadeFileName) {
        System.out.println(cascadeFileName);
        System.out.println(imgFileName);
        IplImage originalImage = cvLoadImage(imgFileName, 1);
        // We need a grayscale image in order to do the recognition, so we 
        // create a new image of the same size as the original one.
        IplImage grayImage = null;
        try {
            grayImage = IplImage.create(originalImage.width(),
                    originalImage.height(), IPL_DEPTH_8U, 1);
        } catch (NullPointerException e) {
            System.out.println("Error: " + e);
            System.out.println("Check image file!");
            System.exit(1);
        }
        // We convert the original image to grayscale. 
        cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);

        CvMemStorage storage = CvMemStorage.create();

        // We instantiate a classifier cascade to be used for detection, using 
        // the cascade definition. 
        CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
                cvLoad(cascadeFileName));

        // We detectionExecute the faces. 
        CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1,
                0);

        // We iterate over the discovered faces and draw yellow rectangles 
        // around them. 
        for (int i = 0; i < faces.total(); i++) {
            CvRect r = new CvRect(cvGetSeqElem(faces, i));
            cvRectangle(originalImage, cvPoint(r.x(), r.y()),
                    cvPoint(r.x() + r.width(), r.y() + r.height()),
                    CvScalar.YELLOW, 1, CV_AA, 0);

        }

        ImageIcon image = new ImageIcon(originalImage.getBufferedImage().getScaledInstance(-1, -1, 0));
        // ImageIcon.getImage() will make sure all bytes are loaded
        image.getImage();
        setDetectionResult(faces.total());
        return image;
    }

}
TOP

Related Classes of detection.javacv.Detect

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.