In this tutorial I want explain how to segment an image and detect each object inside image, in this tutorial we can detect objects from plate (the numbers) or each object draw into a paper.
We can define three steps to do this task: preprocess image, find countours, and calculate bounding rect. With this three steps we have each object separatly and then we can use each object to classify it with basicocr sample.
For preprocessing task we use:
- Smooth filter to simple noise elimination.
- Threshold image to get binary image
- Morphologic filter, erode and dilate to eliminate noise.
Once we have preprocessed our input image we look for the contours in our binary image with cvFindContours, and optimize its with approxpoly.
//Search countours in preprocesed image
cvFindContours( img_contornos, storage, &contour, sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0) );
//Optimize contours, reduce points
contourLow=cvApproxPoly(contour, sizeof(CvContour), storage,CV_POLY_APPROX_DP,1,1);
Then for each searched contour we calculate the bounding rect.
//For each contour found
for( ; contourLow != 0; contourLow = contourLow->h_next )
We can now draw his contour and/or bounding rect.
pt1.x = rect.x;
pt2.x = (rect.x+rect.width);
pt1.y = rect.y;
pt2.y = (rect.y+rect.height);
cvRectangle(imagen_color, pt1,pt2, color, 1, 8, 0);
cvDrawContours( imagen_color, contourLow, color, color, -1, 0, 8, cvPoint(0,0) );
Now we have each object differenciate, now we can use this to get characteristics to classificate, for example the 7 invariant hu moments.
//First calculate object moments
cvMoments(contourLow, &moments, 0);
//Now calculate hu moments
Download source from GitHub
About David Millán Escrivá
David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.