MATLAB pixel values in gray scale images
image-processing, matlab
Solution
Use `sub2ind`.
pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));
Problem
Say, I have an RGB image `rgb` and a list of spatial coordinates `coords`. I would like to extract pixel values at spatial coordinates, for example, `[x1 y1]`, `[x2 y2]`, and `[x3 y3]`. For RGB images I can do so using: ``` rgb = imread('sample.jpg') coords = [x1 y1; x2 y2; x3 y3]; pixelData = impixel(rgb, coords(:,1), coords(:,2)); ``` Which returns red, green, and blue color values of specified image pixels. `impixel` only works for color (RGB) images. But I want to extract the pixel values from a grayscale image `I`. I can do so using a `for` loop as follows ``` for i = 1:size(coords,1) pixelData(i,:) = I(coords(i,2), coords(i,1)); end ``` I would like to avoid using `for` loops. Is there another way to accomplish this? `imstats = regionprops(mask, I,'PixelValues');` also works, but I would need an image `mask` first.