Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV 2.4.6 Java VideoCapture not working

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!

OpenCV 2.4.6 Java VideoCapture not working

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!