funykatebird的个人博客分享 http://blog.sciencenet.cn/u/funykatebird

博文

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

已有 4213 次阅读 2016-9-23 15:37 |系统分类:科研笔记

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

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

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


具体的程序如下所示:

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');%可以修改保存的格式

保存图像如下所示:

frame=getframe(gcf,[0,0,n,m]);

F = getframe(h,rect) specifies a rectangular area from which to copy the pixmap. rect is relative to the lower left corner of the figure or axes h, in pixel units. rect is a four-element vector in the form [left bottom width height], where width and height define the dimensions of the rectangle.

rec的坐下角坐标可以通过imtool将图片打开,看一下该点的坐标即可(以图像左上角为原点)

换算一下即可


如该图左下角坐标为(83,181),换算后的坐标为(83,59=240-181)

语句修改为:

frame=getframe(gcf,[83,59,320,240]);

320 240为片的宽与高



https://blog.sciencenet.cn/blog-597740-1004598.html

上一篇:PS处理图片技巧
下一篇:如何选择适合的SCI期刊进行投稿
收藏 IP: 202.113.11.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 17:28

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部