您的位置:首页 > 编程语言 > MATLAB

Matlab中del2()函数学习笔记

2011-03-16 11:40 381 查看
在L=del2(U)表达式中,Matlab帮助文件的解释是:li,j=(ui+1,j+ui-1,j+ui,j+1+ui,j-1)/4 - ui,j

但帮助文件没有解释边缘点如何处理。根据数学中的原理,在程序中做适当变化处理:

1. 数组左上角角点的公式应为:li,j=(ui+2,j+ui+2,j+2ui,j)/4 - (ui+1,j+ui,j+1)/2

2. 数组左边边点公式应为:li,j=(ui+1,j+ui-1,j+ui,j+2+ui,j)/4 - (ui,j+ui,j+1)/2

依这两个公式类推,可以得到其他角点和其他边点的计算公式,从而得到计算结果。用C编程实现del2时需要注意这些点的计算。

在OpenCV中,cvLaplace的功能能够实现del2。但不知道为什么使用的时候会出现问题,通不过。所以自己写了一个cvDel2。可能是画蛇添足,不过自己写的能用就可以了,写起来也不是太麻烦。

void cvDel2(IplImage *src,IplImage *dst)
{
for(i=0;i<src->height;i++)
{
for(j=0;j<src->width;j++)
{
if(i==0 && j==0)
{
cvSetReal1D(dst,i*src->width+j,(2*cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i+2)*src->width+j)+cvGetReal1D(src,i*src->width+j+2))/4-(cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,i*src->width+j+1))/2);
}
else if(i==0 && j!=0 && j!=src->width-1)
{
cvSetReal1D(dst,i*src->width+j,(cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i+2)*src->width+j)+cvGetReal1D(src,i*src->width+j+1)+cvGetReal1D(src,i*src->width+j-1))/4-(cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,i*src->width+j))/2);
}
else if(i==0 && j==src->width-1)
{
cvSetReal1D(dst,i*src->width+j,(2*cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i+2)*src->width+j)+cvGetReal1D(src,i*src->width+j-2))/4-(cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,i*src->width+j-1))/2);
}
else if(i!=0 && i!=src->height-1 && j==src->width-1)
{
cvSetReal1D(dst,i*src->width+j,(cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,i*src->width+j-2)+cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,(i-1)*src->width+j))/4-(cvGetReal1D(src,i*src->width+j-1)+cvGetReal1D(src,i*src->width+j))/2);
}
else if(i==src->height-1 && j==src->width-1)
{
cvSetReal1D(dst,i*src->width+j,(2*cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i-2)*src->width+j)+cvGetReal1D(src,i*src->width+j-2))/4-(cvGetReal1D(src,(i-1)*src->width+j)+cvGetReal1D(src,i*src->width+j-1))/2);
}
else if(i==src->height-1 && j!=0 && j!=src->width-1)
{
cvSetReal1D(dst,i*src->width+j,(cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i-2)*src->width+j)+cvGetReal1D(src,i*src->width+j+1)+cvGetReal1D(src,i*src->width+j-1))/4-(cvGetReal1D(src,(i-1)*src->width+j)+cvGetReal1D(src,i*src->width+j))/2);
}
else if(i==src->height-1 && j==0)
{
cvSetReal1D(dst,i*src->width+j,(2*cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,(i-2)*src->width+j)+cvGetReal1D(src,i*src->width+j+2))/4-(cvGetReal1D(src,(i-1)*src->width+j)+cvGetReal1D(src,i*src->width+j+1))/2);
}
else if(i!=src->height-1 && i!=0 && j==0)
{
cvSetReal1D(dst,i*src->width+j,(cvGetReal1D(src,(i-1)*src->width+j)+cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,i*src->width+j+2))/4-(cvGetReal1D(src,i*src->width+j)+cvGetReal1D(src,i*src->width+j+1))/2);
}
else
{
cvSetReal1D(dst,i*src->width+j,(cvGetReal1D(src,(i-1)*src->width+j)+cvGetReal1D(src,(i+1)*src->width+j)+cvGetReal1D(src,i*src->width+j-1)+cvGetReal1D(src,i*src->width+j+1))/4-cvGetReal1D(src,i*src->width+j));
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: