You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

niBlackThreshold.h 3.6 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Created by Jack Yu on 26/10/2017.
  3. //
  4. #ifndef HYPERPR_NIBLACKTHRESHOLD_H
  5. #define HYPERPR_NIBLACKTHRESHOLD_H
  6. #include <opencv2/opencv.hpp>
  7. using namespace cv;
  8. enum LocalBinarizationMethods {
  9. BINARIZATION_NIBLACK =
  10. 0, //!< Classic Niblack binarization. See @cite Niblack1985 .
  11. BINARIZATION_SAUVOLA = 1, //!< Sauvola's technique. See @cite Sauvola1997 .
  12. BINARIZATION_WOLF = 2, //!< Wolf's technique. See @cite Wolf2004 .
  13. BINARIZATION_NICK = 3 //!< NICK technique. See @cite Khurshid2009 .
  14. };
  15. void niBlackThreshold(InputArray _src, OutputArray _dst, double maxValue,
  16. int type, int blockSize, double k,
  17. int binarizationMethod) {
  18. // Input grayscale image
  19. Mat src = _src.getMat();
  20. CV_Assert(src.channels() == 1);
  21. CV_Assert(blockSize % 2 == 1 && blockSize > 1);
  22. if (binarizationMethod == BINARIZATION_SAUVOLA) {
  23. CV_Assert(src.depth() == CV_8U);
  24. }
  25. type &= THRESH_MASK;
  26. // Compute local threshold (T = mean + k * stddev)
  27. // using mean and standard deviation in the neighborhood of each pixel
  28. // (intermediate calculations are done with floating-point precision)
  29. Mat test;
  30. Mat thresh;
  31. {
  32. // note that: Var[X] = E[X^2] - E[X]^2
  33. Mat mean, sqmean, variance, stddev, sqrtVarianceMeanSum;
  34. double srcMin, stddevMax;
  35. boxFilter(src, mean, CV_32F, Size(blockSize, blockSize), Point(-1, -1),
  36. true, BORDER_REPLICATE);
  37. sqrBoxFilter(src, sqmean, CV_32F, Size(blockSize, blockSize), Point(-1, -1),
  38. true, BORDER_REPLICATE);
  39. variance = sqmean - mean.mul(mean);
  40. sqrt(variance, stddev);
  41. switch (binarizationMethod) {
  42. case BINARIZATION_NIBLACK:
  43. thresh = mean + stddev * static_cast<float>(k);
  44. break;
  45. case BINARIZATION_SAUVOLA:
  46. thresh = mean.mul(1. + static_cast<float>(k) * (stddev / 128.0 - 1.));
  47. break;
  48. case BINARIZATION_WOLF:
  49. minMaxIdx(src, &srcMin, NULL);
  50. minMaxIdx(stddev, NULL, &stddevMax);
  51. thresh =
  52. mean - static_cast<float>(k) *
  53. (mean - srcMin - stddev.mul(mean - srcMin) / stddevMax);
  54. break;
  55. case BINARIZATION_NICK:
  56. sqrt(variance + sqmean, sqrtVarianceMeanSum);
  57. thresh = mean + static_cast<float>(k) * sqrtVarianceMeanSum;
  58. break;
  59. default:
  60. CV_Error(CV_StsBadArg, "Unknown binarization method");
  61. break;
  62. }
  63. thresh.convertTo(thresh, src.depth());
  64. thresh.convertTo(test, src.depth());
  65. //
  66. // cv::imshow("imagex",test);
  67. // cv::waitKey(0);
  68. }
  69. // Prepare output image
  70. _dst.create(src.size(), src.type());
  71. Mat dst = _dst.getMat();
  72. CV_Assert(src.data != dst.data); // no inplace processing
  73. // Apply thresholding: ( pixel > threshold ) ? foreground : background
  74. Mat mask;
  75. switch (type) {
  76. case THRESH_BINARY: // dst = (src > thresh) ? maxval : 0
  77. case THRESH_BINARY_INV: // dst = (src > thresh) ? 0 : maxval
  78. compare(src, thresh, mask, (type == THRESH_BINARY ? CMP_GT : CMP_LE));
  79. dst.setTo(0);
  80. dst.setTo(maxValue, mask);
  81. break;
  82. case THRESH_TRUNC: // dst = (src > thresh) ? thresh : src
  83. compare(src, thresh, mask, CMP_GT);
  84. src.copyTo(dst);
  85. thresh.copyTo(dst, mask);
  86. break;
  87. case THRESH_TOZERO: // dst = (src > thresh) ? src : 0
  88. case THRESH_TOZERO_INV: // dst = (src > thresh) ? 0 : src
  89. compare(src, thresh, mask, (type == THRESH_TOZERO ? CMP_GT : CMP_LE));
  90. dst.setTo(0);
  91. src.copyTo(dst, mask);
  92. break;
  93. default:
  94. CV_Error(CV_StsBadArg, "Unknown threshold type");
  95. break;
  96. }
  97. }
  98. #endif // HYPERPR_NIBLACKTHRESHOLD_H