Based on Opencv binocular camera camera program

  • 2020-06-15 10:04:24
  • OfStack

This article shares the specific code of Opencv binocular camera program for your reference. The specific content is as follows

The binocular camera I use is an usb cable connected to the computer. The operating environment is vc2015, opencv3.0. Save the pictures from the left and right cameras separately.

Post code (C++)


#include"stdafx.h"
#include<iostream>
#include<string>
#include<sstream>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/videoio.hpp>
#include<opencv2/opencv.hpp>
#include<stdio.h>
using namespace std;
using namespace cv;
const char* keys =
{
 "{help h usage ? | | print this message}"
 "{@video | | Video file, if not defined try to use webcamera}"
};
 
int main(int argc, const char** argv) // Program main function 
{
 CommandLineParser parser(argc, argv, keys);
 parser.about("Video Capture");
 
 if (parser.has("help")) // Help information 
 {
 parser.printMessage();
 return 0;
 }
 String videoFile = parser.get<String>(0);
 
 if (!parser.check())
 {
 parser.printErrors();
 return 0;
 }
 
 VideoCapture cap;
 if (videoFile != "")
 {
 cap.open(videoFile);
 }
 else
 {
 
 cap.open(0); // Turn on the camera. The computer has its own camera 1 A number of 0 , the external camera number is 1 , mainly to view the number of your own camera in the device Manager. 
     //--------------------------------------------------------------------------------------
 
 cap.set(CV_CAP_PROP_FRAME_WIDTH, 2560); // Sets the width of the captured video 
 cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720); // Set the height of the captured video 
 }
 if (!cap.isOpened())             // Determine if the camera was successfully turned on 
 {
 cout << " Camera failed to open !" << endl;
 return -1;
 }
    Mat frame, frame_L,frame_R;
    
 cap >> frame;                // Capture from camera 1 frame 
 
 Mat grayImage;                // Used to store grayscale data 
 
 double fScale = 0.5;             // I'm going to define the scaling factor, right 2560*720 Image zoom display ( 2560*720 The image is too large and the RESOLUTION of the LCD screen is small, so it needs to be zooming to display on the screen.)  
 Size dsize = Size(frame.cols*fScale, frame.rows*fScale);
 Mat imagedst = Mat(dsize, CV_32S);
 resize(frame, imagedst, dsize);
    char key;
    char image_left[200];
    char image_right[200];
    int count1 = 0;
    int count2 = 0;
    namedWindow(" The picture 1",1);
    namedWindow(" The picture 2",1);
   
 
    while (1)
 {
 key = waitKey(50);
 cap >> frame;   // Capture from camera 1 frame 
 resize(frame, imagedst, dsize);     // Scale the captured image 
 
 frame_L = imagedst(Rect(0, 0, 640, 360)); // Gets the zoomed left Camera The image of 
 namedWindow("Video_L", 1);
 imshow("Video_L", frame_L);
 
 frame_R = imagedst(Rect(640, 0, 640, 360)); // Get right after scaling Camera The image of 
 namedWindow("Video_R", 2);
 imshow("Video_R", frame_R);
 if (key == 27) // Press the ESC exit 
  break;
 if (key == 32) //  Press space to start taking pictures. Save the pictures in the project file 
 {
  sprintf_s(image_left, "image_left_%d.jpg", ++count1);
  imwrite(image_left, frame_L);
  imshow(" The picture 1", frame_L);
  sprintf_s(image_right, "image_right_%d.jpg", ++count2);
  imwrite(image_right, frame_R);
  imshow(" The picture 2", frame_R);
 }
     }
   
     return 0;
}

Related articles: