I have the x and y coordinates of a gray scale image. How to get the gray values for that particular set of coordinates?
Show older comments
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
More Answers (1)
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
on 5 May 2015
Edited: Image Analyst
on 5 May 2015
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.
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!