Вот окончательный код, который я в конце концов написал.Результаты выглядят правильно.
% aa is the image
oil = (aa(:,:,3)==255);
rock =(aa(:,:,2)==179);
gas =(aa(:,:,2)==255);
water =(aa(:,:,2)==0 && aa(:,:,3)==0);
phases(1,:,:)=RockBW;
phases(2,:,:)=WaterBW;
phases(3,:,:)=OilBW;
phases(4,:,:)=GasBW;
outMat = ContactMatrix(phases);
Функция ContactMatrix показана следующим образом:
function [ContactMat] = ContactMatrix(phases)
%CONTACTMATRIX measure the contact area between diferent phases
% The output is a 2D matrix which shows the lengths
% Phase1, Phase2, Phase 3
%phase1 L1 L2 L4
%Phase2 L2 L3 L5
%Phase3 L4 L5 L6
% L1 is zero
% L2 is the contact area of Phase1 and Phase2
nph = size(phases,1);
imSize = size(phases(1,:,:));
xmax =imSize(2);
ymax = imSize(3);
% Idealy we need a check for all sizes :)
ContactMat = zeros(nph);
for i=1:1:nph
counts = zeros(1,nph);
dd2=bwmorph(squeeze(phases(i,:,:)),'dilate',1);
B = bwboundaries(dd2);
nB = size(B,1);
coefs=1:1:nph;
coefs(i)=[];
for j=1:1:nB
fd = B{j};
% Ignore the points at boundary of image
fd(fd(:,1)==1 | fd(:,1)==xmax | fd(:,2)==1 | fd(:,2)==ymax,:)=[];
nL = size(fd,1);
for k=1:1:nL(1)
%bufCheck=false(nph-1,1);
mat=fd(k,1) + (fd(k,2)-1)*xmax;
bufCheck = phases(coefs,mat);
counts(coefs)=counts(coefs)+bufCheck';
end
end
ContactMat(i,coefs)=counts(coefs);
end
ContactMat = 0.5*(ContactMat+ContactMat');
end