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.
댓글 수: 0
채택된 답변
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
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
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!