Advertisement
dan-masek

Untitled

Jun 6th, 2019
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. void splitTo3Channel_dan1(const cv::Mat& input, std::vector<cv::Mat>& output)
  2. {
  3.     CV_Assert(!input.empty());
  4.     CV_Assert(input.channels() == 3);
  5.  
  6.     // (re)allocate outputs
  7.     output.clear();
  8.     for (int i(0); i < input.channels(); ++i) {
  9.         output.push_back(cv::Mat::zeros(input.size(), input.type()));
  10.     }
  11.  
  12.     // Set up index pairs (input->output);
  13.     int from_to[] = { 0,0, 1,4, 2,8 };
  14.     cv::mixChannels(&input, 1, &output[0], 3, from_to, 3);
  15. }
  16.  
  17. void splitTo3Channel_dan1b(const cv::Mat& input, std::vector<cv::Mat>& output)
  18. {
  19.     CV_Assert(!input.empty());
  20.     CV_Assert(input.channels() == 3);
  21.  
  22.     // (re)allocate outputs
  23.     output.clear();
  24.     for (int i(0); i < input.channels(); ++i) {
  25.         output.push_back(cv::Mat::zeros(input.size(), input.type()));
  26.     }
  27.  
  28.     // Set up index pairs (input->output);
  29.     std::vector<int> from_to{ 0,0, 1,4, 2,8 };
  30.     CV_Assert(from_to.size() == (output.size() * 2));
  31.     cv::mixChannels(&input, 1
  32.         , &output[0], output.size()
  33.         , &from_to[0], output.size());
  34. }
  35.  
  36. void splitTo3Channel_dan1c(const cv::Mat& input, std::vector<cv::Mat>& output)
  37. {
  38.     CV_Assert(!input.empty());
  39.  
  40.     // (re)allocate outputs
  41.     output.clear();
  42.     for (int i(0); i < input.channels(); ++i) {
  43.         output.push_back(cv::Mat::zeros(input.size(), input.type()));
  44.     }
  45.  
  46.     // Set up channel index pairs (input->output);
  47.     std::vector<int> from_to;
  48.     for (int i(0); i < input.channels(); ++i) {
  49.         from_to.push_back(i); // Input
  50.         from_to.push_back(i * input.channels() + i); // Output
  51.     }
  52.  
  53.     cv::mixChannels(&input, 1
  54.         , &output[0], output.size()
  55.         , &from_to[0], output.size());
  56. }
  57.  
  58. void splitToNChannel_dan2(const cv::Mat& input
  59.     , std::vector<cv::Mat>& output
  60.     , bool reuse_output = false)
  61. {
  62.     CV_Assert(!input.empty());
  63.     CV_Assert(input.channels() <= 4); // Not tested for more than 4
  64.     CV_Assert(output.empty() || (output.size() == input.channels()));
  65.  
  66.     // Allocate (or just clear if reusing/possible to reuse) outputs
  67.     if (reuse_output && (output.size() == input.channels())) {
  68.         for (int i(0); i < input.channels(); ++i) {
  69.             if ((output[i].size() == input.size()) && (output[i].type() == input.type())) {
  70.                 output[i].setTo(cv::Scalar());
  71.             } else {
  72.                 output[i] = cv::Mat::zeros(input.size(), input.type());
  73.             }
  74.         }
  75.     } else {
  76.         output.clear();
  77.         for (int i(0); i < input.channels(); ++i) {
  78.             output.push_back(cv::Mat::zeros(input.size(), input.type()));
  79.         }
  80.     }
  81.  
  82.     // Set up channel index pairs (input->output);
  83.     std::vector<int> from_to;
  84.     for (int i(0); i < input.channels(); ++i) {
  85.         from_to.push_back(i); // Input
  86.         from_to.push_back(i * input.channels() + i); // Output
  87.     }
  88.  
  89.     cv::mixChannels(&input, 1
  90.         , &output[0], output.size()
  91.         , &from_to[0], output.size());
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement