使用Matlab计算图像像素的条件概率

时间:2018-08-09 18:24:17

标签: image matlab probability entropy

我已经编写了这段代码来计算图像的概率和稍后的熵:

main.m

% 512x512 image
x = imread('IMAGE.png');
x = double(x);
% H(Xij)
p = probIm(x);
entropy = entropyCalc(p)

probIm.m

function p = probIm(x)

[n m] = size(x); 

% Minimum to maximum value of the image [0-255]
range = (min(min(x)):max(max(x))); 
N = length(range); 

% How many times does x take value xi
p = histogram(x,N); 
p = p.BinCounts; 
% Normalize p so sum(p)=1
p = p./(n*m); 

entropyCalc.m

function y = entropyCalc(P)
P(P==0) = [];
y = -sum(P.*log2(P));

它工作正常。现在,我需要为同一张图像计算对熵H(Xij,Xi+1j)和条件熵H(Xi+1,j|Xi,j)。我不知道如何解决这个问题。我将这些公式用于对熵:H(X,Y) = -sum(Pxy(a,b)*log(Pxy(a,b))),其中Pxy(a,b) = Px(a)*Pyx(b|a)意味着我必须计算两个图像之间的条件概率,这就是我被困住的地方。任何建议,将不胜感激。

我以这种方式开始获取Xi+1j,并尝试了一些计算,但是结果很糟糕

% xrow = Xi+1,j
xrow = x;
xrow(:,2:512) = x(:,1:511);
xrow(:,1) = 128; % Pixels outside the edges = 128 (grey)

0 个答案:

没有答案