OpenCV实用程序:“OpenCV相机”——获取、保存选定时刻的摄像头图像

该程序是用于获取、保存选定时刻的摄像头图像的程序,可用于采集特征匹配算法所需的模板。博主原本想写以并行方式实时截取图像的摄像头图像获取程序,但因对并行的了解有限,网上资料较少,暂时只写出了这个通过中断程序来保存中断前显示的最后一帧图片的程序。

用法:

(1)插入USB摄像头,复制该程序进main.cpp,增加OpenCV动态链接。

(2)修改代码注释处想获得的图像的分辨率(水平、垂直像素数,需要摄像头支持该分辨率),编译运行。将看到摄像头当前图像被实时显示出来。

(2)摄像头显示出想要的图片时,按下Ctrl+C中断程序(因为signal的保护,不会出现内存溢出)。到main.cpp目录下可见一名为“test_picture.jpg”的图片即为保存得到的图片。将其改为别的名字,即可进行下一次截取(否则会被覆盖)。

#include <opencv2/opencv.hpp>
#include <signal.h>
#include <unistd.h>

//#include "sub_aming.hpp"
using namespace cv;
using namespace std;

bool stop = false;
void sigIntHandler(int signal)
{
	stop = true;
	cout<<"Honestly, you are out!"<<endl;
}

int main()
{
	/* init camera */

	CvCapture* pCapture = cvCreateCameraCapture(0);
	cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 800);//分辨率水平像素数
	cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 600);//分辨率垂直像素数

	IplImage *pFrame = 0;
	const char *pImageFileName ="test_picture.jpg";

	if(NULL == pCapture)
	{
		fprintf(stderr,"Can't initialize webcam!\n");
		return -1;
	}

	signal(SIGINT, sigIntHandler);

	while(!stop)
	{
		cout << "Your Dream is here" << endl;
		pFrame = cvQueryFrame(pCapture); // query a frame

		if(NULL == pFrame)
		{
			fprintf(stderr,"Can't get a frame!\n");
			return -1;
		}

		cvShowImage("What you see",pFrame);
		cvWaitKey(1);
	}

	if(stop)
	{
		cvSaveImage(pImageFileName, pFrame);
	}
	cvReleaseCapture(&pCapture); // free memory

	return 1;
}


博主未来可能将该程序改为按键检测与OpenCV读取、显示摄像头双线程并行的程序,实时按键截取,并给予新截取的图片新的命名。欢迎交流讨论!


附上相关博文(OpenCV特征提取算法若在摄像头上进行应用,建议用此程序获取模板):
要点初见:OpenCV3中ORB特征提取算法的实现与分析

http://blog.csdn.net/m0_37857300/article/details/79037988

要点初见:OpenCV3中CUDA ORB特征提取算法的实现(GPU加速的ORB算法)

http://blog.csdn.net/m0_37857300/article/details/79039214

猜你喜欢

转载自blog.csdn.net/m0_37857300/article/details/79038894
今日推荐