# How to vertically and horizontally scan an image to store a coordinates (positions) of the elements having values other than 255.

10 views (last 30 days)
kanika bhalla on 4 Jun 2021
Answered: kanika bhalla on 5 Jun 2021
I want to identify or locate the coordinates of the multi-colored blob present on the white background i.e. (255) as shown in the figure.
For this purpose, I want to scan the attached image image both horizontally and vertically. Scanning should stop and display the coordinates where it finds the pixels value other than 0. The same procedure should be continued for whole image to identify all the coordinates having values other than 255.
I have tried to code it but unable to do so. Code is attached for your reference.
[height, width, plane] = size(I);
Coordinates = zeros(height,width);
for i=1:height
for j=1:width
x=i;
y=j;
if I(i,j)~=0
Coordinate(x,y)=I(i,j)
end
end
end
I have attached the two images for your reference. Image1 is manually described multi-colored blob with rectangle. And Image2 is the originlal image.
Any help is much appreciated. Thank you!!!

Joseph Cheng on 4 Jun 2021
do you need to "scan" in a for loop or are you just looking for the positions of the colors? if you don't need to scan in a for loop the function find() will get the values for you
bwimg = rgb2gray(img);
x = linspace(-1,1,size(img,2));
y = linspace(-1,1,size(img,1));
[X,Y]=meshgrid(x,y);
bwimg(sqrt(X.^2+Y.^2)>=.1)=255;
figure,image(bwimg),colormap gray
[r,c,V]=find(bwimg~=255);
hold on
k = boundary(c,r); %just for easier plotting to show points collected
plot(c(k),r(k),'r')
kanika bhalla on 4 Jun 2021
Thank you so much @Joseph Chengfor your answer. Actually i want to scan so as to get the positions of the colored part with row and column number. So that later I will acess those rows and column easily.
I have also tried the code given by you but it is giving error. Error is described below:
Error using linspace
Too many input arguments.

kanika bhalla on 5 Jun 2021