假设我有一张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
。