Question

I have a task that requires analyzing an image filled with colored shapes and highlighting the blue circles in the image. To do this, I have split the image into its RGB channels and created a binary image of only the blue pixels. Using blob detection I've managed to segment the image and find the roundness for each of the shapes. However, when I try to place a boundary line on the round shapes, it places the boundary around ALL shapes, not just the circles. The relevant section of code is shown below;

imshow(BBinEro)
for cnt = 1:length(BlueProps)
    %disp(score);
    if score(cnt) >= 0.98 %
        text(BlueProps(cnt).Centroid(1),BlueProps(cnt).Centroid(2),num2str(score(cnt)),'color','red');

        boundaries = bwboundaries(BBinEro); 
        numberOfBoundaries = size(boundaries);
        hold on
        for k = 1 : numberOfBoundaries
            thisBoundary = boundaries{k};
            if score(cnt) >= 0.98 %Only showing for circles
            plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
            end
        end
        hold off
    end    

end

"Score" is a measure of the circularity of the blob. A score of 1 means that the blob is perfectly round. The program displays the circularity of the round shapes at their centroid; using the same if-statement, I've attempted to incorporate inserting a boundary line around them. Any suggestions or observations on how I can fix this code so that it only shows the boundaries on the circles would be greatly appreciated.

The attached image shows what the program is currently doing. I want to get rid of the green boundaries around the triangle and the square. enter image description here

Was it helpful?

Solution

This seems to solve your problem:

[B,L,N,A] = bwboundaries(BBinEro);
for k=1:length(B)
    text(BlueProps(k).Centroid(1),BlueProps(k).Centroid(2),num2str(score(k)),'color','red');
    if(~sum(A(k,:))) & score(k) >= 0.98
        boundary = B{k};
        plot(boundary(:,2), boundary(:,1), 'r','LineWidth',2);
    end
end 

I just added the condition & score(k) >= 0.98 to an example in the matlab documentation here. This assumes that the score array is ordered in the same way as the objects array generated by bwboundaries. If objects in score are ordered in the same way as the objects generated by regionprops then this is ok, since regionprops and bwboundaries index objects in the same order.

The error in your code lies in that you recalculate bwboundaries(BBinEro) with each iteration of your outer loop. Presumably you are trying to compute boundaries for each new object with each new iteration, but instead you are computing over the entire image (which I guess is BBinEro). Hope this solves your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top