Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to select a specific bounding box

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

how to select a specific bounding box

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;

}