I have the x and y coordinates of a gray scale image. How to get the gray values for that particular set of coordinates?

2 views (last 30 days)
gray = rgb2gray(I5);
imtool(gray)
graydata = 0;
graydata = gray(sub2ind(size(gray)),(y(:)),(x(:)));
I tried this code.
Error in fourierdescriptorscode (line 18)
graydata = gray(sub2ind(size(gray)),round(y(:)),round(x(:))); - Command window displayed this.
Please answer as soon as possible. Thank you.

Accepted Answer

Stephen23
Stephen23 on 1 May 2015
Edited: Stephen23 on 1 May 2015
You are putting way too many superfluous parentheses in there, and this is causing the error. If you just stick to putting in the parentheses that are actually required, then the code becomes easier to read and the problem is easier to identify. So instead of (x(:)) you should just put x(:), etc., which makes it much easier to keep track of the matching parentheses.
Then you would discover that the function parentheses for sub2ind are closed just after size(gray), and do not include the x and y values at all, as they should.
To make your code clearer and reduce the chance of writing this kind of basic bug, consider allocating the indices first. Then you would have something like this:
idx = sub2ind(size(gray),y(:),x(:));
graydata = gray(idx);
which makes it easier to debug because you can also check the values in idx.
  2 Comments
pooja
pooja on 1 May 2015
Here, all my values come up to be 255. But the actual values should vary between 0 and 255. Please, tell me where am i going wrong now. Please reply as soon as possible.
Stephen23
Stephen23 on 1 May 2015
Which values? In what variable? What data are you using? How are you checking these values? What is the whole code?
You only give us part of the code and none of the data... Sorry, but reading minds is very difficult.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 1 May 2015
Edited: Image Analyst on 1 May 2015
gray() is a built in function. Don't use that name.
Also, you can simply just use a for loop to get the values. No need for linear indexing and sub2ind(). I think the for loop is more intuitive and it's very fast for a small number of coordinates.
xr = round(x);
yr = round(y);
for k = 1 : length(x)
pixelValues(k) = grayImage(yr(k), xr(k));
end
  2 Comments
pooja
pooja on 5 May 2015
Edited: Image Analyst on 5 May 2015
How to understand whether it should be like-
pixelValues(k) = grayImage(yr(k), xr(k));
or
pixelValues(k) = grayImage(xr(k), yr(k));
I had got xr and yr in the following manner:-
[x,y] = find(I5 == 0);
xr = round(x);
yr = round(y);
Please reply as soon as possible. Thanking you.
Image Analyst
Image Analyst on 5 May 2015
Arrays are always (row, column), never (x, y). Images are just arrays, so you need to use row, column, which is y,x:
pixelValues(k) = grayImage(yr(k), xr(k));
like I showed you. Be careful about this. The x,y/row,column mixup is extremely common amongst MATLAB programmers.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!