web-dev-qa-db-ja.com

OpenCvを搭載したIPカメラJava

IPカメラからビデオストリームを取得して表示しようとしています。ここでいくつかのサンプルコードを見つけました: http://answers.opencv.org/question/24012/reading-video-stream-from-ip-camera-in-opencv-Java/

しかし、それは私にとってはうまくいきません。内部Webカメラを使用するとコードは機能しますが、カメラをIPに切り替えると機能しません。理由がわかりません。誰か助けてくれませんか?これが私のコードです:

    import Java.awt.BorderLayout;
    import Java.awt.Image;
    import Java.awt.image.BufferedImage;
    import Java.awt.image.DataBufferByte;
    import Java.io.File;
    import Java.io.IOException;

    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.imgproc.Imgproc;

    public class openCVTest
    {

    public openCVTest()
    {

        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException
    {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //VideoCapture camera = new VideoCapture("http://192.168.0.7/image.jpg");
        VideoCapture camera = new VideoCapture(0);

        if (camera.isOpened()) 
        {
            System.out.println("Video is captured");
        }
        else
        {
            System.out.println("");
        }
        videoCamera cam = new videoCamera(camera);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.add(cam);
        frame.setSize(800,800);  
        frame.setVisible(true);


        while(camera.isOpened())
        {
            cam.repaint();


        }

    }



      }


import Java.awt.Graphics;
import Java.awt.Point;
import Java.awt.image.BufferedImage;
import Java.awt.image.DataBufferByte;
import Java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.core.*;



@SuppressWarnings("serial")
public class videoCamera extends JPanel
{
    VideoCapture camera; 

    public videoCamera(VideoCapture cam) 
    {

        camera  = cam; 

    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        // TODO Auto-generated method stub

    }
    public BufferedImage Mat2BufferedImage(Mat m)
    {

        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1)
        {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return img;


    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Mat mat = new Mat();

        if( camera.read(mat))
        {
            System.out.print("IMAGE");


        }

        BufferedImage image = Mat2BufferedImage(mat);
        //Mat gray = turnGray(mat);
        //MatOfRect objects = new MatOfRect();
        //CascadeClassifier cas = new CascadeClassifier();
        //cas.detectMultiScale(gray,objects);
        //Mat thresh  = threash( gray);

        //BufferedImage image = Mat2BufferedImage(thresh);
        g.drawImage(image,10,10,image.getWidth(),image.getHeight(), null);

    }
    public Mat turnGray( Mat img)

    {
        Mat mat1 = new Mat();
        Imgproc.cvtColor(img, mat1, Imgproc.COLOR_RGB2GRAY);
        return mat1;
    }
    public Mat threash(Mat img)
    {
        Mat threshed = new Mat();
        int SENSITIVITY_VALUE = 100;
        Imgproc.threshold(img, threshed, SENSITIVITY_VALUE,255,Imgproc.THRESH_BINARY);
        return threshed;
    }


}

お手伝いありがとう。私はあなたが言ったことを使って適切なIPアドレスを見つけることができました(http://192.168.0.6/VIDEO.CGI)(はい、7が6になり、別のカメラになりました)ブラウザでビデオを取得します。しかし、それでも次のエラーが発生します。

 Exception in thread "AWT-EventQueue-0" Java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0
        at Java.awt.image.SampleModel.<init>(Unknown Source)
        at Java.awt.image.ComponentSampleModel.<init>(Unknown Source)
        at Java.awt.image.PixelInterleavedSampleModel.<init>(Unknown Source)
        at Java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at Java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at Java.awt.image.Raster.createInterleavedRaster(Unknown Source)
        at Java.awt.image.ComponentColorModel.createCompatibleWritableRaster(Unknown Source)
        at Java.awt.image.BufferedImage.<init>(Unknown Source)
        at videoCamera.Mat2BufferedImage(videoCamera.Java:54)
        at videoCamera.paintComponent(videoCamera.Java:74)
        at javax.swing.JComponent.Paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.Paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.Paint(Unknown Source)
        at javax.swing.JLayeredPane.Paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.BufferStrategyPaintManager.Paint(Unknown Source)
        at javax.swing.RepaintManager.Paint(Unknown Source)
        at javax.swing.JComponent.Paint(Unknown Source)
        at Java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at Sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at Sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at Java.awt.Container.Paint(Unknown Source)
        at Java.awt.Window.Paint(Unknown Source)
        at javax.swing.RepaintManager$3.run(Unknown Source)
        at javax.swing.RepaintManager$3.run(Unknown Source)
        at Java.security.AccessController.doPrivileged(Native Method)
        at Java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.access$1100(Unknown Source)
        at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
        at Java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at Java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at Java.awt.EventQueue.access$200(Unknown Source)
        at Java.awt.EventQueue$3.run(Unknown Source)
        at Java.awt.EventQueue$3.run(Unknown Source)
        at Java.security.AccessController.doPrivileged(Native Method)
        at Java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at Java.awt.EventQueue.dispatchEvent(Unknown Source)
        at Java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at Java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at Java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at Java.awt.EventDispatchThread.run(Unknown Source)

VideoCaptureがフレームを正しくつかんでいないためだと思います。

これは私が使おうとしているカメラです http://www.trendnet.com/products/proddetail.asp?prod=150_TV-IP100W-N

7
user1376339

。jpgは単一の画像ファイルを参照し、。mjpgはビデオストリームへのアクセスを提供します。接続に使用するIPとポートを定義することが重要です。

お使いのデバイスとカメラのウェブインターフェースで定義されている設定に応じて、URLは異なります。

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/?dummy=param.mjpg");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/mjpeg.cgi");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/mjpg/mjpeg.cgi");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/video.mjpeg");

VideoCapture camera = new VideoCapture("http://192.168.0.7:8080/video.cgi?.mjpg");

ブラウザ経由で有効なURLにアクセスすると、ビデオストリームが表示されます。動作するアドレスを見つけたら、それをVideoCaptureコンストラクターに渡すだけです。この例では、HTTP経由でストリームにアクセスする方法を示しましたが、RTSPプロトコルもサポートされています。

11
karlphillip

3〜4週間のハードワークの後、私はこれに対する100%実用的な解決策を見つけました

まず最初に、ffmpegのdllファイルを動的にロードする必要があります。

System.loadLibrary("[NAME OF YOUR DLL FILE]") You can find the required dll file in **opencv/build/x64/vc11/bin** DLL私の場合は"opencv_ffmpeg2413_64.dll"ファイルをプロジェクトのデフォルトパスにコピーして使用します

System.loadLibrary("opencv_ffmpeg2413_64");//You May have different File Name Depending on the Version of OpenCV Installed on your Computer

その後、あなたは単に使用することができます

VideoCapture ipcamera = new VideoCapture("[RTSP URL OF THE IP Camera]")//I used this Demo Link (rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov)
2
Anas khan