Ask Your Question

Number plate segmentation C++

asked 2013-08-23 13:22:23 -0500

Vanushka gravatar image

updated 2013-08-23 13:23:35 -0500

I have a basic knowledge in opencv and this is for my project. I am able to segment characters in this image image description

using this cord:

int main() {

cv::Mat img_ori= cv::imread("image33.jpg");
if (!
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::Mat binary;

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

findContours(blur_out, contours, CV_RETR_EXTERNAL, 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>2 && boundRect[i].width>1)
    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=binary(region_of_interest);

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

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




return 0;


But i want to segment characters in a this kind of image with the border. image description

I am really grateful if someone can help.

edit retag flag offensive close merge delete



There is something called hierarchy in contours. Look it up. Maybe it can help you. A Naive way is area constraint. Just remove the 'contour' having highest area, has a 'hole' inside or something like that. (If you dont know what is the difference in 'hole' and 'contour', check the learning opencv oreilly book).

Prasanna gravatar imagePrasanna ( 2013-08-23 18:28:43 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2013-08-24 01:35:05 -0500

In the book Mastering OpenCV with Practical Computer Vision Projects, chapter 5, by David Escrivá explain plate detection as well as OCR on letters and numbers. The code is available on Gihub here. I think it better to see the whole process than to focus on each stage (in the first approach). After, when everything is working, you may want to focus on some part of recognition, like precise number/letter extraction.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-08-23 13:22:23 -0500

Seen: 6,497 times

Last updated: Aug 24 '13