您的位置:首页 > 编程语言 > MATLAB

Matlab中regionprops的使用示例

2016-05-14 16:46 549 查看
版权声明:本文为shaoxiaohu原创文章,欢迎转载,请注明出处,谢谢。

有这样一幅图,



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

[plain] view
plain copy







src_img_name = 'blue_sky_white_clound_002594.jpg';

img = imread(src_img_name);

% get binary image

gray_img = rgb2gray(img);

T = graythresh(gray_img);

bw_img = im2bw(gray_img, T);

% find the largest connected region

img_reg = regionprops(bw_img, 'area', 'boundingbox');

areas = [img_reg.Area];

rects = cat(1, img_reg.BoundingBox);

显示所有连通区域,

[plain] view
plain copy







% show all the largest connected region

figure(1),

imshow(bw_img);

for i = 1:size(rects, 1)

rectangle('position', rects(i, :), 'EdgeColor', 'r');

end



显示最大连通区域,

[plain] view
plain copy







[~, max_id] = max(areas);

max_rect = rects(max_id, :);

% show the largest connected region

figure(2),

imshow(bw_img);

rectangle('position', max_rect, 'EdgeColor', 'r');

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: