一.opencv 打开双目摄像头只能采集一个摄像头的问题

本人采用单USB接口的双目摄像头,总是只打开左摄像头的图,经摸索后发现主要就是分辨率的问题,如果分辨率设置正确,便可以打开左右目合成图(在一张图上),如果设置不正确,可能报错,也可能只显示左目图像,所以解决如下

int main() {

 cv::VideoCapture cap(0);  //把path改为0,则读取摄像头
 cap.set(cv::CAP_PROP_FRAME_WIDTH,1440);   //设置分辨率
 cap.set(cv::CAP_PROP_FRAME_HEIGHT,480);   //设置分辨率

 cv::Mat img;
 while(1){

   cap.read(img);

   cv::imshow("image",img);

   cv::waitKey(1);
 }
 return 0;
}

二.左右目图像分割成两幅图

由于获得的是左右目合成图,所以需要分割开,具体做法如下

int main()
{
     while(1)
    {
     VideoCapture capture(0);
     if (!capture.isOpened())
     {
         cout<<"can not open the camera"<<endl; cin.get(); exit(1);
     }
     capture.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH);
     capture.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
     int count=0;

     Rect leftRect(0, 0, WIDTH/2, HEIGHT);   //创建一个Rect框,属于cv中的类,四个参数代表x,y,width,height
     Rect rightRect(WIDTH/2, 0, WIDTH/2, HEIGHT);
     while (1) {
         Mat frame; capture>>frame; //载入图像
         if (frame.empty())
         {
             //判断图像是否载入 cout<<"can not load the frame"<<endl;
         } 
        else {
             count++;
             if (count == 1) {
                 cout<<frame.cols<<" "<<frame.rows<<endl;
             }
             Mat leftImg,rightImg;
             frame(leftRect).copyTo(leftImg);
             frame(rightRect).copyTo(rightImg);
             imshow("left",leftImg);
             imshow("right",rightImg);
             //imshow("oriImage", frame);
             char c=waitKey(30); //延时30毫秒 if (c == 27) //按ESC键退出 break;

         }
     }
   }
GitHub 加速计划 / opencv31 / opencv
77.36 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:1 个月前 )
3919f33e G-API: Introduce level optimization flag for ONNXRT backend #26293 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 1 天前
489df18a Use border value in ipp version of warp affine #26313 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐