本文實(shí)例為大家分享了Opencv實(shí)現(xiàn)雙目攝像頭拍照程序的具體代碼,供大家參考,具體內(nèi)容如下
我用的雙目攝像頭是一根usb線接入電腦。運(yùn)行環(huán)境是vc2015,opencv3.0。將左右兩個(gè)攝像頭拍到的圖片分別保存起來(lái)。
貼出代碼(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) //程序主函數(shù){ CommandLineParser parser(argc, argv, keys); parser.about("Video Capture"); if (parser.has("help")) //幫助信息 { 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); //打開(kāi)相機(jī),電腦自帶攝像頭一般編號(hào)為0,外接攝像頭編號(hào)為1,主要是在設(shè)備管理器中查看自己攝像頭的編號(hào)。 //-------------------------------------------------------------------------------------- cap.set(CV_CAP_PROP_FRAME_WIDTH, 2560); //設(shè)置捕獲視頻的寬度 cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720); //設(shè)置捕獲視頻的高度 } if (!cap.isOpened()) //判斷是否成功打開(kāi)相機(jī) { cout << "攝像頭打開(kāi)失敗!" << endl; return -1; } Mat frame, frame_L,frame_R; cap >> frame; //從相機(jī)捕獲一幀圖像 Mat grayImage; //用于存放灰度數(shù)據(jù) double fScale = 0.5; //定義縮放系數(shù),對(duì)2560*720圖像進(jìn)行縮放顯示(2560*720圖像過(guò)大,液晶屏分辨率較小時(shí),需要縮放才可完整顯示在屏幕) 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("圖片1",1); namedWindow("圖片2",1); while (1) { key = waitKey(50); cap >> frame; //從相機(jī)捕獲一幀圖像 resize(frame, imagedst, dsize); //對(duì)捕捉的圖像進(jìn)行縮放操作 frame_L = imagedst(Rect(0, 0, 640, 360)); //獲取縮放后左Camera的圖像 namedWindow("Video_L", 1); imshow("Video_L", frame_L); frame_R = imagedst(Rect(640, 0, 640, 360)); //獲取縮放后右Camera的圖像 namedWindow("Video_R", 2); imshow("Video_R", frame_R); if (key == 27) //按下ESC退出 break; if (key == 32) // 按下空格開(kāi)始拍照?qǐng)D片保存在工程文件下 { sprintf_s(image_left, "image_left_%d.jpg", ++count1); imwrite(image_left, frame_L); imshow("圖片1", frame_L); sprintf_s(image_right, "image_right_%d.jpg", ++count2); imwrite(image_right, frame_R); imshow("圖片2", frame_R); } } return 0;}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選