一般に、コールバックを に割り当てる必要がありますimrect
。例えば:
x = imrect();
x.addNewPositionCallback( @(x)(disp('The rect has changed')))
コールバックは、匿名関数を利用して、画像や 2 番目の軸などの追加パラメーターを取得する必要があります。
あなたが望むことをする小さなコードスニペットを書きました。私は気にしなかったので、境界チェックを追加する必要があります。CData
四角形を移動すると実行されるのではなく更新さimshow
れるため、非常にスムーズです。

function Zoomer
figure();
highResImage = imread('peppers.png');
lowResImage = imresize(highResImage,0.5);
a1 = subplot(2,1,1);
a2 = subplot(2,1,2);
imshow(lowResImage,'Parent',a1);
initialPosition = [10 10 100 100];
lowResRect = imrect(a1,initialPosition);
lowResRect.addNewPositionCallback( @(pos)Callback(pos,a2,highResImage));
Callback( initialPosition , a2, highResImage);
end
function Callback(position,axesHandle, highResImage)
position = position * 2;
x1 = position(1);
y1 = position(2);
x2 = position(1) + position(3);
y2 = position(2) + position(4);
highResThumbnail = highResImage( round(y1:y2),round(x1:x2),:);
if isempty( get(axesHandle,'Children'))
imshow(highResThumbnail,'Parent',axesHandle);
else
imHandle = get(axesHandle,'Children');
oldSize = size(get(imHandle,'CData'));
if ~isequal(oldSize, size(highResThumbnail))
imshow(highResThumbnail,'Parent',axesHandle);
else
set( imHandle,'CData', highResThumbnail);
end
end
end