Ask Your Question
0

how to select a specific bounding box

asked 2013-12-10 01:17:03 -0600

akoo gravatar image

updated 2013-12-10 03:56:20 -0600

Hi everybody, I'm doing a program to obtain bounding boxes and once obtained these bounding boxes I would like to select only one of these. Is there any way to select only the desired bounding box. For example, in the attached picture, I would like to select only the bounding box where is a license plate inside, but I don't know how can I do that. Any help? Thanks in advance. image description

Mat findBox(Mat imagen){

//blur(imagen,imagen,Size(3,3));
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/// Detect edges using Threshold
threshold( imagen, threshold_output, 140, 255, THRESH_BINARY );
/// Find contours
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );

for( uint i = 0; i < contours.size(); i++ ){
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
    minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
}
/// Draw polygonal contour + bonding rects
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
cvtColor(imagen,imagen,CV_GRAY2BGR);
Scalar colorRed( 0,0,255);
Scalar colorWhite( 255,255,255);
Point inici;Point fin;
//Point un, dos;
float ratio;

for( uint i = 0; i< contours.size(); i++ ){

    double area=contourArea(contours[i]); //calculamos area de contornos
    cout<<area<<endl;
    /*si mayor que 1000 tengo la placa matricula
     * si mayor que 200 los caracteres y calculamos el aspect ratio */
    if( area >= 1000 ){
        if(area <= 8000){


            //Point uno(boundRect[i].x,boundRect[i].y);
            //inici=uno;
            //cout<<"Bound:"<<boundRect[i].br()<<" "<<inici.y<<endl;
            drawContours( drawing, contours_poly, i, colorWhite, 1, 8, vector<Vec4i>(), 0, Point() );
            //rectangle( imagen, boundRect[i].tl(), boundRect[i].br(), colorRed, 1, 8, 0 );
            //Point dos(boundRect[i].br());
            //fin=dos;

            ratio = boundRect[i].width/boundRect[i].height;
            if(ratio >= 3){
                if(ratio <= 6){
                    cout<<"ratio:"<<ratio<<endl;
                    rectangle( imagen, boundRect[i].tl(), boundRect[i].br(), colorRed, 1, 8, 0 );

                    Point p1(boundRect[i].x,boundRect[i].y);
                    Point p2(boundRect[i].br());
                    inici=p1;
                    fin=p2;
                }
            }
        }
    }

}

imshow("Image",imagen);
return drawing;

}

edit retag flag offensive close merge delete

Comments

maybe you could add, how you get your bounding boxes ?

berak gravatar imageberak ( 2013-12-10 03:10:39 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-12-10 02:31:20 -0600

pyro gravatar image

This is not an easy problem to give a generic solution. But for the given image and bounding boxes I can think of two approaches:

  1. Find features which can distinguish between the bounding boxes, such as color. You could calculate the color histogram (assuming you could also use a color image) for the patch in each bounding box, and make a comparison to select the one you need. Example code: http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

  2. Assuming that you only want to detect the bounding box with some alphanumerics inside, you could consider applying Optical Character Recognition for each patch, and select the one which has letters and numbers. Note however that, you may get false positives if there are other patches satisfying this criteria. Example Python code to get started: http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python

This is really a very specific problem, and you need to use your domain knowledge to think of features that can help to select the required patch.

edit flag offensive delete link more

Comments

I'm trying to use an OCR to recognize these characters from inside the license plate that is the reason I want to select only the bounding box containing characters, to run after an OCR engine. Anyway I think I've found one way to select it. It has worked with some pictures, not only this. I have calculated the aspect ratio of a bounding boxes, and if it is between 3 and 6 then is a possible license plate. Thanks pyro.

akoo gravatar imageakoo ( 2013-12-10 03:15:38 -0600 )edit

@akoo I don't think aspect ratio is an elegant solution. You may get a lot of false positives. Assuming that the license plate primarily contains black and white colors, you can consider using the color feature first, followed by OCR to narrow down your selection.

pyro gravatar imagepyro ( 2013-12-10 21:01:27 -0600 )edit

Yeah pyro, I've realized this solution gives me some false positives. I guess I would do any other thing to avoid these false positives. And how can I pass the bounding boxes to the function for computing the histogram, because they are not Mat, as you can see in the code. Thanks

akoo gravatar imageakoo ( 2013-12-11 01:17:36 -0600 )edit

@akoo you can consider accepting the answer if it helped.

pyro gravatar imagepyro ( 2013-12-14 08:44:31 -0600 )edit

@akoo regarding your previous question, since you already have the coordinates for the bounding box, you can extract the patch from the image using cv::Rect. There's an example here: http://stackoverflow.com/questions/16621983/how-opencv-c-interface-manage-roi

pyro gravatar imagepyro ( 2013-12-14 08:48:00 -0600 )edit

Question Tools

Stats

Asked: 2013-12-10 01:17:03 -0600

Seen: 3,049 times

Last updated: Dec 10 '13