通常,您应该为您的imrect
. 例如:
x = imrect();
x.addNewPositionCallback( @(x)(disp('The rect has changed')))
回调应该通过使用匿名函数来获取附加参数,例如图像和第二轴。
我写了一个小代码片段,可以满足你的要求。您应该添加边界检查,因为我没有打扰。当您移动矩形时,它会更新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