Advertisement
Guest User

getDateOCR.cpp

a guest
May 16th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "tesseract/baseapi.h"
  4. #include "iostream"
  5.  
  6. using namespace cv;
  7. using namespace std;
  8.  
  9. static void help()
  10. {
  11.     cout << "\nThis program demonstrates circle finding with the Hough transform.\n"
  12.             "Usage:\n"
  13.             "./houghcircles <image_name>, Default is pic1.png\n" << endl;
  14. }
  15.  
  16. /**
  17.  * Rotate an image
  18.  */
  19. void rotate(cv::Mat& src, double angle, cv::Mat& dst)
  20. {
  21.     int len = std::max(src.cols, src.rows);
  22.     cv::Point2f pt(len/2., len/2.);
  23.     cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0);
  24.  
  25.     cv::warpAffine(src, dst, r, cv::Size(len, len));
  26. }
  27.  
  28. int main(int argc, char** argv)
  29. {
  30.     const char* filename = argc >= 2 ? argv[1] : "board.jpg";
  31.    
  32.  
  33.     int centerx,centery, radius;
  34.  
  35.     //Mat img = imread(filename, 0);
  36.     Mat img = imread(filename,0);
  37.  
  38.     Mat org;
  39.     img.copyTo(org);
  40.  
  41.     Mat date_img;
  42.  
  43.     if(img.empty())
  44.     {
  45.         help();
  46.         cout << "can not open " << filename << endl;
  47.         return -1;
  48.     }
  49.  
  50.     Mat cimg;
  51.     medianBlur(img, img, 5);
  52.     cvtColor(img, cimg, COLOR_GRAY2BGR);
  53.  
  54.     vector<Vec3f> circles;
  55.     HoughCircles(img, circles, HOUGH_GRADIENT, 1, 300,
  56.                  100, 30, 400, 2000 // change the last two parameters
  57.                                 // (min_radius & max_radius) to detect larger circles
  58.                  );
  59.     for( size_t i = 0; i < circles.size(); i++ )
  60.     {
  61.         Vec3i c = circles[i];
  62.         circle( cimg, Point(c[0], c[1]), c[2], Scalar(0,255,255), 3, LINE_AA);
  63.         circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, LINE_AA);
  64.  
  65.     centerx = c[0];
  66.     centery = c[1];
  67.     radius = c[2]; 
  68.  
  69.     cv::Mat mask = cv::Mat::zeros( img.rows, img.cols, CV_8UC1 );
  70.     circle( mask, Point(c[0], c[1]), c[2], Scalar(255,255,255), -1, 8, 0 ); //-1 means filled
  71.     org.copyTo( cimg, mask ); // copy values of img to dst if mask is > 0.
  72.    
  73.     break;
  74.  
  75.     }
  76.  
  77.     //cv::Mat roi( cimg, cv::Rect( centerx-radius, centery-radius, radius*2, radius*2 ) );
  78.     cout << "DIAMETER: " <<  radius  << " CENTERX: " << centerx << " CENTERY: " << centery << endl;
  79.  
  80.     cv::Rect myROI(centerx-radius,  centery-radius, radius*2,radius*2);
  81.     cimg = cimg(myROI);
  82.  
  83.  //  rotate(cimg, 90, cimg);
  84.  
  85.  
  86.  
  87.    
  88.  //   rectangle(cimg, Point(cimg.rows/1.45, cimg.cols/1.6),Point(cimg.rows/1.1,cimg.cols/1.35), Scalar(0,255,255), 3, 8, 0 );
  89.  
  90.     //get date
  91.     date_img = cimg(Rect( Point(cimg.rows/1.45, cimg.cols/1.6),Point(cimg.rows/1.1,cimg.cols/1.35) ) ) ;
  92.     date_img.convertTo(date_img, -1, 1.8, 1);
  93.  
  94.  
  95.     Mat element = getStructuringElement(MORPH_ELLIPSE, Size(15,15), Point(-1,-1) );
  96.     erode(date_img,date_img, element);
  97.     //dilate(date_img, date_img, element);
  98.     morphologyEx(date_img, date_img, MORPH_CLOSE, element);
  99.  
  100.    // medianBlur(date_img, date_img, 7);
  101.  
  102.  
  103.     // Pass it to Tesseract API
  104.     tesseract::TessBaseAPI tess;
  105.     tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
  106.     tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
  107.     tess.SetImage((uchar*)date_img.data, date_img.cols, date_img.rows, 1, date_img.cols);
  108.  
  109.     // Get the text
  110.     char* out = tess.GetUTF8Text();
  111.     std::cout << "DATE: " << out << std::endl;
  112.  
  113.  
  114.  
  115.  //   imshow("detected circles", cimg);
  116.  //   imshow("dates", date_img);
  117.  //   waitKey();
  118.  
  119.     imwrite("date_img.jpg", date_img);    
  120.  
  121.     return 0;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement