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

你可能感兴趣的:(OpenCV学习笔记——视频的边缘检测)