2

I have detected blob keypoints in opencv c++. The centroid displays fine. How do I then draw a bounding box around the detected blob if I only have the blob center coordinates? I can't work backwards from center because of too many unknowns(or so I believe).

        threshold(imageUndistorted, binary_image, 30, 255, THRESH_BINARY);
        Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); 
        // Detect blob
        detector->detect(binary_image, binary_keypoints);
        drawKeypoints(binary_image, binary_keypoints, bin_image_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

//draw BBox ?

What am I overlooking to draw the bounding box around the single blob?

user121903
  • 57
  • 1
  • 1
  • 5

1 Answers1

0

I said:

I can't work backwards from center because of too many unknowns(or so I believe).

There is not limited information if blob size is used: keypoints.size which returns the diameter of the blob in question. Though there might be some inaccurate results with highly asymmetric or lopsided targets, this worked well for me b/c I used spheroid objects. Moments/ is probably the better approached for the asymmetrical targets.

keypoints.size should not be confused with keypoints.size(). The latter does a count in the vector of objects in my case the former is the diameter. Using both. Using the diameter I can then calculate the rest with no problem:

            float TLx = (ctr_x - r);
            float TLy = (ctr_y - r);
            float BRx = (ctr_x + r);
            float Bry = (ctr_y + r);
            Point TLp(TLx-10, TLy-10); //works fine without but more visible with enhancement
            Point BRp(BRx+10, Bry+10); //same here
            std::cout << "Top Left: " << TLp << std::endl << "Right Lower:" << BRp << std::endl;
            cv::rectangle(bin_with_keypoints, TLp, BRp, cv::Scalar(0, 255, 0));
            imshow("With Green Bounding Box:", bin_with_keypoints);

TLp = top left point with 10px adjustments to make box bigger.

BRp = bottom right point

TLx, TLy are calculated from blob center coordinates as well as BRps. If you are going to use multiple targets would suggest contours approach (with the moments). I have 1 - 2 blobs to keep track of which is a lot easier but keeps resource usage down.

Rectangle drawing function can also work with Rect (diameter = keypoint.size)

Rect r(TLp, BRp, center_x + diameter/2, center_y+diamter/2) // r(TLc, BRc, width, heigth)
cv::rectangle(bin_with_keypoints, rect, cv::Scalar(0, 255, 0));
user121903
  • 57
  • 1
  • 1
  • 5