比如说,我有一个 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
。
最佳答案
使用sub2ind
.
pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));
关于灰度图像中的 MATLAB 像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757802/