小白毕设调用CCD(就是USB摄像机)并获得图像序列
软件是VS2017,OpenCV3.4.1版本
首先是调用摄像头,自己借用的别人的程序,头文件不是很懂,所以比较多,都放上去了
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(0);
//设置摄像头
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
//确认是否成功打开摄像头
if (!capture.isOpened())
{
cout << "打开摄像头失败,退出" << endl;
exit(-1);
}
Mat frame(Size(1280, 720), CV_8UC3);
while (1)
{
if (!capture.read(frame))
break;
capture >> frame;
if (!frame.empty()) //很重要,确保摄像头已启动
{
imshow("window", frame);//显示摄像头
}
if (waitKey(30) >= 0) break;//延时时间可调
}
return 0;
}
可以用下面的一些函数,唔,貌似叫API,更改摄像头的一些设置
capture.set(CV_CAP_PROP_SETTINGS, 1);//弹出一个设置界面
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
capture.set(CV_CAP_PROP_FPS, 30);//帧数
capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
capture.set(CV_CAP_PROP_CONTRAST, 40);//对比度 40
capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
capture.set(CV_CAP_PROP_HUE, 50);//色调 50
capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50
capture.set(CV_CAP_PROP_FOCUS, 85);//变焦85(此相机0--1023)
如果想打印现有参数,可以用下面的代码:
printf("width = %.2f\n", capture.get(CV_CAP_PROP_FRAME_WIDTH));
printf("height = %.2f\n", capture.get(CV_CAP_PROP_FRAME_HEIGHT));
printf("fbs = %.2f\n", capture.get(CV_CAP_PROP_FPS));
printf("brightness = %.2f\n", capture.get(CV_CAP_PROP_BRIGHTNESS));
printf("contrast = %.2f\n", capture.get(CV_CAP_PROP_CONTRAST));
printf("saturation = %.2f\n", capture.get(CV_CAP_PROP_SATURATION));
printf("hue = %.2f\n", capture.get(CV_CAP_PROP_HUE));
printf("exposure = %.2f\n", capture.get(CV_CAP_PROP_EXPOSURE));
printf("FOCUS = %.2f\n", capture.get(CV_CAP_PROP_FOCUS));
修改参数和获取参数的代码,可以直接插在调用CCD的程序里面,对,“插”进去
下面是调用摄像头,改变摄像头焦点,从1到1022(这是我的变化范围),并获取图像序列的内容,我学到三种方式:
方法1——输出到imwrite的默认文件夹(应该是无法更改的,反正俺不会)
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(1);//数字0是电脑的easycream,1是外接USB的CCD
//设置摄像头
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
//确认是否成功打开摄像头
if (!capture.isOpened())
{
cout << "打开摄像头失败,退出" << endl;
exit(-1);
}
int i = 0;
int a = 0;
string b = "样本";//保存图片名称
string ext = ".jpg";//保存图片格式
Mat frame(Size(1280, 720), CV_8UC3);//长宽1280×7208位(bit)uchar类型的3通到图像矩阵
while (1)
{
if (!capture.read(frame))
break;
capture >> frame;
if (!frame.empty()) //很重要,确保摄像头已启动
{
imshow("window", frame);//显示摄像头
}
while (i < 33)
{
capture.read(frame);
if (i > 0)
a = 32 * i - 2;
else
a = 1;
capture.set(CV_CAP_PROP_FOCUS, a);
printf("FOCUS = %.2f\n", capture.get(CV_CAP_PROP_FOCUS));
//字符串函数输出命名
string name(b);
ostringstream ss;
string y = "focus";
ostringstream F;
ss << i;
name += ss.str();
name += y.c_str();
F << a;
name += F.str();
cout << name << std::endl;//显示框输出文件名
imwrite(name, frame);//输出到默认文件夹
}
if (waitKey(30) >= 0) break;//延时时间可调
}
return 0;
}
输出结果是一串名字:“样本i focus a.jpg”,其中 i 和 a 都是int变量
方法2——我最后用的程序,个人最推荐,可以自定义输出图片名称(当然要有规律,能被电脑区分开),自定义输出到指定目录:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(1);//数字0是电脑的easycream,1是外接USB的CCD
//设置摄像头
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
//确认是否成功打开摄像头
if (!capture.isOpened())
{
cout << "打开摄像头失败,退出" << endl;
exit(-1);
}
int i = 0;
int a = 0;
char names[255];
Mat frame(Size(1280, 720), CV_8UC3);
while (1)
{
if (!capture.read(frame))
break;
capture >> frame;
if (!frame.empty()) //很重要,确保摄像头已启动
{
imshow("window", frame);//显示摄像头
}
while (i < 33)
{
capture.read(frame);
if (i > 0)
a = 32 * i - 2;
else
a = 1;
capture.set(CV_CAP_PROP_FOCUS, a);
printf("FOCUS = %.2f\n", capture.get(CV_CAP_PROP_FOCUS));
sprintf_s(names, "E:\\哥哥耀\\本科\\毕业设计\\控制程序\\学习\\CCD部分\\样本图像序列\\样本%dfocus%d.jpg",i,a);
cout << names << endl;
imwrite(names, frame);
}
if (waitKey(30) >= 0) break;//延时时间可调
}
return 0;
}
输出到指定目录的名字格式为:“样本i focus a.jpg”,i和a都是我定义的int变量
方法3——输出到指定目录,但是文件名只能是数字+扩展名:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(1);//数字0是电脑的easycream,1是外接USB的CCD
//设置摄像头
capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
//确认是否成功打开摄像头
if (!capture.isOpened())
{
cout << "打开摄像头失败,退出" << endl;
exit(-1);
}
int i = 0;
int a = 0;
Mat frame(Size(1280, 720), CV_8UC3);
while (1)
{
if (!capture.read(frame))
break;
capture >> frame;
if (!frame.empty()) //很重要,确保摄像头已启动
{
imshow("window", frame);//显示摄像头
}
while (i < 33)
{
capture.read(frame);
//waitKey(30);
if (i > 0)
a = 32 * i - 2;
else
a = 1;
capture.set(CV_CAP_PROP_FOCUS, a);
printf("FOCUS = %.2f\n", capture.get(CV_CAP_PROP_FOCUS));
string names = "E:/哥哥耀/本科/毕业设计/控制程序/学习/CCD部分/样本图像序列/" + to_string(i) + ".jpg";
//命名出来的文件是i.jpg
cout << names << endl;
imwrite(names, frame);
}
if (waitKey(30) >= 0) break;//延时时间可调
}
return 0;
}
输出的图片名字为:“i.jpg”,i是程序里定义的int变量
就到这儿了~
如果有人看,而且有需要的话,我后期可以把运行结果再挂上来,这次不想在运行程序了,午安各位。