MATLAB: Hi, I am a beginner in MATLAB and wanted to count the number of neighbors with value 1 for each pixel which included value one in binary image… The target is to point out the pixels which are included value 1and their neighbor with 1 is >2

binary imagefinding the intersection in the vessle

A= [ 0 0 0 0; 0 1 1 0 ; 0 0 1 0;0 0 0 0 ];
% BW= imread('19-test-bin.tif');
% CC = bwconncomp(BW,4)
%fh=fopen('result1.txt', 'a+');
nrow=3;
ncol=3;
n=0;
i=2; j=2;
for i = 1:nrow
for j = 1:ncol
fprintf('Here1!!!\n')
if (A(i,j) == 1) && ( i-1>0) && (j-1>0)
fprintf('Here2!!!!\n')
if A(i-1,j-1)==1
n=n+1;
if A(i-1,j)==1;
n=n+1;
fprintf('nei(1)= %d\n',n)
if A(i-1,j+1)==1;
n=n+1;
fprintf('Here4!!!\n')
if A(i,j-1)==1;
n = n+1;
if A(i,j+1)==1;
n= n+1;
if A(i+1,j-1)==1;
n=n+1;
if A(i+1,j)==1;
n=n+1;
if A(i+1,j+1)==1;
n=n+1;
else
fprintf('There is not any neighbor!!!!\n')
end
end
end
end
end
end
end
end
end
end
end
fprintf( '%d ',n );

Best Answer

The easiest way to count neighbouring pixels is to conv2() with [1 1 1; 1 0 1; 1 1 1] .
Related Question