Sobel算子的C++代码实现

@[Sobel算子的C++代码实现]

代码片.

// An highlighted block
#include"cxcore.h"
#include"highgui.h"
#include<cmath>
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cxcore.lib")

// sobel 算子
int sobel(IplImage* src, IplImage*  tar , int limit ) 
{
	int SblMask1[3][3],SblMask2[3][3];

	//水平的模板
	 SblMask1[0][0] = -1; //{-1,0,1,-2,0,2,-1,0,1};  
	 SblMask1[0][1] = -2;
	 SblMask1[0][2] = -1;
	 SblMask1[1][0] = 0;
	 SblMask1[1][1] = 0;
	 SblMask1[1][2] = 0;
	 SblMask1[2][0] = 1;
	 SblMask1[2][1] = 2;
	 SblMask1[2][2] = 1;

	 //垂直的模板
	 SblMask2[0][0] = -1;
	 SblMask2[0][1] = 0;
	 SblMask2[0][2] = 1;
	 SblMask2[1][0] = -2;
	 SblMask2[1][1] = 0;
	 SblMask2[1][2] = 2;
	 SblMask2[2][0] = -1;
	 SblMask2[2][1] = 0;
	 SblMask2[2][2] = 1;
//	}
	
	int H = src->height ; int W = src->width ; 
	int i , j , h , w , temp1 , temp2 ; 
	for ( h = 1 ; h < H - 1 ; h ++)
	{	
		for( w = 1 ; w < W - 1 ; w ++)
		{
			temp1 = temp2 = 0 ; 

			for(i = 0 ; i < 3 ; i++)
			{
				for(j = 0 ; j < 3 ; j++)
				{
					temp1 += (src->imageData+(h - 1 + i)*src->widthStep)[(w - 1 + j)] * SblMask1[i][j] ;
					temp2 += (src->imageData+(h - 1 + i)*src->widthStep)[(w - 1 + j)] * SblMask2[i][j] ;
				}
			}
			int k = sqrt(temp1*temp1 + temp2*temp2 )  ;
			if(  k > limit)
				(tar->imageData + tar->widthStep * h)[w] = (uchar)255; 
			else
				(tar->imageData + tar->widthStep * h)[w] = (uchar)0 ;
		}
	}
	
	return 0;
}

//灰度图
void getGrayImg (  IplImage *src , IplImage * tar )
{

	int w , h ;
	int H = src->height ; 

	int W = src->width ; 

	for(h = 0 ; h < H ; h++)
	{
		for(w = 0 ; w < W ; w++)
		{
			//三通道图转到单通道
			( tar->imageData + h * tar->widthStep) [w] = (unsigned char)
			(	(unsigned char)( src->imageData + h * src->widthStep )[ 3*w ]* 0.136 + 
				(unsigned char)( src->imageData + h * src->widthStep )[ 3*w+1]*0.514 +
				(unsigned char)( src->imageData + h * src->widthStep )[ 3*w+2]*0.350 ) ; 
		}
	}
}
//原图
IplImage* src  ; //声明IplImage指针
//灰度图
IplImage* gray  ;
// sobel边缘检测后生成图
IplImage* tar ;
//创建窗口
void showWindow( )
{
	cvNamedWindow( "src" , 1 ) ; //创建窗口
	cvShowImage( "src" , src ) ; //显示窗口

		
	cvNamedWindow( "gray" , 1 ) ; //创建窗口
	cvShowImage( "gray" , gray) ; //显示窗口
		
	cvNamedWindow( "tar" , 1 ) ; //创建窗口
	cvShowImage( "tar" , tar ) ; //显示窗口
}

//销毁窗口
void closeWindow()
{
	cvDestroyWindow("src"); //销毁窗口
	cvDestroyWindow("gray");
	cvDestroyWindow("tar");

	cvReleaseImage( &src ) ; //释放图像
	cvReleaseImage( &gray) ;
	cvReleaseImage( &tar ) ;
}

//图片保存
void save()
{
	cvSaveImage( "灰度图.jpg" , gray ) ;
	cvSaveImage( "边缘检测.jpg" , tar ) ;
}
int main(int argc , char** argv	)
{
	//读入文件,默认选择test3
	if( (src= cvLoadImage( argc == 2?argv[1]:"test3.bmp")) )
	{
		tar = cvCreateImage( cvSize(src->width  , src->height) , IPL_DEPTH_8U , 1) ;
		gray = cvCreateImage( cvSize(src->width , src->height) , IPL_DEPTH_8U , 1) ;

		getGrayImg ( src , gray);
		// 默认阈值150
		sobel(gray , tar , 150);

		showWindow() ;
		save() ; 
		cvWaitKey(0) ;
		closeWindow();

	}//
	
	return 0;
	return 0;
}

你可能感兴趣的:(Sobel算子的C++代码实现)