Matlab中的regionprops

来源:互联网 发布:熊猫弹幕机器人软件 编辑:程序博客网 时间:2024/06/06 00:41

有这样一幅图,


我们想获取其中的连通区域,可以使用以下代码:

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. src_img_name = 'blue_sky_white_clound_002594.jpg';  
  2. img = imread(src_img_name);  
  3.   
  4. % get binary image  
  5. gray_img = rgb2gray(img);  
  6. T = graythresh(gray_img);  
  7. bw_img = im2bw(gray_img, T);  
  8.   
  9. % find the largest connected region  
  10. img_reg = regionprops(bw_img,  'area', 'boundingbox');  
  11. areas = [img_reg.Area];  
  12. rects = cat(1,  img_reg.BoundingBox);  

显示所有连通区域,

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. % show all the largest connected region  
  2. figure(1),  
  3. imshow(bw_img);  
  4. for i = 1:size(rects, 1)  
  5.     rectangle('position', rects(i, :), 'EdgeColor', 'r');  
  6. end  

显示最大连通区域,

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. [~, max_id] = max(areas);  
  2. max_rect = rects(max_id, :);  
  3.   
  4. % show the largest connected region  
  5. figure(2),   
  6. imshow(bw_img);  
  7. rectangle('position', max_rect, 'EdgeColor', 'r');  

0 0