使用Matlab中regionprops函数获取图像连通区域

 申明:本文转载自http://blog.csdn.net/shaoxiaohu1/article/details/40272531

Matlabregionprops的使用示例

获取图像的连通区域。有这样一幅图,

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

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);

 

显示所有连通区域,

% show all the largest connected region

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, :);

 

% show the largest connected region

figure(2),

imshow(bw_img);

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

你可能感兴趣的:(图像处理之MATLAB)