Matlab中对图像应用plot或者rectangle后的图像保存问题

有时候,我们处理好图像后,为了标识出图像的目标区域来,需要利用plot函数或者rectangle函数,这样标识目标后,就保存图像。一般的保存图像可以利用figure中的edit菜单中的copy figure,这样可以完成,但是保存后的图像外围多了一片区域,这是figure的区域,效果如下:

 Matlab中对图像应用plot或者rectangle后的图像保存问题

外部的白色区域不是我们想要的……

于是我们想办法,利用imwrite函数可以保存图像,但是利用plot或者rectangle函数后,并没有改变图像原来的像素值,imwrite函数不可以。怎么办?哈哈……于是就有了下面的一种算法……

以下面的图像为例,将图像中的白色区域利用矩形标记出来:

Matlab中对图像应用plot或者rectangle后的图像保存问题

具体的程序如下所示:

clc;close all;clear all;
Img=imread('1.jpg');
if ndims(Img)==3
    I=rgb2gray(Img);
else
    I=Img;
end
I=im2bw(I,graythresh(I));
[m,n]=size(I);
imshow(I);title('binary image');
txt=get(gca,'Title');
set(txt,'fontsize',16);
L=bwlabel(I);
stats=regionprops(L,'all');
set(gcf,'color','w');
set(gca,'units','pixels','Visible','off');
q=get(gca,'position');
q(1)=0;%设置左边距离值为零
q(2)=0;%设置右边距离值为零
set(gca,'position',q);
for i=1:length(stats)
    hold on;
    rectangle('position',stats(i).BoundingBox,'edgecolor','y','linewidth',2);
    temp = stats(i).Centroid;
    plot(temp(1),temp(2),'r.');
    drawnow;
end
frame=getframe(gcf,[0,0,n,m]);
im=frame2im(frame);
imwrite(im,'a.jpg','jpg');%可以修改保存的格式

保存图像如下所示:

Matlab中对图像应用plot或者rectangle后的图像保存问题
success……哈哈……

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值