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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
pooja
am 1 Mai 2015
Kommentiert: Image Analyst
am 5 Mai 2015
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 Kommentare
Akzeptierte Antwort
Stephen23
am 1 Mai 2015
Bearbeitet: Stephen23
am 1 Mai 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 Kommentare
Stephen23
am 1 Mai 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.
Weitere Antworten (1)
Image Analyst
am 1 Mai 2015
Bearbeitet: Image Analyst
am 1 Mai 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 Kommentare
Image Analyst
am 5 Mai 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.
Siehe auch
Kategorien
Mehr zu Filter Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!