How to represent the value of X and Y Pixels in array of Two Dimensions using Matlab
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi , Suppose if this is my image

I need to find the x, y coordinates of white pixels in image. I am getting the value of x and y coordinates separately one after the other, i want to represent it in 2 dimensional array matrix like [x,y] ,how can i represent it in 2 D matrix?
Then i need to calculate the farthest distance in that box ? how to solve this?
Accepted Answer
Youssef Khmou
on 15 Sep 2014
The provided answer is correct, you only need some processing steps, the image contains white borders , you need to crop it using some software, after that, you need to convert into grayscale and then to double instead of unit8 :
X=rgb2gray(X);
X=im2double(X);
% then the command provided above.
12 Comments
Lakshya
on 16 Sep 2014
Thank You Youssef khmou ,
Actually this image i have converted to gray then applied threshold , then edge detection, this image is the output after edge detection .
Using 'find' i am getting x and y coordinates , how to represent it in matrix??
Not a square matrix but a very thin matrix :
[y, x] = find(binaryImage);
M=[y,x];
Thank you so much....
Now i need to calculate the distance between those coordinates , this can be done by using distance formula. But i need to compare each coordinate with all other coordinate in matrix, then find the farthest distance between them.
like [x1,y1] with [x2,y2],, in next iteration [x1,y1] with [x3,y3] and so on till [xn,yn].
Similarly [x2,y2] with all other coordinates in matrix.
Suppose the matrix you obtained is M :
N=length(M);
The euclidean distances form a matrix of dimensions NxN where the diagonal elements are 0, you can basically use two dimensional loop :
D=zeros(N);
for x=1:N
for y=1:N
D(x,y)=sqrt((M(x,1)-M(y,1))^2-(M(x,2)-M(y,2))^2);
end
end
Its not working out...
this is my program where a(i,j) is my image
count=1;
for(i=1:x)
for(j=1:y)
if a(i,j)~=1;
x(count)=j;
y(count)=i;
x(1,:)= x(count);
y(:,1)= y(count);
count=count+1;
a(i,j) = sqrt((y(count)-y(:,1))^2 + (x(count)-x(1,:))^2);
end;
end;
end;
[x,y]=find(a);
xy=[x,y]
My problem is i want to store the value of x(count) and y(count) in some temporary variable. How to assign the value ???
here i have used x(1:,) and y(:,1)
but i am getting error
"Index exceeds matrix dimensions "
Youssef Khmou
on 16 Sep 2014
I tried the the loop i proposed and it is working, try to verify your program .
I tried out your program..
i am not getting any answer...
and i have a doubt... why ur using 'zeros', coz i need to find pixel values with ones... and my image is not a square matrix.. its MxN
Youssef Khmou
on 16 Sep 2014
Edited: Youssef Khmou
on 16 Sep 2014
earlier after obtaining the matrix M of coordinates of white pixels, you wanted to calculate the distance between each point M(x,y) and the rest of the points, the two dimensional loop works,zeros is a variable initialization :
for x=1:N
for y=1:N
D(x,y)=sqrt((M(x,1)-M(y,1))^2-(M(x,2)-M(y,2))^2);
end
end
The diagonal is zero because it is a distance between a point M(x,y) and itself.
Lakshya
on 16 Sep 2014
ok, but i am not getting any result...
can you provide me your code.....
Lakshya
on 16 Sep 2014
And in my code , is this statement correct....??
x(1,:)= x(count);
y(:,1)= y(count);
actually i am getting error at this line...
Youssef Khmou
on 16 Sep 2014
x(1,:) means the x,y coordinates of the 1st point, but y(:,1) is the x coordinates of all points, that is where you get the error, the distance between the x and y points is sqrt((M(x,1)-M(y,1))^2-(M(x,2)-M(y,2))^2)
Lakshya
on 17 Sep 2014
k attach me your complete code ,,, ill just try
More Answers (0)
Categories
Find more on Computational Geometry in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)