The graduation project is to do a measurement of the object's distance through a binocular camera, and then control the model car to avoid the near obstacles. Therefore, the image of the binocular camera mounted on the model car needs to be transmitted and sent to the computer for double. The distance measurement algorithm calculates the distance, and the computer sends the calculated result to the car. The car controls the direction of travel so as to avoid obstacles.The Pcduino development board is used on the car, the Ubuntu system is installed, the Arduino expansion board is externally inserted, and the hardware part of the car is connected. After binocular cameras bought from Taobao, the way to get binocular camera images using java cv is similar to that of monocular cameras.The difference is that in order to separate the binocular cameras, the resolution parameters in the code need to be set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Package cn.wangbaiyuan.doublecamera; Import javax.swing.JFrame; Import org.bytedeco.javacv.CanvasFrame; Import org.bytedeco.javacv.OpenCVFrameConverter; Import org.bytedeco.javacv.FrameGrabber.Exception; Import org.bytedeco.javacv.OpenCVFrameGrabber; /** * Call the local camera window video * @author BrainWang * @version March 6, 2017 */ Public class Camera { Public static void main(String[] args) throws Exception, InterruptedException { OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(1);//device number, your computer may have multiple cameras, 0 and 1 try their own corresponding to which one Grabber.start(); //Start getting camera data CanvasFrame canvas = new CanvasFrame("camera") ;//Create a new window canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setAlwaysOnTop(true); While(true) { If(!canvas.isDisplayable()) {// Window is closed Grabber.stop();// Stop crawling System.exit(2);//Exit } canvas.showImage(grabber.grab());//Get the camera image and place it on the window, where Frame frame=grabber.grab(); frame is a frame of video image Thread.sleep (50) ;/ / 50 ms refresh the image } } } |
If you do not set any resolution, the effect is as follows:

It is not familiar, we see this in a movie theater watching 3D movies without glasses.For developers, it's obviously not a good idea to mix two images together. You need to make the following settings in your code.
1 2 3 4 |
Int captureWidth = 1280; Int captureHeight = 480; grabber.setImageWidth(captureWidth); grabber.setImageHeight(captureHeight); |
Because the resolution of the camera in the author's hands is 640,480, the resolution is changed to 1280480, and the picture can be successfully separated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
Package cn.wangbaiyuan.doublecamera; Import javax.swing.JFrame; Import org.bytedeco.javacv.CanvasFrame; Import org.bytedeco.javacv.OpenCVFrameConverter; Import org.bytedeco.javacv.FrameGrabber.Exception; Import org.bytedeco.javacv.OpenCVFrameGrabber; /** * Call the local camera window video * @author BrainWang * @version March 6, 2017 */ Public class Camera { Public static void main(String[] args) throws Exception, InterruptedException { OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(1); Int captureWidth = 1280; Int captureHeight = 480; grabber.setImageWidth(captureWidth); grabber.setImageHeight(captureHeight); Grabber.start(); //Start getting camera data CanvasFrame canvas = new CanvasFrame("camera") ;//Create a new window canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setAlwaysOnTop(true); While(true) { If(!canvas.isDisplayable()) {// Window is closed Grabber.stop();// Stop crawling System.exit(2);//Exit } canvas.showImage(grabber.grab());//Get the camera image and place it on the window, where Frame frame=grabber.grab(); frame is a frame of video image Thread.sleep (50) ;/ / 50 ms refresh the image } } } |
The effect after the picture is separated is as follows:

The code uses the java cv library. If it is a maven project, you need to configure the dependencies: Add in the project node in pom.xml:
1234567 <dependencies><dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>1.3.1</version></dependency></dependencies>More environment configurations can be accessed on the official javathv home page of Github.
This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/by-using-java-cv-for-binocular-camera-image-under-ubuntu-2.html
No Comment