您的位置:首页 > 其它

Sobel,LoG,Canny边缘提取算法的实现和比较

2013-02-13 18:36 447 查看
#include "stdafx.h"
#include "cv_LoG.h"

#include "highgui.h"
#include "cv.h"

/*
opencv_core231d.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib

e:\\a_square.jpg
*/
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if(argc<2)
{
printf("Input Wrong!\n");
return 0;
}
IplImage *img;
img=cvLoadImage(argv[1],0);

IplImage *imgLoG,*img16S,*imgSobel,*imgSobelx,*imgSobely,*imgCanny;
CvScalar mean;
img16S=cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_16S,img->nChannels);

imgLoG=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
cvSmooth(img,img,CV_GAUSSIAN,3,3);	//高斯模糊
cvLaplace(img,img16S,3);			//拉普拉斯算子
cvConvertScaleAbs(img16S,imgLoG,1);
//mean=cvAvg(imgLoG);
//cvThreshold(imgLoG,imgLoG,mean.val[0],256,CV_THRESH_BINARY);

imgSobel=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
imgSobelx=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
imgSobely=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
cvSobel(img,img16S,1,0,3);
cvConvertScaleAbs(img16S,imgSobelx,1);
cvSobel(img,img16S,0,1,3);
cvConvertScaleAbs(img16S,imgSobely,1);
cvAdd(imgSobelx,imgSobely,imgSobel);	//使用两个方向的梯度绝对值和近似梯度模
//mean=cvAvg(imgSobel);
//cvThreshold(imgSobel,imgSobel,mean.val[0],256,CV_THRESH_BINARY);

imgCanny=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
cvCanny(img,imgCanny,150,50);

cvSaveImage("E:\\EdgeImages\\img.jpg",img);
cvSaveImage("E:\\EdgeImages\\imgLoG.jpg",imgLoG);
cvSaveImage("E:\\EdgeImages\\imgSobel.jpg",imgSobel);
cvSaveImage("E:\\EdgeImages\\imgCanny.jpg",imgCanny);

cvNamedWindow("Original Image");
cvShowImage("Original Image",img);
cvNamedWindow("LoG Image");
cvShowImage("LoG Image",imgLoG);
cvNamedWindow("Sobel Image");
cvShowImage("Sobel Image",imgSobel);
cvNamedWindow("Canny Image");
cvShowImage("Canny Image",imgCanny);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&imgLoG);
cvReleaseImage(&imgSobel);
cvReleaseImage(&imgCanny);
cvReleaseImage(&imgSobelx);
cvReleaseImage(&imgSobely);
cvReleaseImage(&img16S);
return 1;
return nRetCode;
}

结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: