MATLAB: How to detect first 50 white pixels from right side of a binary image

image processing

i have a binary image and for extracting the REGIIN OF INTEREST we need to detect the first 50 white pixels from right side of the image. can anyone suggest me how to do it?

Best Answer

You can use find to get the indices from the left, so the only thing you need to do is flipping the image and convert the col indices:
A=[1 0 0;0 1 0;1 1 1];
N_pixels=3;
A=fliplr(A);%flip since find looks from the upper left corner
[r,c]=find(A,N_pixels);
c=size(A,2)-c+1;%flip indices back
%prove this is indeed correct:
B=accumarray([r,c],ones(size(r)),size(A))
%this may be faster depending on the size of your matrix and number of points:
%B=zeros(size(A));ind=sub2ind(size(B),r,c);B(ind)=1