Ask Your Question
0

OpenCV 2.4.6 Java VideoCapture not working

asked 2013-11-09 13:11:46 -0500

ibby gravatar image

updated 2013-11-10 07:32:07 -0500

Hi,

I am fairly new to OpenCV. I am using version 2.4.6 as a Java library in Eclipse projects. I have been following the Java OpenCV tutorial which loads up an image and runs face detection on it. Instead of using an image from a file I wanted to capture an image from my webcam. Strangely, this only works the very first time I try it. Afterwards I get the warning/error:

libpng warning: Image width is zero in IHDR

libpng warning: Image height is zero in IHDR

libpng error: Invalid IHDR data

The .read() method on the VideoCapture object only returns true the first time. I am calling the .release() method, so I really don't know what's wrong. I have to physically restart my laptop for it to work again (maybe restarting Eclipse is sufficient, I haven't tried). The program does terminate just fine. Here is the code:

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.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;

class DetectFaceDemo {
    public void run() {
        System.out.println("Running DetectFaceDemo");
        CascadeClassifier faceDetector = new CascadeClassifier(getResource("/resources/lbpcascade_frontalface.xml"));
        Mat image = new Mat();
        VideoCapture camera = new VideoCapture(0);
        System.out.println(camera.isOpened()); // Always true.
        System.out.println(camera.read(image)); // Only true for first execution.
        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 = "faceDetection.png";
        System.out.println(String.format("Writing %s", filename));
        Highgui.imwrite(filename, image);
        camera.release();
    }

    private String getResource(String path) {
        String retVal = getClass().getResource(path).getPath();
        if (retVal.startsWith("/")) retVal = retVal.replaceFirst("/", "");
        return retVal;
    }
}

public class OpenCVTest {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new DetectFaceDemo().run();
    }
}

The .getResource() method is just there because the usual .getResource() method has a little bug where a "/" is prefixed to the returned (absolute) path (not okay under Windows).

Thanks a lot for any help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-11-18 03:49:47 -0500

I recommend you to update OpenCV up to 2.4.7. There were some cv::VideoCapture issues on Linux and Mac in 2.4.6, 2.4.6.1 releases.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-11-09 13:11:46 -0500

Seen: 1,300 times

Last updated: Nov 18 '13