图像处理算法基础(五)---拉普拉斯变换自实现与opencv对比

来源:互联网 发布:dokuwiki nginx 编辑:程序博客网 时间:2024/06/06 07:08

最近做了几个平滑锐化的程序效果都是比opencv差一些,猜测opencv应该加了一些效果优化。

拉普拉斯算子是n维欧几里德空间中的一个二阶微分算子,定义为梯度(▽f)的散度(▽·f)。因此如果f是二阶可微的实函数,则f的拉普拉斯算子定义为:
f的拉普拉斯算子也是笛卡儿坐标系xi中的所有非混合二阶偏导数:
作为一个二阶微分算子,拉普拉斯算子把C函数映射到C函数,对于k≥ 2。表达式(1)(或(2))定义了一个算子Δ :C(R) →C(R),或更一般地,定义了一个算子Δ :C(Ω) →C(Ω),对于任何开集Ω。
函数的拉普拉斯算子也是该函数的黑塞矩阵的迹
另外, 满足▽·▽f=0 的函数f, 称为调和函数.

在二维空间坐标系中xy代表 x-y 平面上的笛卡儿坐标:

在数字图像中离散表达式:△f(x,y)=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4*f(x,y)

代码实现为:
int  picProcessBasics::IMGLaplace(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= aValue[1]+aValue[3]+aValue[5]+aValue[7]-4*aValue[4];

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函数 void cvLaplace( const CvArr* src, CvArr* dst, int aperture_size=3 );  效果图:



0 0
原创粉丝点击