栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

opencv打开相机获取图像并保存

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

opencv打开相机获取图像并保存

#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;


}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/503757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号