matlab在图像上画矩形框并保存

clc,close all;
file_path='pictures/';
image_name1='t1.jpg';

ref = imread(strcat(file_path,image_name1));

[rows,cols,depth] = size(ref);
figure;
set (gcf,'Position',[100,100,cols,rows]);
imshow(ref,'border','tight','initialmagnification','fit');

x1=50;y1=50;width1=150;height1=150;
x2=450;y2=100;width2=150;height2=150;
x3=350;y3=350;width3=150;height3=150;

hold on
rectangle('Position',[x1,y1,width1,height1],'LineWidth',2,'EdgeColor','r');
text(x1+width1-15,y1+height1-10,'I','Color', 'y','fontsize',15);
rectangle('Position',[x2,y2,width2,height2],'LineWidth',2,'EdgeColor','r');
text(x2+width2-15,y2+height2-10,'II','Color', 'y','fontsize',15);
rectangle('Position',[x3,y3,width3,height3],'LineWidth',2,'EdgeColor','r');
text(x3+width3-15,y3+height3-10,'II','Color', 'y','fontsize',15);

frame=getframe(gcf);
result=frame2im(frame);
imwrite(result,strcat(file_path,'result_rect.bmp'));
hold off;

part_1 = get_rect(ref,x1,y1,width1,height1,'I');
imwrite(part_1,strcat(file_path,'part_1.bmp'));
part_2 = get_rect(ref,x2,y2,width2,height2,'II');
imwrite(part_2,strcat(file_path,'part_2.bmp'));
part_3 = get_rect(ref,x3,y3,width3,height3,'III');
imwrite(part_3,strcat(file_path,'part_3.bmp'));

function res = get_rect(ref,x,y,width,height,flag)
% 获取原图的局部矩形区域,并返回
im = ref(y:y+height-1,x:x+width-1,:);

figure;
set (gcf,'Position',[300,300,width,height]);
imshow(im,'border','tight','initialmagnification','fit');

text(width-15,height-10,flag,'Color', 'y','fontsize',15);

frame=getframe(gcf);
res=frame2im(frame);
end

效果如下:

matlab在图像上画矩形框并保存_第1张图片

matlab在图像上画矩形框并保存_第2张图片  matlab在图像上画矩形框并保存_第3张图片  matlab在图像上画矩形框并保存_第4张图片

你可能感兴趣的:(Matlab)