Ask Your Question
1

Sort bounding boxes in x- Axis of a image in order

asked 2013-09-02 12:25:12 -0600

Vanushka gravatar image

updated 2020-05-24 07:24:32 -0600

supra56 gravatar image

In this cord it finds all the contours and extract each contour and save them as .jpg randomly . But is there a method to save the images in order from left to right (e.g. as 1.jpg, 2.jpg, 3.jpg...).

Any suggestions will be greatly appreciated. Thank you.

image description

Code:

int main()
{


    cv::Mat img_ori= cv::imread("NumberPlate.jpg");
    if (!img_ori.data)
    {
    std::cout << "Image file not found\n";
    return 1;
    }

    cv::Mat img_threshold;
    // threshold image
    cv::cvtColor(img_ori, img_threshold, CV_BGR2GRAY);
    //cv::threshold(img_threshold,img_threshold, 150, 255, CV_THRESH_BINARY_INV);
    //cv::adaptiveThreshold(img_threshold, img_threshold,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,15,-5);
    cv::threshold(img_threshold, img_threshold, 128, 255,CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    cv::Mat binary;
    cv::erode(img_threshold,binary,cv::Mat(),cv::Point(-1,-1),2);
    cv::dilate(img_threshold,binary,cv::Mat(),cv::Point(-1,-1),3);

    //Search contours
    cv::Mat blur_out(img_threshold);
    cv::vector<cv::vector<cv::Point> > contours;
    cv::vector<cv::Point> points;
    cv::vector<cv::Vec4i> hierarchy;

    //cv::Mat canny_output;
    //int thresh = 100;

    //Canny( img_threshold, canny_output, thresh, thresh*2, 3 );

    //findContours(blur_out, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    findContours(img_threshold, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    // Approximate contours to polygons + get bounding rects 
    cv::vector<cv::vector<cv::Point> > contours_poly( contours.size() );
    cv::vector<cv::Rect> boundRect( contours.size() );
    cv::vector<cv::Point2f>center( contours.size() );
    cv::vector<float>radius( contours.size() );

    for( int i = 0; i < contours.size(); i++ )
    {
    approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = cv::boundingRect( cv::Mat(contours_poly[i]) );
    }

    //Draw bounding rect
    int x=1;
    cv::Mat drawing(img_ori);
    for( int i = 0; i< contours.size(); i++ )
    {
        std::stringstream s;
        s << x;
        std::string str1 = s.str();
        cv::Scalar color = cv::Scalar(0,255,0);

        if(boundRect[i].height>50 && boundRect[i].width>25)
        {
        rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
        cv::Rect region_of_interest = cv::Rect(boundRect[i].x,boundRect[i].y,boundRect[i].width,boundRect[i].height);
        cv:: Mat imageroi=img_ori(region_of_interest);

        //templates.Add(new Comparison<FoundTemplateDesc>((t1, t2) => -t2.sample.contour.SourceBoundingRect.X.CompareTo(t1.sample.contour.SourceBoundingRect.X)));

        std::stringstream ss;
        ss << i;
        std::string str = ss.str();
        const char *cstr = str.c_str();

        cv::imwrite( str1+".jpg", imageroi );

        cvNamedWindow(cstr,CV_WINDOW_AUTOSIZE);
        imshow(str,imageroi);
        x++;
        }
    }

    imshow("Box",img_ori);

cvWaitKey(0);

return 0;

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
7

answered 2013-09-02 13:22:43 -0600

berak gravatar image

if you got a vector<Rect>, you can use std::sort, all you need is a compare function:

bool compare_rect(const Rect & a, const Rect &b) {
    return a.x < b.x;
}

vector<Rect> rects;
sort( rects.begin(), rects.end(), compare_rect );
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-09-02 12:25:12 -0600

Seen: 8,227 times

Last updated: May 24 '20