opencv 学习第二天 学习opencv(中文版)对一幅图片进行canny边缘检测

#include 
#include 
#include 
using namespace std;
using namespace cv;
IplImage *dopyrDown(IplImage *in,int filter = IPL_GAUSSIAN_5x5)
{
	//assert(in->width%2 == 0 && in->height%2 == 0);//一个断言要求图像的长宽是偶数
	IplImage *out = cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
	cvPyrDown(in,out);
	return out;
}
//to split the image 
IplImage *transform(IplImage *in)
{
	IplImage *dst;
	IplImage *dst1;
	IplImage *dst2;
	IplImage *dst3;
	dst1 = cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1);//得到该图片的三个通道
	dst2 = cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1);
	dst3 = cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1);
	//dst =  cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,3);
	cvSplit(in,dst1,dst2,dst3,0);//分离RGB通道得到rgb单通道的图片
	//cvMerge(dst1,dst2,dst3,0,dst);//逆运算,合成
	return dst1;
}
//canny edge detection
IplImage *docanny(IplImage *in,double lowthresh,double highthresh,double aperture)
{
	if (in->nChannels != 1)
		return 0;//this step why?because only gray scale image can be handled by canny
	IplImage *out = cvCreateImage(cvGetSize(in),IPL_DEPTH_8U,1);
	cvCanny(in,out,lowthresh,highthresh,aperture);
	return out;
}

void main()
{
	IplImage *image = cvLoadImage("C:\\1.jpg");
	IplImage *dst = transform(image);//保存其中的一个通道
	cvNamedWindow("example4-in");
	cvNamedWindow("example4-out");
	cvShowImage("example4-in",image);
	cvShowImage("example4-pyrdown",dopyrDown(image));
	cvShowImage("example4-canny",docanny(dst,50,150,3));
	//imshow("example4-in",image);
	//Mat out = cvCreateImage(cvGetSize(&image),IPL_DEPTH_8U,3);
	IplImage *out = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
	cvSmooth(image,out,CV_GAUSSIAN,3,3,0,0);//高斯平滑
	cvShowImage("example4-out",out);
	
	cvReleaseImage(&out);
	waitKey(0);
	cvDestroyAllWindows();
	system("pause");
}
在写这个程序的时候老是在canny算法那么显示不出来,最后才发现人家这个函数要求必须是单通道的图像,对其不明白,baidu了一下ok,所以我参照

一叶障目的博客

http://blog.sina.com.cn/u/1748729840

这个人的文章修改了我的程序,ok了。

opencv 学习第二天 学习opencv(中文版)对一幅图片进行canny边缘检测_第1张图片


你可能感兴趣的:(opencv)