灰度图像中的MATLAB像素值

Ale*_*xey 3 matlab image-processing

说,我有一个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));
Run Code Online (Sandbox Code Playgroud)

返回指定图像像素的红色,绿色和蓝色值.

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

for i = 1:size(coords,1)
    pixelData(i,:) = I(coords(i,2), coords(i,1));
end
Run Code Online (Sandbox Code Playgroud)

我想避免使用for循环.还有另一种方法来实现这一目标吗?

imstats = regionprops(mask, I,'PixelValues');也有效,但我首先需要一个图像mask.

Oli*_*rth 5

使用sub2ind.

pixelData = I(sub2ind(size(I), coords(:,2), coords(:,1)));
Run Code Online (Sandbox Code Playgroud)