MATLAB pixel values ​​in grayscale images

Say I have an RGB image rgband a list of spatial coordinates coords. I would like to retrieve the values of pixels in the spatial coordinates, for example, [x1 y1], [x2 y2]and [x3 y3]. For RGB images, I can do this using:

rgb = imread('sample.jpg')
coords = [x1 y1; x2 y2; x3 y3];
pixelData = impixel(rgb, coords(:,1), coords(:,2));

Returns the red, green, and blue values ​​of the specified pixels in the image.

impixelonly works for color (RGB) images. But I want to extract the pixel values ​​from the grayscale image I. I can do this using a loop forlike this

for i = 1:size(coords,1)
    pixelData(i,:) = I(coords(i,2), coords(i,1));
end

I would like to avoid using loops for. Is there any other way to do this?

imstats = regionprops(mask, I,'PixelValues');also works, but first I need an image mask.

+3
2

sub2ind.

pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));
+5
void Image::createImage(int width_x, int height_y)
{
   pixelData = new Color* [width];  // array of Pixel*

   for (int x = 0; x < width; x++) 
   {
       pixelData[x] = new Color [height];  // this is 2nd dimension of pixelData    
   }
}
-3

All Articles