Guest User

Untitled

a guest
Apr 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. int COCR::method_gradient(int nonGradient, int showOutput)
  2. {
  3. Mat large = imread(INPUT_FILE);
  4. Mat rgb;
  5.  
  6. rgb = large; //--> change
  7.  
  8. Mat small;
  9. cvtColor(rgb, small, CV_BGR2GRAY);
  10.  
  11. // morphological gradient
  12. Mat grad;
  13. Mat morphKernel;
  14.  
  15. if (!nonGradient)
  16. {
  17. morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
  18. morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
  19. }
  20. else
  21. grad = small;
  22.  
  23.  
  24. // binarize
  25. Mat bw;
  26. threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
  27.  
  28. // connect horizontally oriented regions
  29. Mat connected;
  30. morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
  31. morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
  32.  
  33. // find contours
  34. Mat mask = Mat::zeros(bw.size(), CV_8UC1);
  35. vector<vector<Point>> contours;
  36. vector<Vec4i> hierarchy;
  37. findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
  38.  
  39. if (nonGradient)
  40. rgb = MatGradient;
  41.  
  42. // filter contours
  43. for (int idx = 0; idx >= 0; idx = hierarchy[idx][0])
  44. {
  45. Rect rect = boundingRect(contours[idx]);
  46. Mat maskROI(mask, rect);
  47. maskROI = Scalar(0, 0, 0);
  48.  
  49.  
  50. // fill the contour
  51. drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
  52.  
  53. // ratio of non-zero pixels in the filled region
  54. double r = (double)countNonZero(maskROI) / (rect.width*rect.height);
  55.  
  56. if (r > .25 /* assume at least 25% of the area is filled if it contains text */
  57. &&
  58. (rect.height > 8 && rect.width > 8)
  59. )
  60. {
  61. rectangle(rgb, rect, Scalar(0, 255, 0), 2);
  62. }
  63. }
  64.  
  65. if (!nonGradient)
  66. MatGradient = rgb;
  67.  
  68. if (showOutput)
  69. imwrite(OUTPUT_FOLDER_PATH, rgb);
  70.  
  71. return 0;
  72. }
  73.  
  74. // calling
  75. COCR obj_ocr;
  76. obj_ocr.method_gradient(0, 0);
  77. obj_ocr.method_gradient(1, 1);
Add Comment
Please, Sign In to add comment