基本上,我正在尝试从网络摄像头读取实时提要,将此提要拆分为 10x10(总共 100 个)块的网格,并以随机顺序重新构建视频帧。通过这种新的块顺序构成一帧,我试图根据每个块的位置(例如:灰度、高斯模糊等)对每个块应用效果。以上没问题。我遇到的问题来自尝试使用 RGB 和灰度的块重建框架,因为一个具有三个通道,另一个具有一个,给出以下错误:
错误:(-215)通道()==函数copyTo中的CV_MAT_CN(dtype)
我之前遇到过这种情况,过去通过将单个灰度通道复制三遍并合并在一起来解决它。然而,这一次似乎并没有解决问题。我还应该指出我对 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;
}
该程序在没有应用灰度的情况下按预期运行。非常感谢任何帮助或指示。