题目内容:
a. Create a program that (1)reads frames from a video, (2) turns the result to grayscale,
and (3) performs Canny edge detection on the image.Display all three
stages of processing in three diff erent windows,with each window appropriately
named for its function.
程序:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
IplImage* Canny(IplImage* img ,double lowThread,double highThread,double aperture)//边缘检测,使用canny算子
{
if(img->nChannels!=1)
{
return NULL;
}
IplImage* out =cvCreateImage(cvGetSize(img),img->depth,1);
cvCanny(img,out,lowThread,highThread,aperture);
return (out);
}
IplImage* colorchange(IplImage* in)//单通道灰度图像转变
{
IplImage* out = cvCreateImage(cvGetSize(in),in->depth,1);
cvCvtColor(in,out,CV_RGB2GRAY);
return (out);
}
int main(int argc,char** argv)
{
CvCapture* capture = NULL;
IplImage* frame=NULL;
IplImage* gray=NULL;
IplImage* canny=NULL;
cvNamedWindow("Primary",CV_WINDOW_AUTOSIZE);
cvNamedWindow("Gray",CV_WINDOW_AUTOSIZE);
cvNamedWindow("Canny",CV_WINDOW_AUTOSIZE);
capture = cvCreateFileCapture("d:\\opencv\\test1.avi");
while (1)
{
frame=cvQueryFrame(capture);
if (!frame)
{
break;
}
cvShowImage("Primary",frame);
gray=colorchange(frame);
cvShowImage("Gray",gray);
canny = Canny(gray,120,160,3);
cvShowImage("Canny",canny);
if (cvWaitKey(33)==27)
{
break;
}
}
#if 1 /*测试*/
cvSaveImage("d:\\opencv\\experiment\\frame.jpg",frame);
cvSaveImage("d:\\opencv\\experiment\\gray.jpg",gray);
cvSaveImage("d:\\opencv\\experiment\\canny.jpg",canny);
#endif
cvReleaseCapture(&capture);
cvDestroyWindow("Primary");
cvDestroyWindow("Gray");
cvDestroyWindow("Canny");
return 0;
}
实验结果: