Number plate segmentation C++
I have a basic knowledge in opencv and this is for my project. I am able to segment characters in this image
using this cord:
int main() {
cv::Mat img_ori= cv::imread("image33.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::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;
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 );
cvNamedWindow(cstr,CV_WINDOW_AUTOSIZE);
imshow(str,imageroi);
x++;
}
}
imshow("Box",img_ori);
cvWaitKey(0);
return 0;
}
But i want to segment characters in a this kind of image with the border.
I am really grateful if someone can help.
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).