如何在一个子图中显示图像的放大(图上放大镜)

How to show zomming in of a image within one subplot (On-figure magnifier)

我有一张图片。我想在图像中显示放大区域。

例如,我有两个感兴趣区域 (ROI):红色区域和黄色区域。 "red zooming image" 应显示在原图上方,"yellow zooming image" 应显示在原图下方。

为了在一个子图中显示它们,我将三个图像(两个缩放图像和原始图像)组合成一个行图像。组合图像应显示在子图中。下图显示了我的期望。我的问题是这些缩放图像无法放大组合图像的第一行和最后一行。你能帮我做吗?这是我试过的

 Img = imread('peppers.png');
 Img=rgb2gray(Img);
 Img=double(Img);
 Img=imresize(Img,[256 256]);
 %%Draw rectangle
 red_rect=[100 50 20 20];
 yellow_rect=[200 100 20 20];
 %% zoom in image
 red_Img_zoomIn=Img(red_rect(2) : (red_rect(2)+red_rect(4)) , red_rect(1) : (red_rect(1)+red_rect(3)) , :);
 yellow_Img_zoomIn=Img(yellow_rect(2) : (yellow_rect(2)+yellow_rect(4)) , yellow_rect(1) : (yellow_rect(1)+yellow_rect(3)) , :);
startrow = 30;
startcol = 30;
Img_zoom1=zeros(size(Img));
red_Img_zoomIn_original=red_Img_zoomIn;
red_Img_zoomIn=imresize(red_Img_zoomIn,10);
Img_zoom1(startrow:startrow+size(red_Img_zoomIn,1)-1,startcol:startcol+size(red_Img_zoomIn,2)-1) = red_Img_zoomIn;
Img_combined=[Img_zoom1;Img;zeros(size(Img))];
 %% Adding zooming images in Img_combined-centering
figure(1);
set(gcf,'color','w');
subplot(121);imshow(Img_combined,[]);
subplot(122);imshow(red_Img_zoomIn_original,[]);

这是我的预期结果。

你的缩放问题出在插值方法上。如果您不指定,imresize 将使用双线性插值,有效地 "blurring" 您的图像。

为避免这种情况,请明确指定您想要的方法,最近邻,一种复制最近像素值的方法。请注意,还有一种方法可以告诉 imresize 您希望输出图像具有的确切行数和列数,而不仅仅是比例。

如果您将这段代码添加到您的代码中:

 red_Img_zoomIn=Img(red_rect(2) : (red_rect(2)+red_rect(4)) , red_rect(1) : (red_rect(1)+red_rect(3)) , :);
 yellow_Img_zoomIn=Img(yellow_rect(2) : (yellow_rect(2)+yellow_rect(4)) , yellow_rect(1) : (yellow_rect(1)+yellow_rect(3)) , :);

 % We need to rezise the pieces of the image if they want to be seen "big"


 % lets use nearest neighbour interpolation to make sure new pixel are just "repeated values"
red_Img_zoomOut=imresize(red_Img_zoomIn,'OutputSize',[size(Img,1) size(Img,2)],'Method','nearest');
yellow_Img_zoomOut=imresize(yellow_Img_zoomIn,'OutputSize',[size(Img,1) size(Img,2)],'Method',  'nearest');



% Combine images
 Img_combined=[red_Img_zoomOut;
      Img;
     yellow_Img_zoomOut];
 %% Adding zooming images in Img_combined-centering
 figure(1);
 set(gcf,'color','w');
imshow(Img_combined);

输出将不模糊: