Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

In this cord it finds all the contours and extract each contour and save them as a .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

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;

}

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

In this cord it finds all the contours and extract each contour and save them as a .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

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;

}

click to hide/show revision 3
hi there, please don't use hash signs with tags, as it leads to annoying duplicates. thank you!

updated 2013-09-02 12:28:13 -0600

berak gravatar image

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

In this cord it finds all the contours and extract each contour and save them as a .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

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;

}

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

In this cord it finds all the contours and extract each contour and save them as a .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

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;

}

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

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

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;

}

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

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;

}

cvWaitKey(0);

return 0;

}