Advertisement
dan-masek

Untitled

Mar 1st, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2.  
  3. #include <cstdint>
  4.  
  5. enum
  6. {
  7.     BAT_MODE_FLOOR = 0
  8.     , BAT_MODE_CEIL = 1
  9.     , BAT_MODE_INVALID
  10. };
  11.  
  12. cv::Mat bilateral_adaptive_threshold(cv::Mat const& input
  13.     , int32_t kernel_size = 20
  14.     , int32_t C = 0
  15.     , int32_t mode = BAT_MODE_FLOOR
  16.     , uint8_t true_value = 255
  17.     , uint8_t false_value = 0)
  18. {
  19.     CV_Assert(input.channels() == 1);
  20.     CV_Assert(input.type() == CV_8UC1);
  21.     CV_Assert(kernel_size > 0);
  22.     CV_Assert(mode < BAT_MODE_INVALID);
  23.  
  24.     // Prepare kernels
  25.  
  26.     cv::Mat kernel_left(cv::Mat::ones(1, kernel_size + 1, CV_16S));
  27.     kernel_left.at<int16_t>(0, kernel_size) = -kernel_size;
  28.  
  29.     cv::Mat kernel_right(cv::Mat::ones(1, kernel_size + 1, CV_16S));
  30.     kernel_right.at<int16_t>(0, 0) = -kernel_size;
  31.  
  32.     cv::Mat kernel_up(cv::Mat::ones(kernel_size + 1, 1, CV_16S));
  33.     kernel_up.at<int16_t>(kernel_size, 0) = -kernel_size;
  34.  
  35.     cv::Mat kernel_down(cv::Mat::ones(kernel_size + 1, 1, CV_16S));
  36.     kernel_down.at<int16_t>(0, 0) = -kernel_size;
  37.  
  38.     double delta(0.0);
  39.     switch (mode) {
  40.     case BAT_MODE_FLOOR: delta = C; break;
  41.     case BAT_MODE_CEIL: delta = -C; break;
  42.     default: break;
  43.     };
  44.  
  45.     cv::Mat left_thresh, right_thresh, up_thresh, down_thresh;
  46.     cv::filter2D(input, left_thresh, CV_16S, kernel_left
  47.         , cv::Point(kernel_size, 0), delta, cv::BORDER_CONSTANT);
  48.     cv::filter2D(input, right_thresh, CV_16S, kernel_right
  49.         , cv::Point(0, 0), delta, cv::BORDER_CONSTANT);
  50.     cv::filter2D(input, up_thresh, CV_16S, kernel_up
  51.         , cv::Point(0, kernel_size), delta, cv::BORDER_CONSTANT);
  52.     cv::filter2D(input, down_thresh, CV_16S, kernel_down
  53.         , cv::Point(0, 0), delta, cv::BORDER_CONSTANT);
  54.  
  55.     cv::Mat result(input.rows, input.cols, input.type(), cv::Scalar(false_value));
  56.     cv::Mat mask;
  57.     if (mode == BAT_MODE_FLOOR) {
  58.         mask = ((0 > left_thresh) & (0 > right_thresh))
  59.             | ((0 > up_thresh) & (0 > down_thresh));
  60.     } else if (mode == BAT_MODE_CEIL) {
  61.         mask = ((0 < left_thresh) & (0 < right_thresh))
  62.             | ((0 < up_thresh) & (0 < down_thresh));
  63.     }
  64.     result.setTo(true_value, mask);
  65.     return result;
  66. }
  67.  
  68. int main()
  69. {
  70.     cv::Mat img_in(cv::imread("foo_in.png", -1));
  71.     cv::Mat img_out(cv::imread("foo_out.png", -1));
  72.  
  73.     cv::Mat dummy(img_in), result;
  74.    
  75.     int64 total_t(0);
  76.     for (int i(0); i < 256; ++i) {
  77.         int64 start_t(cv::getTickCount());
  78.         result = bilateral_adaptive_threshold(img_in);
  79.         total_t += cv::getTickCount() - start_t;
  80.         dummy += result;
  81.     }
  82.    
  83.     std::cout << (total_t / (cv::getTickFrequency() * 256) * 1000.0) << "\n";
  84.  
  85.     CV_Assert(cv::countNonZero(result == img_out) == result.rows * result.cols);
  86.  
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement