获取图片中物体轮廓最小外接矩形
``
src_img_name=‘1.png’;
img=imread(src_img_name);
gray_img=rgb2gray(img);
T=graythresh(gray_img)
bw_img=im2bw(gray_img,T)
img_reg=regionprops(bw_img,‘area’,‘boundingbox’)
areas=[img_reg.Area];
rects=cat(1,img_reg.BoundingBox);
figure(1)
imshow(bw_img);
for i=1:size(rects,1)
rectangle(‘position’,rects(i,:),‘EdgeColor’,‘r’);
end
[~,max_id]=max(areas);
max_rect=rects(max_id,:);
figure(2)
imshow(bw_img);
rectangle(‘position’,max_rect,‘EdgeColor’,‘r’);
![在这里插入图片描述](https://img-blog.csdnimg.cn/cb0937add5e04ce095880a6afff19085.png)