OpenCV - find bounding box of largest blob in binary image

Related searches

What is the most efficient way to find the bounding box of the largest blob in a binary image using OpenCV? Unfortunately, OpenCV does not have specific functionality for blob detection. Should I just use findContours() and search for the largest in the list?

If you want to use OpenCV libs, check out OpenCVs SimpleBlobDetector. Here's another stack overflow showing a small tutorial of it: How to use OpenCV SimpleBlobDetector

This only gives you key points though. You could use this as an initial search to find the blob you want, and then possibly use the findContours algorithm around the most likely blobs.

Also the more information you know about your blob, you can provide parameters to filter out the blobs you don't want. You might want to test out the area parameters of the SimpleBlobDetector. Possibly could could compute the area based on the size of the area of the image and then iteratively allow for a smaller blob if the algorithm does not detect any blobs.

Here is the link to the main OpenCV documentation: http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html#simpleblobdetector

OpenCV - find bounding box of largest blob in binary image, What is the most efficient way to find the bounding box of the largest blob in a binary image using OpenCV? Unfortunately, OpenCV does not have specific� findContours gives me the border of the image. What is the best way to find bounding box for binary mask? cv::findContours, unable to find contours. Do all opencv functions support in-place mode for their arguments? What is the most effective way to access cv::Mat elements in a loop? Sobel derivatives in the 45 and 135 degree direction

Here. It. Is. (FYI: try not to be lazy and figure out what happens in my function below.

cv::Mat findBiggestBlob(cv::Mat & matImage){
    int largest_area=0;
    int largest_contour_index=0;

    vector< vector<Point> > contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    findContours( matImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( int i = 0; i< contours.size(); i++ ) {// iterate through each contour. 
        double a=contourArea( contours[i],false);  //  Find the area of contour
        if(a>largest_area){
            largest_area=a;
            largest_contour_index=i;                //Store the index of largest contour
            //bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }
    }

    drawContours( matImage, contours, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
    return matImage;
}

What is the best way to find bounding box for binary mask , Is there any code for Eye and Yawning detection using opencv? The maximum circle or rectangle inside the blob! Copyright Itseez, 2012-2016. To find the centroid of the image, we generally convert it to binary format and then find its center. The centroid is given by the formula:- is the x coordinate and is the y coordinate of the centroid and denotes the Moment. Steps for finding Centroid of a Blob in OpenCV. To find the center of the blob, we will perform the following steps:-1.

To find the bounding box of the largest blob, I used findContours, followed by the following code:

double maxArea = 0;
for (MatOfPoint contour : contours) {
    double area = Imgproc.contourArea(contour);
    if (area > maxArea) {
        maxArea = area;
        largestContour = contour;
    }
}
Rect boundingRect = Imgproc.boundingRect(largestContour);

Get the contours of largest blob, double area = contourArea(contours[i]); // Find the area of contour if (area > boundingRect(contours[i]); // Find the bounding rectangle for biggest contour Mat thr; threshold(mask_combined, thr, 125, 255, THRESH_BINARY); // Threshold CHAIN_APPROX_SIMPLE); // Find the contours in the image for� For every found contour we now apply approximation to polygons with accuracy +-3 and stating that the curve must be closed. After that we find a bounding rect for every polygon and save it to boundRect. At last we find a minimum enclosing circle for every polygon and save it to center and radius vectors.

TimZaman, your code has a bug but I can't comment so I start a new and correct answer. Here is my solution based on 1"'s and TimZaman's ideas:

Mat measure::findBiggestBlob(cv::Mat &src){
int largest_area=0;
int largest_contour_index=0;
Mat temp(src.rows,src.cols,CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
src.copyTo(temp);

vector<vector<Point>> contours; // storing contour
vector<Vec4i> hierarchy;

findContours( temp, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

for( int i = 0; i< contours.size(); i++ ) // iterate
{
    double a=contourArea( contours[i],false);  //Find the largest area of contour
    if(a>largest_area)
    {
        largest_area=a;
        largest_contour_index=i;
    }

}

drawContours( dst, contours,largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); 
// Draw the largest contour
return dst;
}

Calc a bounding box with a binarized image?, Steps: Find contours(largest contour) in your image. Calculate boundingRect for the largest contour. Reference :. Suppose you have the following image: Now I want to extract each of the independent letters into individual images. Currently, I've recovered the contours and then drew a bounding box, in this case for the character a: After this, I want to extract each of the boxes (in this case for the letter a) and save it to an image file. Expected result:

Since no one has posted a complete OpenCV solution, here's a simple approach using thresholding + contour area filtering


Input image

Largest blob/contour highlighted in green

import cv2

# Load image, grayscale, Gaussian blur, and Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours and sort using contour area
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
    # Highlight largest contour
    cv2.drawContours(image, [c], -1, (36,255,12), 3)
    break

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

Opencv find largest rectangle, Find Contours in the image ( image should be binary as given in your question) 2 . inRange(pupilFrame,250,255) #get the blobs _, contours, hierarchy = cv2. [ boundingBox] opencv example python - Contours – bounding box, minimum� To find the different features of contours, like area, perimeter, centroid, bounding box etc; You will see plenty of functions related to contours. 1. Moments . Image moments help you to calculate some features like center of mass of the object, area of the object etc. Check out the wikipedia page on Image Moments

You can compare the relative position by comparing the centroid of each blob, and you can compare the relative size of each blob by extracting its contour and obtaining a bounding box. However, if you can't specify what you want (the selection criteria) then naturally you also can't get what you want.

Draws a bounding box around a moving object; Calculates the coordinates of the centroid of the object; Tracks the centroid of the object; Directions. Open up your favorite IDE or code editor. Make sure you have the OpenCV and Numpy libraries installed. There are a number of ways to install both libraries.

Line 42 computes the bounding box of the contour. Next, we make sure these bounding boxes are a reasonable size and filter out those that are either too large or too small (Line 46). For each bounding box meeting our size criteria, we extract the region of interest (roi) associated with the character (Line 50).

Comments
  • It depends what you want to do with the blob afterwards, but your approach is valid :)
  • If you say what language you're using, you may get more specific answers.
  • I just read a bit about this. If you already have a binary image, it sounds like using Suzuki's method (findContours) is a really good fit. You could also incrementally find the first white pixel, then use floodFill to find the rest of that region, and so on. But I'm not sure that would be any faster.
  • I just want to find the bounding box of the blob. I'm using OpenCV for Android, but all versions of OpenCV have pretty much the same functionality.
  • Thanks for the link, this looks like a good alternative to findContours. Unfortunately it's not in OpenCV4Android, so I'll stick to my original plan.
  • Isn't this a duplicate of my own answer?
  • Nope, my answer is in C++, the core language for OpenCV. As i said, it's for the lazy ones.
  • @TimZaman Could you help me with the java equivalent of drawContours()
  • What if there is a hole inside that contour and we want the blob with the largest area?