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

조회 수: 8 (최근 30일)
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.
I=imread(Image2); % Read input image
[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!!!

답변 (2개)

Joseph Cheng
Joseph Cheng 2021년 6월 4일
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
img = imread('pears.png');
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')
  댓글 수: 1
kanika bhalla
kanika bhalla 2021년 6월 4일
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.
Thank you for your time.

댓글을 달려면 로그인하십시오.


kanika bhalla
kanika bhalla 2021년 6월 5일
Sir @Walter Roberson, Could you please help me reagrding my query.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by