Restricting getrect boundaries (image processing)

1 回表示 (過去 30 日間)
Sordin
Sordin 2018 年 1 月 15 日
編集済み: Matt J 2018 年 1 月 15 日
When using the getrect function, is there any way to restrict the selected rectangle so that it cannot be dragged outside the borders of the image?
Normally it is draggable outside the image frame:
In most situations, being even slightly outside the image frame leads to errors. To fix this I used several if statements to ensure we enclose the appropriate pixels:
X=imread('office_5.jpg');
imshow(X)
rect = getrect; % [x, y, width, height]
x1 =rect(1);
x2 = x1 + rect(3);
y1 =rect(2);
y2 = y1 + rect(4);
%--------------------------------------------------
% Selected rectangle completely outside the image
if ((x1 > size(X,2)) || (x2 < 0) || (y1 > size(X,1)) || (y2 < 0))
error('No pixels selected.');
rect = [];
end
% Leftmost edge outside the image
if (x1 < 1)
x1 = 1;
end
% Rightmost edge outside the image
if (x2 > size(X,2))
x2=size(X,2);
end
% Lower edge outside the image
if y1 < 1
y1 = 1;
end
% Upper edge outside the image
if y2 > size(X,1)
y2 = size(X,1);
end
But I think there must be a simpler way to do this.
Any suggestions would be greatly appreciated.

採用された回答

Matt J
Matt J 2018 年 1 月 15 日
編集済み: Matt J 2018 年 1 月 15 日
It would be better to use imrect() instead in conjunction with makeConstrainToRectFcn,
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(imrect(),fcn);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by