您可以使用getrect用鼠標選擇矩形,然後將選定的座標轉換爲roipoly輸入格式。
我用函數insertShape來繪製一個紅色的矩形。
(如果您無法訪問請求的工具箱,則可以使用其他方法)。
我使用的是BW = roipoly(I, c, r)
版本,它的圖像大小爲圖像I
。
這裏是我的代碼:
%Read input image.
I = imread('autumn.tif');
h = figure;
imshow(I);
%Specify rectangle with mouse
rect = getrect(h);
%Draw red rectangle (replace pixels data with red color).
J = insertShape(I, 'Rectangle', rect, 'Color', [220, 0, 0], 'LineWidth', 3);
imshow(J);
%Compute rows and columns parameters from selected rect, to expected format of roipoly:
% (x0,y0) is top left corner, and (x1,y1) is bottom right corner.
x0 = rect(1);
y0 = rect(2);
x1 = x0 + rect(3) - 1; %rect(3) is width of rectangle.
y1 = y0 + rect(4) - 1; %rect(4) is height of rectangle.
%Imagine drawing lines from (x0,y0) to (x1,y0) to (x1,y1) to (x0,y1), [and back to (x0,y0), which closes the polygon]
c = [x0, x1, x1, x0];
r = [y0, y0, y1, y1];
BW = roipoly(I, c, r);
figure;imshow(BW);
結果(選擇矩形後):
![enter image description here](https://i.stack.imgur.com/SKtzU.png)
![enter image description here](https://i.stack.imgur.com/AAyIw.png)
非常感謝!!!! – Andre