Ask Your Question

Revision history [back]

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);
            }
        }
    }
}