MATLAB中灰度图像的像素值

3

假设我有一张RGB图像rgb和一组空间坐标coords。我希望提取空间坐标处的像素值,例如[x1 y1][x2 y2][x3 y3]。对于RGB图像,我可以使用以下代码实现:

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

该函数返回指定图像像素的红色、绿色和蓝色值。

impixel 仅适用于彩色(RGB)图像。但是我想从灰度图像I中提取像素值。可以使用以下for循环来实现。

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

我希望避免使用for循环。有没有其他方法可以实现这个功能? imstats = regionprops(mask, I,'PixelValues');也可以工作,但首先需要一个图像mask
2个回答

5
使用 sub2ind 函数。
pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));

-3
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    
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接