您的位置:首页 > 运维架构

图像处理算法基础(六)---sobel算子自实现与opencv对比

2015-12-16 09:16 1176 查看
本次实验自己实现的sobel效果还是比opencv中的Sobel函数效果差一些,opencv中应该加了一些效果优化的东西,鉴于自己实现的代码还需要继续优化,Roberts算子和canny算子等就不一一实现了。

sobel算子是一个离散的一阶差分算子,用来计算图像亮度函数的一阶梯度之近似值。在图像的任何一点使用此算子,将会产生该点对应的梯度矢量或是其法矢量。

主要是通过计算X、Y方向查分绝对值求和,公式如下





自实现代码如下:

int picProcessBasics::IMGSobel(IplImage* pImg,IplImage* pDestImg)

{

int FilterW=0;

int FilterH=0;

int FilterCenterX=0;

int FilterCenterY=0;

int aValue[64]={0};//滤波掩模

int tempV=0;

FilterW=3;

FilterH=3;

FilterCenterX=1;

FilterCenterY=1;

cvCopy(pImg,pDestImg,0);

for(int i = FilterCenterY; i < pImg->height-FilterH+FilterCenterY+1; i++){

for(int j = FilterCenterX; j < pImg->width-FilterW+FilterCenterX+1; j++){

// 读取滤波器数组

for (int k = 0; k < FilterH; k++)

{

for (int l = 0; l < FilterW; l++)

{

// 指向DIB第i - iFilterMY + k行,第j - iFilterMX + l个象素的指针

aValue[k * FilterW + l] = pImg->imageData[pImg->widthStep * (i - FilterCenterY + k) + (j - FilterCenterX + l) ];

}

}

//tempV= aValue[0]+aValue[1]+aValue[2]+aValue[3]+aValue[5]+aValue[6]+aValue[7]+aValue[8]-8*aValue[4];

tempV= abs(aValue[6]+2*aValue[7]+aValue[8] - (aValue[0]+2*aValue[1]+aValue[2]))+\

abs(aValue[2]+2*aValue[5]+aValue[8] - (aValue[0]+2*aValue[3]+aValue[6]));

if(tempV>255)

tempV=255;

else if(tempV<0)

tempV=0;

pDestImg->imageData[pDestImg->widthStep * i + j ]= tempV; //pImg->imageData[pImg->widthStep * i + j ] - tempV;

}

}

return 0;

}

效果如下:



opencv函数 Sobel( imgDest, tempTest, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT );效果如下:




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