function [pts, colors] = imgPointSample(img, r)
%从图像中按输入点采样
%采样大小数据:(2 * r + 1) * (2 * r + 1) * 图像色彩通道数
%采样结果:像素位置存放在pts中,像素位置及区域像素值存放在vals中
imagesc(img);
[h w c] = size(img);
hold on;
pts = [];
colors = {};
while 1
[y, x, btn] = ginput(1);
x = round(x) + 1;
y = round(y) + 1;
if (x < 1 || x > h)
continue;
end;
if (y < 1 || y > w)
continue;
end;
if (btn == 3)
break;
elseif (btn == 1)
pt = [x y];
pts = [pts; pt];
pixels = img((x-r):(x+r), (y-r):(y+r), :)
colors{end + 1}{1} = pt;
colors{end + 1}{2} = pixels;
plot(pts(:, 2), pts(:, 1), 'or', 'MarkerSize', 2 * r + 1);
end;
end;
hold off;
end
用法:
img = imread('x.bmp');
[pts, vals] = imgPointSample(img, 3);