OpenCV学习笔记_图片边缘检测

#include 
#include 

using namespace std;


IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
	if (in->nChannels != 1)
		return(0); // Canny only handles gray scale images
	IplImage* out = cvCreateImage(cvGetSize(in), in->depth, 1);
	//图像的边缘检测.lowThresh 第一个阈值,highThresh 第二个阈值,aperture 算子内核大小
	cvCanny(in, out, lowThresh, highThresh, aperture);
	return(out);
};

int main(int argc, char** argv)
{
	IplImage* img_rgb = cvLoadImage("D:\\15.jpg");
	IplImage* img_gry = cvCreateImage(cvSize(img_rgb->width, img_rgb->height), img_rgb->depth, 1);
	//将BGR图转成灰度图
	cvCvtColor(img_rgb, img_gry, CV_BGR2GRAY);
	cvShowImage("Example Gray", img_gry);
	IplImage* smooth = cvCreateImage(cvGetSize(img_rgb), img_rgb->depth, 1);
	//在进行图片边缘检测前,应先对图片进行模糊处理
	cvSmooth(img_gry, smooth, CV_GAUSSIAN, 3, 3);
	//进行图片边缘检测
	IplImage* img_cny = doCanny(smooth, 10, 100, 3);
	cvShowImage("Example Canny", img_cny);
	cvWaitKey(0);
	cvReleaseImage(&img_rgb);
	cvReleaseImage(&img_gry);
	cvReleaseImage(&smooth);
	cvReleaseImage(&img_cny);
	cvDestroyWindow("Example Gray");
	cvDestroyWindow("Example Canny");
}

你可能感兴趣的:(OpenCV学习)