MATLAB: How to store specific pixels after scanning the image with a mask

digital image processingimage processingmatrix arraypixel

Hi, I am working on Diabetic retinopathy and have to find the vessel bifurcations. In order to do so, I am scanning the entire image with 5 different masks in a loop. If any of the pixels in the image satisfy either one of the 5 conditions, I have to store the corresponding pixel position for later use.
I am unable to store the pixel positions in an array. Please help me.
This is the code I am using to scan the image with different masks.
[nrows,ncols] = size(cmp);
count = 0;
m = 1;
n = 1;
for i = 2:nrows-1
for j = 2:ncols-1
if cmp(i,j)== 0 && cmp(i,j+1)==1 && cmp(i,j-1)==1 && cmp(i-1,j)==0 && cmp(i-1,j-1)==1 && cmp(i-1,j+1)==1 && cmp(i+1,j)==1 && cmp(i+1,j-1)==0 && cmp(i+1,j+1)==0
count = count+1;
elseif cmp(i,j)== 0 && cmp(i,j+1)==1 && cmp(i,j-1)==1 && cmp(i-1,j)==1 && cmp(i-1,j-1)==0 && cmp(i-1,j+1)==0 && cmp(i+1,j)==0 && cmp(i+1,j-1)==1 && cmp(i+1,j+1)==1
count = count+1;
elseif cmp(i,j)== 0 && cmp(i,j+1)==0 && cmp(i,j-1)==1 && cmp(i-1,j)==0 && cmp(i-1,j-1)==1 && cmp(i-1,j+1)==1 && cmp(i+1,j)==1 && cmp(i+1,j-1)==0 && cmp(i+1,j+1)==1
count = count+1;
elseif cmp(i,j)== 0 && cmp(i,j+1)==1 && cmp(i,j-1)==1 && cmp(i-1,j)==1 && cmp(i-1,j-1)==0 && cmp(i-1,j+1)==0 && cmp(i+1,j)==1 && cmp(i+1,j-1)==1 && cmp(i+1,j+1)==0
count = count+1;
else cmp(i,j)== 0 && cmp(i,j+1)==1 && cmp(i,j-1)==1 && cmp(i-1,j)==1 && cmp(i-1,j-1)==0 && cmp(i-1,j+1)==0 && cmp(i+1,j)==1 && cmp(i+1,j-1)==0 && cmp(i+1,j+1)==0
count = count+1;
end
end
end

Best Answer

h = cell(1,5)
h{1} = [1 0 1;1 0 1;0 1 0];
h{2} = flipud(h{1});
h{3} = h{1}; h{3}([8,9]) = [0 1];
h{4} = h{2}; h{4}([6,9]) = [1 0];
h{5} = h{4}; h{5}(3) = 0;
[I,J] = cellfun(@(x)find(imfilter(cmp,x)==nnz(x)...
& imfilter(~cmp + 0,~x + 0)==nnz(~x)),h,'un',0);
count = numel([I{:}]);
or:
[I,J] = cellfun(@(y)find(nlfilter(cmp,[3 3], @(x)isequal(x,y))),h,'un',0);
or:
[I,J] = cellfun(@(z)find(colfilt(cmp,[3 3],'sliding',@(x,y)all(eq(x,y)),repmat(z(:),1,numel(cmp)))),h,'un',0);
ADDED
r = cat(1,I{:});
c = cat(1,J{:});