#include#include using namespace cv; using namespace std; Mat input_Image, Left_Image, Right_Image; //Mat矩阵 //写入图像数据 void save( int times ) { string a = "0"; string leftImage = "left0.jpg"; //设置照片的名字格式 string rightImage = "right0.jpg";//设置照片的名字格式 int re_length = 1; a = to_string(times); //将int型数据转换为string型 //将字符串第4个位置(因为是从0开始算的)的1位字符替换为a;比如 a="1",则替换后为"left1.jpg" leftImage.replace(4, 1, a); //将字符串第5个位置(因为是从0开始算的)的1位字符替换为a;比如a="1",则替换后为"right1.jpg" rightImage.replace(5, 1, a); imwrite(leftImage, Left_Image); //将Left_Image(Mat型)中的图像数据写入到leftImage(string)中 imwrite(rightImage, Right_Image); //将Right_Image(Mat型)中的图像数据写入到rightImage(string)中 } //获取图像数据 void setCam(int weight, int height, int num) { int state = 0; string Error; VideoCapture cam(0); int times = 1; cam.set(CV_CAP_PROP_frame_WIDTH, weight); cam.set(CV_CAP_PROP_frame_HEIGHT, height); while (! cam.isOpened() ) //报错 { Error = "camre is not open"; return; } while (true) { cam >> input_Image; //将获取的影像数据传入input_Image矩阵中 //分为左右两个窗口显示获取的图像 Left_Image = input_Image(Rect( 0, 0, input_Image.size().width / 2, input_Image.size().height)); Right_Image = input_Image(Rect( input_Image.size().width / 2, 0, input_Image.size().width / 2, input_Image.size().height)); //显示图像 imshow("LeftImage", Left_Image); imshow("ReightImage", Right_Image); save( times );//保存图像数据 times += 1; //在等待的30毫秒期间如果按下Esc键或者循环保存图片已经有40张(左右各20张),则跳出循环 if (27 == waitKey(30) || times > 20 ) break; //waitKey(100)等待100毫秒 waitKey(100); } } int main() { int weight = 1280; //图像捕捉窗口的宽度 int height = 480; //图像捕捉窗口高度 setCam( weight, height, 20 ); system("pause"); //程序停止不关闭控制台窗口 return 0; }



