在Javacv中,cvCreateFileCapture无法从内存中读取视频

时间:2017-01-06 15:13:05

标签: opencv javacv

我正在尝试在javacv中导入视频,但cvCreateFileCapture("文件名")类正在返回null值,我也尝试使用OpenCVFrameGrabber("文件名"),它也是不正常。

这是我的代码:

public class SmokeDetectionInStoredVideo {
    private static final int IMG_SCALE = 2;
    private static final int width = 640;  
    private static final int height = 480;

public static void main(String args[]){
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    CvMemStorage storage = CvMemStorage.create();
    IplImage img1=null,imghsv=null,imgbin=null;
    CvSeq contour1;
    CvSeq contour2;
    double areaMax = 00, areaC = 0.0;
    imghsv = IplImage.create(width/IMG_SCALE, height/IMG_SCALE, 8, 3);
    imgbin = IplImage.create(width/IMG_SCALE, height/IMG_SCALE, 8, 1);
    try{
        CvCapture capture = cvCreateFileCapture("Red.mp4");//cvCreateCameraCapture(0);
        if(capture.isNull()){
            System.out.println("Error reading file");
        }
        IplImage grabbedImage = cvQueryFrame(capture);
        OpenCVFrameConverter.ToIplImage converter=new OpenCVFrameConverter.ToIplImage();
        CanvasFrame canvasFrame = new CanvasFrame("Actual Video");
        CanvasFrame canvasBinFrame = new CanvasFrame("Contour Video");
        canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());
        canvasBinFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());
        imghsv = IplImage.create(grabbedImage.width(), grabbedImage.height(), 8, 3);
        imgbin = IplImage.create(grabbedImage.width(), grabbedImage.height(), 8, 1);

        while (canvasFrame.isVisible() && (img1 = cvQueryFrame(capture)) != null) {
            cvCvtColor(img1, imghsv, CV_RGB2HSV);
            //canvasFrame.showImage(converter.convert(imghsv));

            cvInRangeS(img1, cvScalar(220, 220, 220, 0), cvScalar(235, 235, 235, 0), imgbin);
            contour1 = new CvSeq();
            cvFindContours(imgbin, storage, contour1, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_LINK_RUNS);
            contour2 = contour1;

            while (contour1 != null && !contour1.isNull()) {
                areaC = cvContourArea(contour1, CV_WHOLE_SEQ, 1);
                if(areaC>areaMax){
                    areaMax = areaC;
                }
                contour1 = contour1.h_next();
            }

            while (contour2 != null && !contour2.isNull()){
                areaC = cvContourArea(contour2, CV_WHOLE_SEQ, 1);
                if(areaC>areaMax){
                    cvDrawContours(imgbin, contour1, CV_RGB(0, 0, 0), CV_RGB(0, 0, 0), 0, CV_FILLED, 8);
                }
                contour2 = contour2.h_next();
            }
            canvasBinFrame.showImage(converter.convert(imgbin));
            canvasFrame.showImage(converter.convert(img1));
        }
        canvasFrame.dispose();
        cvReleaseMemStorage(storage);
        cvReleaseImage(img1);
        cvReleaseImage(imgbin);
        cvReleaseImage(imghsv);
    }
    catch (Exception e) {
        System.out.println("Error while processing video");
        // TODO: handle exception
    }
}

}

是否有其他方法可以在javacv中导入视频。

1 个答案:

答案 0 :(得分:0)

您确定在类路径中有此文件吗?您可以尝试以下方法来检查文件是否存在并显示输出吗?

File file = new File("Red.mp4");
System.out.println(file.exists());