在图像处理中,常常会遇到这样一个问题:如何将图像中某一小块使用红色边框标记,然后再将其放大放置于左下角?之前在网上查找了很多关于此类方法的解决方法,但是都失败了。最后自己编写了一个小的程序来实现改功能。其代码如下:
Img = imread('Lena.png');
Local_Zoom = ShowEnlargedPatch(Img, 241, 426, 70, 70, 2, 2);
imwrite(Local_Zoom, '.../Results/Local_Zoom.tif');
子函数ShowEnlargedPatch():
function O_img = ShowEnlargedPatch(Img, Row, Col, WindowRow, WindowCol, LineWidth, Scale)
% 函数输入说明:
% Row, Col: 感兴趣区域的左上角横纵坐标(在图像表现为行、列)
% WindowRow, WindowCol: 感兴趣区域大小
% LineWidth: 线的宽度
% Scale: 感兴趣区域放大的倍数
% 函数输出:
% O_img: 合成后的输出图像
[height width] = size(Img);
Patch(:,:) = Img(Row-LineWidth:Row+LineWidth+WindowRow, Col-LineWidth:Col+LineWidth+WindowCol);
Interpolation_Method = 'bicubic';
Enlarged = imresize(Patch, Scale, Interpolation_Method);
[m n] = size(Enlarged);
EnlargedShowStartRow = height - LineWidth;
EnlargedShowStartCol = 1 + LineWidth;
Img(EnlargedShowStartRow - m + 1:EnlargedShowStartRow,EnlargedShowStartCol:EnlargedShowStartCol + n - 1) = Enlarged(:,:);
Point = [EnlargedShowStartCol EnlargedShowStartRow-m m n];
figure, imshow(Img, 'border', 'tight')
hold on;
rectangle('Position', [Col Row WindowCol WindowRow], 'LineWidth', 2, 'EdgeColor', 'r')
rectangle('Position', Point, 'LineWidth', 2, 'EdgeColor', 'r')
frame = getframe(gcf, [0 0 512 512]);
O_img = frame2im(frame);