OpenCV& c ++频道问题。合并RGB和灰度图像。

时间:2017-03-04 15:55:32

标签: c++ opencv rgb grayscale bgr

基本上,我正在尝试从网络摄像头读取实时源,将此源分成10x10(总共100个)块的网格,并以随机顺序重新构建视频帧。使用构成帧的这个新的块顺序,我试图根据其位置将效果应用于每个块(例如:灰度,高斯模糊等)。以上是可以的。我遇到的问题来自尝试使用RGB和灰度的块重建帧,其中一个有三个通道,另一个有一个,给出以下错误:

错误:(-215)channels()== CV_MAT_CN(dtype)函数copyTo

我之前和过去都遇到过这种情况,通过复制单个灰度通道三次并合并在一起来解决它。但这一次似乎并没有解决问题。我还应该指出,我对OpenCV很陌生,所以我可能会把一些微不足道的东西搞糊涂。我希望问题出现在switch语句的第二种情况中:

#include <imgproc_c.h>
#include <iostream>

using namespace cv;

int main( int argc, char** argv ) {
    namedWindow( "Example2_10", WINDOW_AUTOSIZE );
    VideoCapture cap;
    if (argc==1) {
        cap.open(-1); //0
     } else {
        cap.open(argv[1]);
     }
     if( !cap.isOpened() ) {  // check if we succeeded
          std::cerr << "Couldn't open capture." << std::endl;
          return -1;
     }

     int frameWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
     int frameHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
     cv::Size sb_size(frameWidth/10, frameHeight/10);

     Mat frame, img_gry, img_gry_color, img_cny_color, img_cny, img_join,     img_flip, temp, img_effect;
     vector<Mat> img_gry_vec;
     vector<Mat> img_cny_vec;

     int n_cols = 10;
     int n_rows = 10;

     //Create multidimensional array of key pair values for positional data
     std::pair<int, int> posIndex[100];

     //Populate positional array with positions of every possible frame block
     int counter = 0;
     for(int i = 0; i < frameWidth; i += sb_size.width){
         for(int j = 0; j < frameHeight; j += sb_size.height){
             posIndex[counter] = std::make_pair(i, j);
             counter++;
          }
       }

    //Random shuffle positional array so as to provide random structure for compilation
    std::random_shuffle(&posIndex[0], &posIndex[100]);

    for(;;) {
      cap >> frame;
      if( frame.empty() )
      break;

      //Create empty grid to be populated with blocks
      Mat3b grid(n_rows * sb_size.height, n_cols * sb_size.width, Vec3b(0,0,0));

    //Initialise row/column  and effect counters.
    int rowCounter = 0;
    int colCounter = 0;
    int blockIndex = 0;
    int effectIndex = 0;

    for(int i = 0; i < frameWidth; i += sb_size.width){//iterate columns
         colCounter = 0; //Reset column number on new row
         rowCounter++; //Count row number
         effectIndex++; //reset effect counter to equal row number on new row.

        if(effectIndex == 6){
            effectIndex = 1;
        }

        for(int j = 0; j < frameHeight; j += sb_size.height){//iterate rows
           colCounter++; //Count column number: 0 - 9
           blockIndex++; //Count block index: 0 - 99

           //Get block position from shuffled positional array to temporary storage.
           temp = Mat(frame, Rect(posIndex[blockIndex].first, posIndex[blockIndex].second, sb_size.width, sb_size.height)).clone();
           Rect roi(i, j, sb_size.width, sb_size.height);//Get region of interest

           effectIndex++;
           //if effect index reaches five, reset to one
           if(effectIndex == 6){
              effectIndex = 1;
           }

           //Determine which effect to apply based on effect index
           switch(effectIndex){
              case 1:
                  //Copy to grid at correct roi with no effect applied.
                  temp.copyTo(grid(roi));
              break;

              case 2:
                  cvtColor(temp, img_gry, COLOR_BGR2GRAY);
                  img_gry_vec.push_back(img_gry);
                  img_gry_vec.push_back(img_gry);
                  img_gry_vec.push_back(img_gry);
                  cv::merge(img_gry_vec, img_gry_color);
                  img_gry_color.copyTo(grid(roi));
              break;

              case 3:
                  temp.copyTo(grid(roi));
              break;

              case 4:
                   GaussianBlur( temp, img_effect, Size(5, 5), 3, 3 );
                  img_effect.copyTo(grid(roi));
              break;

              case 5:
                  temp.copyTo(grid(roi));
              break;
          }
      }
   }

   imshow( "Example3", grid);

   img_gry_vec.clear();

   if( cv::waitKey(33) >= 0 )
      break;
    }
  return 0;
}

程序按预期运行,但未应用灰度。非常感谢任何帮助或指示。

0 个答案:

没有答案
相关问题