src_img_name = 'haixing.png';
img = imread(src_img_name);
% get binary image
gray_img = rgb2gray(img);
T = graythresh(gray_img);
bw_img = imbinarize(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);
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');
bw_img2 = bwareaopen(bw_img,P); %删除二值图像BW中面积小于P的对象,默认情况下conn使用8邻域
figure;
imshow(bw_img2);
matlab 连通分量去除孤立点
最新推荐文章于 2024-07-04 20:02:40 发布