Ask Your Question
0

extract numbers and bound rectangle on each number

asked 2015-06-28 16:21:02 -0600

ell gravatar image

updated 2015-06-29 22:14:51 -0600

image description

how to extract the numbers in the image above, please note, the number are "touching" the black area. so the numbers will not be damaged for ocr. and how to bound each number (and each "/") with rectangle

another possible line, hitting numbers:

image description

image description

and some more morphologic touching adjacent numbers or letters, follow the "/" touching the "2" :
how to split it correctly ?

image description

edit retag flag offensive close merge delete

Comments

Add more than one example trying to show different types of input.

Diego Cerdan gravatar imageDiego Cerdan ( 2015-06-28 19:28:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-06-28 21:44:00 -0600

here a simple experimental code, i hope it will help you to be a starting point.

result image 1 :image description

result image 2 :image description

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

void step2(Mat src)
{
    Mat gray;
    cvtColor(src,gray,CV_BGR2GRAY);
    threshold(gray,gray,200,255,THRESH_BINARY_INV);

    vector< vector <Point> > contours;
    vector< Vec4i > hierarchy;

    findContours( gray, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

    for( int i = 0; i< contours.size(); i=hierarchy[i][0] )
    {
        Rect r= boundingRect(contours[i]);
        rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255),1,8,0);
    }

    imwrite("result2.jpg",src);
    imshow("step 2 result",src);
    waitKey();
}

int main( int argc, char** argv )
{
    Mat src=imread("14355274696376005.png",1),gray,temp;
    cvtColor(src,gray,CV_BGR2GRAY);
    gray = gray > 127;
    imshow( "Source", src );

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours( gray, contours, hierarchy,
                  CV_RETR_CCOMP, CHAIN_APPROX_SIMPLE );


    Rect minRect;

    if (contours.size()>0)
    {

        for( size_t i = 0; i<contours.size() ; i++ )
        {
            minRect = boundingRect( Mat(contours[i]) );
            if (minRect.width>src.cols*0.9)
            {
                minRect.height=minRect.y+4;
                minRect.y=0;
                temp=src(minRect);
                temp=Scalar(255,255,255);
                imshow( "step 1 result", src );
                imwrite("result1.jpg",src);

                step2(src);

                waitKey(0);
            }
        }
    }
}
edit flag offensive delete link more

Comments

hello turkman, sorry fo late response

the first example it tooks ok, as your sample code, I added 2 other exalple where the horizontal line(thin) actually hits the number and the results damaged the number

ell gravatar imageell ( 2015-11-19 04:16:41 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-06-28 16:21:02 -0600

Seen: 3,730 times

Last updated: Jun 29 '15