pagenormalizer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "pagenormalizer.h"
  2. #include <iostream>
  3. #include <fstream>
  4. PageNormalizer::PageNormalizer() {
  5. width = height = 0;
  6. m_quiet = false;
  7. }
  8. PageNormalizer::PageNormalizer(const std::string inFile,const std::string outFile,const std::string whiteRef,const std::string blackRef,const std::string settingsFile, const bool quiet, const int rotate ) {
  9. m_quiet = quiet;
  10. // Read settings from text-file
  11. ReadSettings( settingsFile.c_str() );
  12. inImage = CImg<>( inFile.c_str() ); // Input image (Not manipulated)
  13. refImages[0] = CImg<>( whiteRef.c_str() ); // White
  14. refImages[1] = CImg<>( blackRef.c_str() ); // Black
  15. width = inImage.width();
  16. height = inImage.height();
  17. if( !m_quiet ) printf("Crop:%d,%d-%d,%d\n",cropX0,cropY0,cropX1,cropY1);
  18. outImage = CImg<>(cropX1-cropX0,cropY1-cropY0,1,3);
  19. NormalizeImage();
  20. RotateImage(rotate);
  21. outImage.save_bmp( outFile.c_str() );
  22. }
  23. void PageNormalizer::RotateImage(const int rotAngle) {
  24. if( rotAngle != 0 ) {
  25. printf("Rotating %d degrees\n",rotAngle);
  26. outImage.rotate(rotAngle);
  27. }
  28. }
  29. void PageNormalizer::NormalizeImage()
  30. {
  31. for(int y=cropY0;y<cropY1;y++)
  32. {
  33. for(int x=cropX0; x<cropX1;x++) {
  34. int wh = GetWhiteness(x,y);
  35. int bh = GetBlackness(x,y);
  36. double whiteFactor = 255.0 / ( wh - bh );
  37. int ch0 = ( inImage(x,y,0,0) - bh ) * whiteFactor;
  38. int ch1 = ( inImage(x,y,0,1) - bh ) * whiteFactor;
  39. int ch2 = ( inImage(x,y,0,2) - bh ) * whiteFactor;
  40. if( ch0 < 0 ) ch0=0; if( ch1 < 0 ) ch1=0; if( ch2 < 0 ) ch2=0;
  41. if( ch0 > 255 ) ch0=255; if( ch1 > 255 ) ch1=255; if( ch2 > 255 ) ch2=255;
  42. outImage(x-cropX0,y-cropY0,0,0) = (unsigned char) ch0;
  43. outImage(x-cropX0,y-cropY0,0,1) = (unsigned char) ch1;
  44. outImage(x-cropX0,y-cropY0,0,2) = (unsigned char) ch2;
  45. }
  46. }
  47. }
  48. int PageNormalizer::GetWhiteness(unsigned int x,unsigned int y) {
  49. return( refImages[0](x,y,0,0) );
  50. }
  51. int PageNormalizer::GetBlackness(unsigned int x,unsigned int y) {
  52. return( refImages[1](x,y,0,0) );
  53. }
  54. void PageNormalizer::ReadSettings(const std::string inFile)
  55. {
  56. if( !m_quiet ) std::cout << "Reading settings from: " << inFile << std::endl;
  57. std::string line;
  58. std::ifstream myfile( inFile.c_str() );
  59. if (myfile.is_open())
  60. {
  61. auto rowNum = -1;
  62. while( std::getline( myfile,line ) )
  63. {
  64. switch( rowNum ) {
  65. case -1: if( line.find("START:") == 0 ) rowNum = 0; break;
  66. case 1: cropX0 = std::stoi(line);break;
  67. case 2: cropY0 = std::stoi(line);break;
  68. case 3: cropX1 = std::stoi(line);break;
  69. case 4: cropY1 = std::stoi(line);break;
  70. }
  71. if( rowNum >= 0 ) rowNum++;
  72. }
  73. myfile.close();
  74. }
  75. else std::cout << "Unable to open file";
  76. }