how to store specific pixels after scanning the image with a mask

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

 Accepted 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{:});

2 Comments

Thank you for helping me out. The first part of the code is working fine. It's actually reducing the computation time but after I run the second part as in from [I J], I don't understand what's happening as the pixel positions are supposed to be stored in an array but I don't find anything being stored in the array.
I had used this code to store the pixel positions in an array but they weren't getting stored.
[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;
r(m) = i;
c(n) = j;
m = m+1;
n = n+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;
r(m) = i;
c(n) = j;
m = m+1;
n = n+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;
r(m) = i;
c(n) = j;
m = m+1;
n = n+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;
r(m) = i;
c(n) = j;
m = m+1;
n = n+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;
r(m) = i;
c(n) = j;
m = m+1;
n = n+1;
end;
end;
end;
Please help me.

Sign in to comment.

More Answers (1)

Why not just use the 'branchpoints' option of bwmorph() and replace all that code with just one simple call to bwmorph()?

1 Comment

I am using R2008b version of Matlab. I cannot find the branch points option of bwmorph(). If you could just tell me how to store the pixel position when it matches with the mask, it would be very helpful.
I tried to scan the whole image with the different masks and then store the pixel position in two arrays r and c for the row number and column number respectively.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!