OpenCV学习笔记——视频的边缘检测

使用Canny算子进行边缘检测,并分开输出到三个窗口中,再给每一个窗口添加文字

代码

#include"cv.h"
#include"highgui.h"
//读入视频并转换为灰度图再作边缘检测
//并分开三个窗口输出
IplImage *frame,*gray,*canny,*uni,*temp;
int wid=-1, hei=-1;
int main(void)
{
	CvCapture *src = cvCreateFileCapture("Megamind.avi");
	//cvNamedWindow("Union");
	cvNamedWindow("Origin");
	cvNamedWindow("Gray");
	cvNamedWindow("Canny");
	CvFont r, g, c;
	cvInitFont(&r, CV_FONT_HERSHEY_TRIPLEX, 1, 1, 0);
	cvInitFont(&g, CV_FONT_HERSHEY_TRIPLEX, 1, 1, 0);
	cvInitFont(&c, CV_FONT_HERSHEY_TRIPLEX, 1, 1, 0);
	while (1)
	{
		frame = cvQueryFrame(src);
		if (!frame)
			break;
		/*if (wid == -1)
		{
			wid = frame->width;
			hei = frame->height;
		}*/
		if (cvWaitKey(33) == 27)
			break;
		temp = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
		cvCopy(frame, temp);
		cvPutText(temp, "Original", CvPoint(10, 30), &r, CV_RGB(0, 204, 153));//显示文字
		cvShowImage("Origin", temp);	

		gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);//先创建图像指针
		cvConvertImage(frame, gray);
		canny = cvCreateImage(cvGetSize(gray), IPL_DEPTH_8U, 1);//同上先创建
		cvPutText(gray, "Gray", CvPoint(10, 30), &g, CV_RGB(0, 204, 153));//显示文字
		cvShowImage("Gray", gray);//显示到灰度窗口中

		
		cvCanny(gray, canny, 20, 250, 3);//边缘检测处理
		cvPutText(canny, "Canny", CvPoint(10, 30), &c, CV_RGB(0, 204, 153));
		cvShowImage("Canny", canny);//显示到边缘检测窗口中
		
		/*uni = cvCreateImage(CvSize(3 * frame->width, frame->width), IPL_DEPTH_8U,3);
		CvRect pos1 = CvRect(0, 0, wid, hei);
		CvRect pos2 = CvRect(wid, 0, wid, hei);
		CvRect pos3 = CvRect(2*wid, 0 ,wid, hei);

		cvSetImageROI(uni, pos1);
		cvCopy(frame, uni);
		cvResetImageROI(uni);

		cvSetImageROI(uni, pos2);
		uni->nChannels = 1;
		cvCopy(gray, uni);
		cvResetImageROI(uni);

		cvSetImageROI(uni, pos3);
		uni->nChannels = 1;
		cvCopy(canny, uni);
		cvResetImageROI(uni);
		cvShowImage("Union", uni);*/
	}
	cvDestroyAllWindows();
	cvReleaseCapture(&src);
	cvReleaseImage(&gray);
	cvReleaseImage(&canny);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值