フィルターのクリア

how to extract connected components with similar height

1 回表示 (過去 30 日間)
Lukasz Jarod
Lukasz Jarod 2015 年 1 月 6 日
回答済み: DGM 2024 年 9 月 23 日
im doing simple extraction of connected components, but now i want to choose only componets with similar height how can i do that??
[L Ne]=bwlabel(imagen);
labeledImage=bwlabel(imagen);
%%Measure properties of image regions
propied=regionprops(L,'BoundingBox');
hold on
%%Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
pause (1)
%%Objects extraction
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
height=max(r)-min(r)+1;
width=max(c)-min(c)+1;
end

回答 (1 件)

DGM
DGM 2024 年 9 月 23 日
There are some problems here.
% the input image
inpict = imread('fruittext.png');
inpict = im2gray(inpict);
mask = imbinarize(inpict);
imshow(mask)
[L Ne] = bwlabel(mask);
labeledImage = bwlabel(mask); % this is redundant
%%Measure properties of image regions
propied = regionprops(L,'BoundingBox');
%%Plot Bounding Box
hold on
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
%%Objects extraction
% this accomplishes nothing.
% height and width are overwritten every time.
% regionprops() already gave us all this information anyway.
for n = 1:Ne
[r,c] = find(L==n);
n1 = inpict(min(r):max(r),min(c):max(c));
height = max(r)-min(r)+1;
width = max(c)-min(c)+1;
end
I'm just going to start over. For this example, I'm just going to be lazy and cluster the objects by height. If you have a specific target height you want to use for discrimination, you can just do that based on the list of object heights instead of using the clustering. The main point here that addresses the question is the fact that the bounding box information that we get from regionprops() already gives us the object height. We don't need to do anything else to get it.
% the input image
inpict = imread('fruittext.png');
inpict = im2gray(inpict);
mask = imbinarize(inpict);
imshow(mask)
% get object heights
S = regionprops(mask,'BoundingBox','image');
objheight = vertcat(S.BoundingBox);
objheight = objheight(:,4);
% cluster the heights
nclusters = 2; % pick something relevant
groupidx = kmeans(objheight,nclusters);
% extract all the blobs as separate images
blobs = cell(nclusters,1);
for kc = 1:nclusters
thesemembers = find(groupidx == kc);
nmembers = numel(thesemembers);
blobs{kc} = cell(nmembers,1);
for km = 1:nmembers
blobs{kc}{km} = S(thesemembers(km)).Image;
end
end
% visualize the collected blobs
for kc = 1:nclusters
subplot(1,nclusters,kc)
montage(blobs{kc},'border',2,'backgroundcolor','g')
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by