Question

Using the code below I can draw a bounding box around every connected component.

bw_normal2 = im2bw(img, graythresh(img));
bw22 = imcomplement(bw_normal2);
bw3 = bwmorph(bw22, 'dilate');
[label2,n2] = bwlabel(bw3);
stats2 = regionprops(label2, {'Area', 'BoundingBox'});
area2 = stats2.Area;

idx = find((0 <= area2) & (area2 <= 6000))
   BW2 = ismember(label2,idx);
figure, imshow(BW2)

imshow(img);
for j=1:n

    hold on
    rectangle('Position',[stats2(j).BoundingBox(1),stats2(j).BoundingBox(2),stats2(j).BoundingBox(3),stats2(j).BoundingBox(4)],...
'EdgeColor','r','LineWidth',2 );
end

enter image description here

Rather than making a bounding box around every connected component is it possible to randomly colour them?

Was it helpful?

Solution

Why don't you just use label2? That's already a matrix with each individual component given a different value. Then apply one of the built in colormaps, or produce a colormap which forces the background (zero) values to be white or black as you prefer.

imshow(label2,[])
colormap(lines)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top