创造一箱车板

时间:2016-04-05 10:44:57

标签: c++ opencv

我有一个程序可以创建车牌。

输入1:enter image description here 输出1:enter image description here

输入2:enter image description here 输出2:enter image description here

代码:

#include <cv.h>         // open cv general include file
#include <highgui.h>    // open cv GUI include file
#include <iostream>     // standard C++ I/O
#include <vector>
#include <list>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <regex>
#include <string>
#include <fstream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{

  Mat img;
  int largest_area=0;
  int largest_contour_index=0;
  Rect bounding_rect;


  img = imread( argv[1], 1 );

  Mat tmp = img;

  cvtColor( img, img, CV_BGR2GRAY );
  GaussianBlur(img, img, Size(5,5), 0);
  threshold(img, img, 0, 255, CV_THRESH_OTSU);

  Mat temp(img.rows,img.cols,CV_8UC1);
  Mat dst(img.rows,img.cols,CV_8UC1,Scalar::all(0));
  img.copyTo(temp);

  vector<vector<Point>> contours; // storing contour
  vector<Vec4i> hierarchy;

  findContours( temp, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

  for( int i = 0; i< contours.size(); i++ ) // iterate
  {
    double a=contourArea( contours[i],false);  //Find the largest area of contour
    if(a>largest_area)
    {
        largest_area=a;
        largest_contour_index=i;
        bounding_rect=boundingRect(contours[largest_contour_index]); // Find the bounding rectangle for biggest contour

    }
  }

  drawContours( dst, contours,largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy );


  rectangle(tmp, bounding_rect,  Scalar(255,255,255),1, 8,0);

  Mat roi = tmp(bounding_rect);

  imshow("Final", roi);


  waitKey();
  return(0);

}

你知道如何比较车牌是否笔直?或者另一种创建这些盒子的方法?

这是我的测试集:http://www.filedropper.com/carboxtar

0 个答案:

没有答案