Ask Your Question

Bounding boxes around certain colors

asked 2019-10-30 01:48:53 -0500

aditya_mangalampalli gravatar image

Hello all, Sorry for asking such a basic question, I'm still getting used to OpenCV. I had a special instance where I have to draw bounding boxes around all the yellow blocks in the image. However, I had no way on how to approach this. Inside some yellow blocks, there are black squares, and I need to be able to detect those too. Is it possible to have one bounding box inside the other? How would I go about approaching this? All help would be greatly appreciated. Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-10-30 06:06:19 -0500

updated 2019-10-30 06:10:16 -0500

If you add an image it'll be more clear. I have only a C++ code to detect colors and draw bounding box search in the internet and you will find a lot of codes with Python doing the same job.

    //===================== code ====================
     if you want to detect yellow  blocks you can test this :
//Convert your image to HSV
cvtColor(im, imHSV, COLOR_BGR2HSV);
//Select yellow part of the image
inRange(imHSV, Scalar(0, 208, 186), Scalar(47, 255, 255), mask1);
//Detect the contours of the yellow parts
vector<vector<Point>> contours; // Vector for storing contours
vector<Point> Ncontours;
 // Find the contours in the image
findContours(mask1, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE); 

for (size_t i = 0; i < contours.size(); i++) // iterate through each contour.
    double area = contourArea(contours[i]);  //  Find the area of contour

    if (area > largest_area)
        largest_area = area;
            //Store the index of largest contour
        largest_contour_index = i;
             // Find the bounding rectangle for biggest contour               
        bounding_rect = boundingRect(contours[i]);
        Ncontours = contours[i];

//In this code i serch the largest_area you can modifie the code to get all the contours present in you code
edit flag offensive delete link more


Hey Kitnos, I had an image which could offer more info. Here is the image. I wanted to know if this technique which you presented could also work for real time video. So for the image, you can see the blocks in the bottom and how they are lined up right next to each other. I wanted to know if the filter can identify and draw bounding boxes around individual blocks instead of a long rectangle.

aditya_mangalampalli gravatar imageaditya_mangalampalli ( 2019-10-30 11:31:54 -0500 )edit

Yes it will work in real time video, but this method (color filtering ), will detect only one long rectangle that englobe all the blocks.

Kitnos gravatar imageKitnos ( 2019-10-30 14:35:21 -0500 )edit

Question Tools

1 follower


Asked: 2019-10-30 01:48:53 -0500

Seen: 1,787 times

Last updated: Oct 30 '19