Guest User

Part 2: Generating Tesseract data

a guest
Jan 13th, 2013
3,726
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ayoungprogrammer.blogspot.com
  3.  
  4. Part 2: Generating Tesseract data
  5.  
  6. */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <windows.h>
  11. #include "cv.h"
  12. #include "highgui.h"
  13.  
  14.  
  15. using namespace std;
  16. using namespace cv;
  17.  
  18.  
  19. int numOfFiles(char* searchPath)
  20. {
  21.     WIN32_FIND_DATA FindData;
  22.     HANDLE      hFiles;
  23.     LPTSTR      lptszFiles[100];
  24.     UINT        nFileCount = 0;
  25.  
  26.     hFiles = FindFirstFile(searchPath, &FindData);
  27.    
  28.     if (hFiles == INVALID_HANDLE_VALUE)
  29.         return 0;
  30.  
  31.     bool bFinished = false;
  32.     while(!bFinished){
  33.         if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
  34.             nFileCount++;
  35.         }
  36.  
  37.         if(!FindNextFile(hFiles, &FindData)){
  38.             bFinished = true;
  39.         }
  40.     }
  41.     FindClose(hFiles);
  42.     return nFileCount;
  43. }
  44.  
  45.  
  46. int main(void){
  47.  
  48.      
  49.     string fileName="mat.arial.exp0";
  50.     cout<<"Output name: ";
  51.    
  52.    
  53.     ofstream box("mat.arial.exp0.box");
  54.    
  55.     char cCurrentPath[256];
  56.     GetCurrentDirectory(sizeof(cCurrentPath),cCurrentPath );
  57.  
  58.    
  59.     string keys="()+0x123456789";
  60.     Mat tiff  = Mat::zeros(1800,1200,CV_8UC3);
  61.     Mat tiff2 = Mat::zeros(1800,1200,CV_8UC3);
  62.     for(int j=0;j<keys.length();j++){
  63.  
  64.     stringstream fileOutput;   
  65.     if(keys[j]=='|')fileOutput<<cCurrentPath<<"\\output\\"<<"abs"<<"\\";
  66.     else fileOutput<<cCurrentPath<<"\\output\\"<<keys[j]<<"\\";
  67.  
  68.     stringstream fileSearch;
  69.     fileSearch<<fileOutput.str();
  70.     fileSearch<<"*.jpg";
  71.    
  72.    
  73.     cout<<fileSearch.str()<<endl;
  74.     int n = numOfFiles((char*)fileSearch.str().c_str());
  75.     cout<<n<<endl;
  76.     float targetHeight = 50;
  77.     int colDist = 0;
  78.     for(int i=0;i<min(13,n);i++){
  79.         stringstream filePath;
  80.         filePath<<fileOutput.str();
  81.         filePath<<i<<".jpg";
  82.         Mat symbol = imread(filePath.str());
  83.  
  84.         float percent = targetHeight/symbol.size().height;
  85.         if(percent*symbol.size().width>50){
  86.             percent = targetHeight/symbol.size().width;
  87.         }
  88.         Mat resized;
  89.         resize(symbol,resized,Size(),percent,percent);
  90.         Mat small = tiff.colRange(75*i+50,50+75*i+resized.size().width).rowRange(75*j+50,75*j+resized.size().height+50);
  91.        
  92.        
  93.         resized.copyTo(small);
  94.         box<<keys[j]<<" "<<75*i+50<<" "<<tiff.size().height-(75*j+resized.size().height+50)<<" "
  95.             <<50+75*i+resized.size().width<<" "<<tiff.size().height-(75*j+50)<<" 0"<<endl;
  96.  
  97.         Rect r = Rect(75*i+50,75*j+50,resized.size().width,resized.size().height);
  98.         Mat small2 = tiff2.colRange(75*i+50,50+75*i+resized.size().width).
  99.             rowRange(75*j+50,75*j+resized.size().height+50);
  100.         resized.copyTo(small2);
  101.         rectangle( tiff2, r.tl(), r.br(),Scalar(0,255,0), 2, 8, 0 );
  102.        
  103.     }
  104.    
  105.     }
  106.     imwrite(fileName+".tif",tiff);
  107.     box.close();
  108.  
  109.    
  110.     waitKey(0);
  111.  
  112.  
  113.  
  114. }
RAW Paste Data