Ask Your Question
0

Is openCv compatible with multiple Cameras???

asked 2017-11-02 01:47:58 -0600

ChukZ gravatar image

updated 2017-11-02 02:47:47 -0600

This code is workable. method 1 returns colored frame while method 2 returns binary frames.Although i initialized two cameras , I am still thinking that this program does not run with two cameras.It is using only one camera. Am i right ?? or wrong??

 @FXML
    public void mcamera(ActionEvent ee){

        new Thread(() -> method1()).start();
        new Thread(() -> method2()).start();
    }




    public void method1() {
       VideoCapture camera1 = new VideoCapture("/home/chinthaka/Downloads/wow/giphy.mp4");
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera can't be opened!");
            return;
        }


        Mat frame = new Mat();

        while (true) {
            if (camera1.read(frame)) {
                System.out.println("Frame Obtained");
                System.out.println("Captured Frame Width " +
                        frame.width() + " Height " + frame.height());
                Imgcodecs.imwrite("camera.jpg", frame);
                System.out.println("OK");


                while (camera1.read(frame)) {
                    System.out.println("Frame Obtained");
                    System.out.println("Captured Frame Width " +
                            frame.width() + " Height " + frame.height());
                    Imgcodecs.imwrite("camera.jpg", frame);
                    System.out.println("OK");





                    bufferedImage = matToBufferedImage(frame);


                    showWindow(bufferedImage);
                }

                camera1.release();

                break;


            }
        }

    }
    public void method2() {
        VideoCapture camera2 = new VideoCapture("/home/chinthaka/Downloads/wow/cinnamon.mp4");
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera can't be opened!");
            return;
        }

        Mat frame = new Mat();


        while (true) {
            if (camera2.read(frame)) {
                System.out.println("Frame Obtained");
                System.out.println("Captured Frame Width " +
                        frame.width() + " Height " + frame.height());
                Imgcodecs.imwrite("camera.jpg", frame);
                System.out.println("OK");


                while (camera2.read(frame)) {
                    System.out.println("Frame Obtained");
                    System.out.println("Captured Frame Width " +
                            frame.width() + " Height " + frame.height());
                    Imgcodecs.imwrite("camera.jpg", frame);
                    System.out.println("OK");


                    //convert original color frame into gray scale frame.
                    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);

                    //convert gray scale frame to binary frame.
                    threshold(frame, frame, 100, 255, THRESH_BINARY);

                    bufferedImage = matToBufferedImage(frame);
                    showWindow(bufferedImage);


                }

                camera2.release();

                break;


            }
        }
    }
edit retag flag offensive close merge delete

Comments

1
  • you're not using any cameras, but video files (and you can have as many of those, as your hardware can handle)
  • if both streams use the same input, you probably need only one (it looks more like a design problem from here)
  • you probably don't need multiple threads (only increases complexity, also some things as cascade classifiers are not threadsafe)
berak gravatar imageberak ( 2017-11-02 02:35:27 -0600 )edit

Thank you @berak for your comment. but this post---your comment-----you have done something like above.Isn't it??. post. Could you please tell me how to integrate multiple cameras???

ChukZ gravatar imageChukZ ( 2017-11-02 02:45:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-11-02 03:11:07 -0600

berak gravatar image

updated 2017-11-02 03:34:23 -0600


bool button1pressed = false;
void action1(ActionEvent e)  {
        button1pressed = true;
}


main() {

        VideoCapture camera1 = new VideoCapture(0);
        if (!camera1.isOpened()) {
            System.out.println("Error! Camera1 can't be opened!");
            return;
        }

        VideoCapture camera2 = new VideoCapture(1); // next id
        if (!camera2.isOpened()) {
            System.out.println("Error! Camera2 can't be opened!");
            return;
        }


        Mat frame1 = new Mat();
        Mat frame2 = new Mat();

        while (true) {
            if (! camera1.read(frame1)) {
                System.out.println("cam1 disconnected");
                return;
           }
            if (! camera2.read(frame2)) {
                System.out.println("cam2 disconnected");
                return;
           }

           // check events (poll)
           if (button1pressed)  {
                 // do something
                  button1pressed = false;
           }

           //
           // your processing code for frame1 and frame2 here
           //
       }
}

some notes:

  • VideoCapture has an internal fifo (with webcams, not movies), so if you don't read images continuously (flush the fifo), you get stale, outdated frames.
  • thus, you should not start your Capture(s) from an event method (stopping and restarting them is also very expensive), but let them run for the whole duration of your prog, if you need something done with events, rather send signals from there to your main loop.
  • when using usb webcams, your usb hub is the bottleneck here. if you experience lags, try to find a different hub, or reduce the resolution
  • camera ids are dynamic, 0 is the first, you plugged in, 1 the second, etc. it's the order, that matters here. if you disconnect them, and plug them in in a different order, your ids will change, too !
edit flag offensive delete link more

Comments

VideoCapture camera1 = new VideoCapture(0); That means if we use file path instead of 0 ,1 in videocapture , there is not using camera neh!. Isn't it??@berak.

ChukZ gravatar imageChukZ ( 2017-11-02 04:24:20 -0600 )edit
1

obviously !

(well, it might also be the url of an ip cam, or remote video)

berak gravatar imageberak ( 2017-11-02 04:28:50 -0600 )edit

Thank You I have learnt a lot of thing

ChukZ gravatar imageChukZ ( 2017-11-02 04:30:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-02 01:47:58 -0600

Seen: 1,860 times

Last updated: Nov 02 '17