图像搜索目标框-Matlab中regionprops的使用示例
目录
图像搜索目标框—Matlab中regionprops的使用示例
图像搜索目标框
版权声明:本文为shaoxiaohu原创文章,欢迎转载,请注明出处,谢谢。
有这样一幅图,
我们想获取其中的连通区域,可以使用以下代码:
[plain]
- 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]
- % 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]
- [~, 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’);