bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.51k stars 1.58k forks source link

How can I detect focal point coordinate of an image? #407

Open probal opened 8 years ago

probal commented 8 years ago

I am creating a service, that takes imageUrl as input and return focal point coordinate as output. How can I do that using javacv? Any help/suggestion is much appreciated.

saudet commented 8 years ago

BTW, this sounds like a question for Stack Overflow or something... Providing a bit more details about the kinds of results you would like to achieve would help as well.

probal commented 8 years ago

Thanks @saudet . Sorry for the inconvenience, let me explain it a bit more.

I can detect face(s) from an image using the code below:

public static void analyzeImage(File inputFile) throws OperationNotSupportedException {

        final File faceCascadeFile = new File("haarcascade_frontalface_alt.xml");

        try {
            final CvHaarClassifierCascade faceCascade = loadFaceCascade(faceCascadeFile);
            final IplImage image = loadImage(inputFile);
            final IplImage imageBW = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
            cvCvtColor(image, imageBW, CV_BGR2GRAY);
            final int[] bbox = new int[4];
            detectFaceInImage(image, imageBW, faceCascade, bbox);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 

public static void detectFaceInImage(final IplImage orig,final IplImage input,final CvHaarClassifierCascade cascade,final int[] bbox) throws Exception {
        CvMemStorage storage = cvCreateMemStorage(0);
        cvClearMemStorage(storage);
        try {
            double search_scale_factor = 1.1;
            int flags = CV_HAAR_DO_CANNY_PRUNING;
            CvSize minFeatureSize = cvSize(50, 50);
            CvSeq rects = cvHaarDetectObjects(input, cascade, storage, search_scale_factor, 2, flags, minFeatureSize, cvSize(0, 0));
            int nFaces = rects.total();
            if (nFaces == 0) {
                throw new Exception("No faces detected");
            }

            for (int iface = 0; iface < nFaces; ++iface) {
                BytePointer elem = cvGetSeqElem(rects, iface);
                CvRect rect = new CvRect(elem);

                bbox[0] = rect.x();
                bbox[1] = rect.y();
                bbox[2] = rect.x() + rect.width();
                bbox[3] = rect.y() + rect.height();
                System.out.println("("+bbox[0]+","+bbox[1]+"),("+bbox[2]+","+bbox[3]+")");                
            }

        } finally {
            cvReleaseMemStorage(storage);
        }
    }

It gives me coordinate of the face(s). Since I need this service for intelligent cropping. In this regard, I also need the approximate coordinate of main focal point of the image as well. How can I detect focal point of an image, getting no clue.

probal commented 8 years ago

@saudet Could you please help me in this regard?