textdetection.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * textdetection.cpp
  3. *
  4. * A demo program of the Extremal Region Filter algorithm described in
  5. * Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012
  6. *
  7. * Created on: Sep 23, 2013
  8. * Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
  9. */
  10. #include "opencv2/text.hpp"
  11. #include "opencv2/highgui.hpp"
  12. #include "opencv2/imgproc.hpp"
  13. #include <vector>
  14. #include <iostream>
  15. #include <iomanip>
  16. using namespace std;
  17. using namespace cv;
  18. using namespace cv::text;
  19. void show_help_and_exit(const char *cmd);
  20. void groups_draw(Mat &src, vector<Rect> &groups);
  21. void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions);
  22. int main(int argc, const char * argv[])
  23. {
  24. cout << endl << argv[0] << endl << endl;
  25. cout << "Demo program of the Extremal Region Filter algorithm described in " << endl;
  26. cout << "Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012" << endl << endl;
  27. if (argc < 2) show_help_and_exit(argv[0]);
  28. Mat src = imread(argv[1]);
  29. // Extract channels to be processed individually
  30. vector<Mat> channels;
  31. computeNMChannels(src, channels);
  32. int cn = (int)channels.size();
  33. // Append negative channels to detect ER- (bright regions over dark background)
  34. for (int c = 0; c < cn-1; c++)
  35. channels.push_back(255-channels[c]);
  36. // Create ERFilter objects with the 1st and 2nd stage default classifiers
  37. Ptr<ERFilter> er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),16,0.00015f,0.13f,0.2f,true,0.1f);
  38. Ptr<ERFilter> er_filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);
  39. vector<vector<ERStat> > regions(channels.size());
  40. // Apply the default cascade classifier to each independent channel (could be done in parallel)
  41. cout << "Extracting Class Specific Extremal Regions from " << (int)channels.size() << " channels ..." << endl;
  42. cout << " (...) this may take a while (...)" << endl << endl;
  43. for (int c=0; c<(int)channels.size(); c++)
  44. {
  45. er_filter1->run(channels[c], regions[c]);
  46. er_filter2->run(channels[c], regions[c]);
  47. }
  48. // Detect character groups
  49. cout << "Grouping extracted ERs ... ";
  50. vector< vector<Vec2i> > region_groups;
  51. vector<Rect> groups_boxes;
  52. erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_HORIZ);
  53. //erGrouping(src, channels, regions, region_groups, groups_boxes, ERGROUPING_ORIENTATION_ANY, "./trained_classifier_erGrouping.xml", 0.5);
  54. // draw groups
  55. groups_draw(src, groups_boxes);
  56. imshow("grouping",src);
  57. cout << "Done!" << endl << endl;
  58. cout << "Press 'space' to show the extracted Extremal Regions, any other key to exit." << endl << endl;
  59. if ((waitKey()&0xff) == ' ')
  60. er_show(channels,regions);
  61. // memory clean-up
  62. er_filter1.release();
  63. er_filter2.release();
  64. regions.clear();
  65. if (!groups_boxes.empty())
  66. {
  67. groups_boxes.clear();
  68. }
  69. }
  70. // helper functions
  71. void show_help_and_exit(const char *cmd)
  72. {
  73. cout << " Usage: " << cmd << " <input_image> " << endl;
  74. cout << " Default classifier files (trained_classifierNM*.xml) must be in current directory" << endl << endl;
  75. exit(-1);
  76. }
  77. void groups_draw(Mat &src, vector<Rect> &groups)
  78. {
  79. for (int i=(int)groups.size()-1; i>=0; i--)
  80. {
  81. if (src.type() == CV_8UC3)
  82. rectangle(src,groups.at(i).tl(),groups.at(i).br(),Scalar( 0, 255, 255 ), 3, 8 );
  83. else
  84. rectangle(src,groups.at(i).tl(),groups.at(i).br(),Scalar( 255 ), 3, 8 );
  85. }
  86. }
  87. void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions)
  88. {
  89. for (int c=0; c<(int)channels.size(); c++)
  90. {
  91. Mat dst = Mat::zeros(channels[0].rows+2,channels[0].cols+2,CV_8UC1);
  92. for (int r=0; r<(int)regions[c].size(); r++)
  93. {
  94. ERStat er = regions[c][r];
  95. if (er.parent != NULL) // deprecate the root region
  96. {
  97. int newMaskVal = 255;
  98. int flags = 4 + (newMaskVal << 8) + FLOODFILL_FIXED_RANGE + FLOODFILL_MASK_ONLY;
  99. floodFill(channels[c],dst,Point(er.pixel%channels[c].cols,er.pixel/channels[c].cols),
  100. Scalar(255),0,Scalar(er.level),Scalar(0),flags);
  101. }
  102. }
  103. char buff[20]; char *buff_ptr = buff;
  104. sprintf(buff, "channel %d", c);
  105. imshow(buff_ptr, dst);
  106. }
  107. waitKey(-1);
  108. }