3
votes

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.

2

2 Answers

5
votes

Use sub2ind.

pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));
-3
votes
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    
   }
}