使用Java中的OpenCV 3.1捕获rtsp视频流

3

我正在尝试使用OpenCV在Java中创建一个应用程序,以从Web服务中抓取视频流,该服务是一个具有多个摄像头和录像设备的相机系统。

我已经找到了地址“rtsp://login:pass@IP address:Port/cam/realmonitor?channel=1&subtype=0”,用于访问通道1上的摄像机。

为了打开摄像机流,我使用了以下代码(目前它捕获本地USB摄像机):

VideoCapture cap; Mat2Image mat2Img = new Mat2Image();

public VideoGrabber(){
    cap = new VideoCapture(0);

    try {
        System.out.println("Sleeping..");
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Camera on..");
    cap.open("0");
    if(!cap.isOpened()){
        System.out.println("Camera Error");
    }
    else{
        System.out.println("Camera OK?");
    }
}

在捕获视频流后,我将其放入了一个JFrame中。

我认为我应该将视频流服务地址放在cap.open(...)中,但使用rtsp://login:pass@http://192.168.1.14:8006/cam/realmonitor?channel=1&subtype=0会出现“Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:宽度(0)和高度(0)必须> 0”的错误。

请帮忙解决,

编辑 我已经发现rtsp://login:pass@http://192.168.1.14:554/cam/realmonitor?channel=1&subtype=0在vlc中可以工作,但在opencv中仍然没有成功。

编辑#2 好的,在尝试了vlcl、gstreamer和大多数流行的解决方案之后,它突然开始工作了。我不知道这是否是错误的rtsp地址。代码:

static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //load the library of opencv
    }

    VideoCapture cap;
    Mat2Image mat2Img = new Mat2Image();
    Mat matFilter = new Mat();

    public VideoGrabber(){
        cap = new VideoCapture();

        try {
            System.out.println("Sleeping..");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Camera on..");
        cap.open("rtsp://login:pass@192.168.1.14:554/cam/realmonitor?channel=1&subtype=0");
        if(!cap.isOpened()){
            System.out.println("Camera Error");
        }
        else{
            System.out.println("Camera OK?");
        }


    }

嘿,你能分享一下你是如何让OpenCV打开RTSP URL的吗? 我已经按照这个指南(http://blog.jiashen.me/2014/12/23/build-opencv-3-on-mac-os-x-with-python-3-and-ffmpeg-support/)编译了带有FFmpeg的OpenCV 3.2版本,但仍然无法打开RTSP URL。 - Fouad
1个回答

5

回答我的问题,为Fouad发布工作代码: 我猜答案是加载ffmpeg dll。

//all the imports
public class App {
static {
    String path = null;
    try {
        //I have copied dlls from opencv folder to my project folder
        path = "E:\\JAVA Projects\\OpenCv\\RTSP Example\\libraries";
        System.load(path+"\\opencv_java310.dll");
        System.load(path+"\\opencv_ffmpeg310_64.dll");
    } catch (UnsatisfiedLinkError e) {
        System.out.println("Error loading libs");
    }
}

public static void main(String[] args) {

    App app = new App();
    //Address can be different. Check your cameras manual. :554 a standard RTSP port for cameras but it can be different
    String addressString = "rtsp://login:password@192.168.1.14:554/cam/realmonitor?channel=11&subtype=0";
    Mat mat = new Mat();
    VideoCapture capturedVideo = new VideoCapture();

    boolean isOpened = capturedVideo.open(addressString); 
    app.openRTSP(isOpened, capturedVideo, mat);

}

public void openRTSP(boolean isOpened, VideoCapture capturedVideo, Mat cameraMat) {
    if (isOpened) {
        boolean tempBool = capturedVideo.read(cameraMat);
        System.out.println("VideoCapture returned mat? "+tempBool);

        if (!cameraMat.empty()) {
            System.out.println("Print image size: "+cameraMat.size());
            //processing image captured in cameraMat object

        } else {
            System.out.println("Mat is empty.");
        }
    } else {
        System.out.println("Camera connection problem. Check addressString");
    }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接