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

조회 수: 1 (최근 30일)
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.

채택된 답변

Stephen23
Stephen23 2015년 5월 1일
편집: Stephen23 2015년 5월 1일
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
pooja
pooja 2015년 5월 1일
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 2015년 5월 1일
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.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 5월 1일
편집: Image Analyst 2015년 5월 1일
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
pooja
pooja 2015년 5월 5일
편집: Image Analyst 2015년 5월 5일
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 2015년 5월 5일
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by