今天在看局部熵方面的内容,看论文中介绍的内容感觉局部熵挺容易了,于是就有了实现的想法,结果效果非常糟糕。
得到的几乎是一张空白的图片,就像下面一样:
究其原因是各种论文上都写了这样一个公式:
这里f(i,j)就是在m*n这个局部的像素,这个没问题,不过这里的p是什么东西,按这里的定义p是当前像素灰度占局部总灰度的概率,而p原本应该是局部直方图,也就是当前像素灰度个数占局部像素总个数的概率,所以这里的意义我也不明白了,结果按公式计算就得到了上图,和各种论文中的图都不一样。
而真正的公式应该是这样:
熵的公式本来不就是这样么,这里的p是归一化后的局部直方图。
这样运行的结果就正确了,和matlab系统提供的entropyfilt函数结果是一样的。
自己函数处理的
系统函数处理的
matlab代码如下:
clear all; close all; clc; img=imread('lena.jpg'); [m n]=size(img); w=3; %模板半径 imgn=zeros(m,n); for i=1+w:m-w for j=1+w:n-w Hist=zeros(1,256); for p=i-w:i+w for q=j-w:j+w Hist(img(p,q)+1)=Hist(img(p,q)+1)+1; %统计局部直方图 end end Hist=Hist/sum(Hist); for k=1:256 if Hist(k)~=0 imgn(i,j)=imgn(i,j)+Hist(k)*log(1/Hist(k)); %局部熵 end end %{ p=sum(sum(img(i-w:i+2,j-w:j+w))); %这里是按第一个公式写的 s=img(i-w:i+w,j-w:j+w)/p; imgn(i,j)=-sum(sum(s.*log(s))); %} end end imshow(imgn,[]) imgn=entropyfilt(img); %系统的局部熵函数 figure; imshow(imgn,[])