图像处理算法基础(六)---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 );效果如下:
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 );效果如下:
相关文章推荐
- OperationDemo2
- shm_open 与 open
- time opt
- linux 远程ssh链接介绍
- 项目讨论:本地手机经销商如何用移动网站开展本地营销?
- While reading /Users/cola/Desktop/csun first 2/Res/Quasar/Quasar_Tex_Up.png pngcrush caught libpng
- Apache 安全设置(持续记录)
- kafka清空topics
- Hadoop Kernel tunning
- Apache ab 压力测试出现 Failed requests
- 期中架构心得
- linux下rpm安装MySQL
- OpenGL ES 学习教程(九) 油腻的效果 Lighting Maps 高光贴图
- Linux防火墙(Iptables)的开启与关闭
- 关于OpenCart的一个小建议
- 关于开源项目中 Apache 开源协议的一些介绍
- linux下的setenv使用
- 马哥linux学习笔记:locate,find命令总结
- TOMCat 解析shtml(配置tomcat让shtml嵌套文件显示)
- errors collection when study hadoop(1)