MATLAB: Comparing 3×3 block with center pixel

matrix computation

i have a matrix as shown below
Untitled.png
i wanted to take 3×3 pixel and take 3 pixels at a time along with the center pixel,
and compare the selected 3 pixel value with the center pixel,
and if 2 or morepixel has value greater than the center pixel and i wanted to assign 1 to it else 0
So in this case i will get 0-1-1-0 and then convert the binary 0110 to its corresponding decimal value = 6
Then the next 3×3 pixel
Untitled1.png

Best Answer

Here, X is the input matrix.
result=0;
result=result+1*rot90(fkernel(rot90(X,+1)),-1);
result=result+2*rot90(fkernel(rot90(X,+2)),-2);
result=result+4*fkernel(X);
result=result+8*rot90(fkernel(rot90(X,-1)),+1);
function Y=fkernel(X)
[m,n]=size(X);
Y=nan(m-2,n-2);
for i=1:n-2
B=X(:,i:i+2);
Y(:,i)=sum(B(1:m-2,:)>=B(2:m-1,2),2)>=2;
end
end
Related Question