大量图片中的车牌定位

时间:2016-10-04 11:41:33

标签: c++ opencv

我有大量的汽车图像,想要进行自动车牌识别,但我陷入了本地化阶段。我希望单独获得车牌作为输出,我可以进行识别。这是我的本地化代码:< / p>

int main(int args,char* argv)
{
//String filename;
//filename="";
cv::Mat image=cv::imread("C:\\Users\\Sarora\\Downloads\\Images\\frame_1375.jpg",CV_LOAD_IMAGE_COLOR);
cv::Mat img;
cv::Mat img_sobel;
cv::Mat grad_x, grad_y;
cv::Mat abs_grad_x, abs_grad_y;
cv::Mat imgContours;

//vector <Plate>result;

cv::cvtColor(image, img, CV_BGR2GRAY);
blur(img, img, cv::Size(5,5));
//cv::namedWindow("Img1.jpg", CV_WINDOW_AUTOSIZE );

//sobel filter applied on image..............................................................................................

cv::Sobel(img, grad_x, CV_16S, 1, 0,3,1,0, cv::BORDER_DEFAULT);
 convertScaleAbs( grad_x, abs_grad_x );

cv::Sobel(img, grad_y, CV_16S, 0, 1,3,1,0, cv::BORDER_DEFAULT);
 convertScaleAbs( grad_y, abs_grad_y );

 addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, img_sobel );

cv::imwrite("Img2.jpg",img_sobel);

//Threshold the image...................................................................................................................

cv::Mat Thresh_img;
threshold(img_sobel, Thresh_img, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
//imshow("Threshold", Thresh_img);

//Morphological close operation applied................................................................................................

cv::Mat element1=cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::morphologyEx(Thresh_img,Thresh_img,CV_MOP_CLOSE,element1);
cv::imwrite("Close1.jpg",Thresh_img);
//cv::waitKey(5000);

//find Contours of whole image......................................................................................................

std::vector <std::vector<cv::Point>> contours;
cv::findContours(Thresh_img, contours,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);
//cv::drawContours(image,contours,-1,cv::Scalar(0,0,255),3);

cv::imwrite("Contours1.jpg",image);
std::vector <std::vector<cv::Point>>::iterator itc= contours.begin();
std::vector <cv::RotatedRect> rects;
 //vector<vector<Point> > contours_poly(rects.size());
    //vector<Rect> boundRect(rects.size());

//Remove patch not inside the limits of aspect ratio and area..................................................................................

while (itc!=contours.end()) {
cv::RotatedRect mr= cv::minAreaRect(cv::Mat(*itc));

if( !verifySizes(mr))
   { itc= contours.erase(itc); 
}else { 
    ++itc; 
   rects.push_back(mr);
  } 
}
cv::Mat drawing;
 vector<vector<cv::Point> > contours_poly(rects.size());
    vector<cv::Rect> boundRect(rects.size());
//Draw contours
    cv::Mat output;
    image.copyTo(output);
    for(int i=0;i<rects.size();i++)
 {
    approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 10, true);
    boundRect[i] = cv::boundingRect(cv::Mat(contours_poly[i]));
}
//cv::imwrite("Contours.jpg", output);
        for (int i = 0; i < rects.size(); i++)
    {
        drawContours(output, contours_poly, i, CV_RGB(255, 255, 255), 1, 8, vector<cv::Vec4i>(), 0, cv::Point());
        //rectangle(output, boundRect[i].tl(), boundRect[i].br(), CV_RGB(0, 255, 0), 3, 8, 0); 
 }
 cv::imwrite("drawing1.jpg",output);
}
bool verifySizes(cv::RotatedRect mr){

    float error=0.4;
    //Set a min and max area. All other patches are discarded
    int min= 5; // minimum area
    int max=1000;  // maximum area

    //Get only patches that match
    float rmin= 1;
    float rmax= 10;

    int area= mr.size.height * mr.size.width;
    float r= (float)mr.size.width / (float)mr.size.height;
    if(r<1)
        r= (float)mr.size.height / (float)mr.size.width;

    if(( area < min || area > max ) || ( r < rmin || r > rmax )){
        return false;
    }else{
        return true;
    }

}

我已经执行了sobel过滤器,阈值(OTSU +二进制),形态操作CLOSE,findContours(),删除了一个不在范围内的区域和宽高比以及图像上的近似值This is my input image

This is approxPolyDP image

问题是输出图像没有在牌照周围形成矩形。任何人都可以告诉代码中有什么问题,以及如何进一步自动查找大量图像中的牌照?我很困惑。

谢谢

0 个答案:

没有答案