C++ OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型)
当我试图从代码中注释掉cvtColor函数时,出现以下错误: “OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型)” 如果我使用的是cvtColor函数,那么程序运行正常 我试图注释它,因为我想要原始的颜色框架,而不是灰色。 这是我的密码:C++ OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型),c++,opencv,visualization,C++,Opencv,Visualization,当我试图从代码中注释掉cvtColor函数时,出现以下错误: “OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型)” 如果我使用的是cvtColor函数,那么程序运行正常 我试图注释它,因为我想要原始的颜色框架,而不是灰色。 这是我的密码: #include <stdio.h> #include <cv.h> #include <highgui.h> #include "opencv2/highgui/highgui
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/video/tracking.hpp"
#include <math.h>
#include <time.h>
using namespace cv;
using namespace std;
float MHI_DURATION = 0.05;
int DEFAULT_THRESHOLD = 10;
void draw_motion_comp(Mat& img, int x_coordinate, int y_coordinate, int width, int height);
Mat frame;
int main(int argc, char** argv)
{
namedWindow("Motion_tracking",CV_WINDOW_AUTOSIZE);
char fileName[100] = "C:\\Users\\survya\\Downloads/VASTChallenge2009-M3-VIDEOPART1 (1).mov";
VideoCapture cap(fileName);
if ( !cap.read(frame) ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
Mat frame,ret,frame_diff,gray_diff,motion_mask;
for(int i = 0; i<10; i++)
{
cap.read(frame);
Size frame_size = frame.size();
int h = frame_size.height;
int w = frame_size.width;
if(i==5)
break;
}
ret = frame.clone();
Size frame_size = frame.size();
int h = frame_size.height;
int w = frame_size.width;
Mat prev_frame = frame.clone();
Mat motion_history(h,w, CV_32FC1,Scalar(0,0,0));
Mat seg_mask(h,w, CV_32FC1,Scalar(0,0,0));
vector<Rect> seg_bounds;
Mat vis(h,w,CV_32FC3);
Mat vis1(h,w,CV_8UC1);
while(1)
{
cap.retrieve(frame);
cap.read(frame);
ret = frame.clone();
if (!ret.data) //if not success, break loop
{
cout << "video ended" << endl;
break;
}
absdiff(frame, prev_frame, frame_diff);
//cvtColor(frame_diff,gray_diff, CV_BGR2GRAY); //want to comment this line out but getting the error stated in my question
threshold(gray_diff,ret,DEFAULT_THRESHOLD,255,0);
motion_mask = ret.clone();
double timestamp = 1000.0*clock()/CLOCKS_PER_SEC;
updateMotionHistory(motion_mask, motion_history, timestamp, MHI_DURATION);
segmentMotion(motion_history, seg_mask, seg_bounds, timestamp, 32);
vis = frame.clone();
vis = frame_diff.clone();
for(int i=0; i< motion_history.cols; i++)
{
for(int j=0; j< motion_history.rows ; j++)
{
float a = motion_history.at<float>(j,i);
if((a-timestamp-MHI_DURATION)/MHI_DURATION <= -5)
vis1.at<uchar>(j,i) = 0;
else
vis1.at<uchar>(j,i) = (a-timestamp-MHI_DURATION)/MHI_DURATION;
}
}
cvtColor(vis1,vis,COLOR_GRAY2BGR);
for(unsigned int h = 0; h < seg_bounds.size(); h++)
{
Rect rec = seg_bounds[h];
if(rec.area() > 5000 && rec.area() < 70000)
{
rectangle(vis, rec,Scalar(0,0,255),2);
}
}
imshow("Motion_tracking",vis);
prev_frame = frame.clone();
if(waitKey(30) >= 0) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
#包括
#包括
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
#包括“opencv2/video/tracking.hpp”
#包括
#包括
使用名称空间cv;
使用名称空间std;
浮动MHI_持续时间=0.05;
int DEFAULT_THRESHOLD=10;
无效绘制运动补偿(Mat和img、int x坐标、int y坐标、int宽度、int高度);
垫架;
int main(int argc,字符**argv)
{
namedWindow(“运动跟踪”,CV窗口自动调整大小);
char fileName[100]=“C:\\Users\\survya\\Downloads/VASTChallenge2009-M3-VIDEOPART1(1).mov”;
视频捕获cap(文件名);
如果(!cap.read(frame))//如果不成功,退出程序
{
cout错误是因为,如果您注释掉该行:
您从未将值分配给gray_diff
,因此以下threshold
函数将在无效矩阵上工作,并且
当您计算absdiff
时,frame
和prev\u frame
的类型为CV\u 8UC3
,结果也是frame\u diff
。但是threshold
要求输入类型为CV\u 8UC1
。因此您需要将frame\u diff
转换为单通道gray\u diff
要使用阈值处理它
因此,这里您需要使用cvtColor
,我看不出您想删除它的任何原因。我想删除它,因为我想让代码在原始彩色视频上运行,而不是在黑白视频上运行。如果必须使用cvtColor,您知道如何做到吗?您正在处理彩色视频。但是,差异掩码是d应该是一个单通道图像,因为根据定义,掩码是一个二进制图像。很难理解您想要做什么,而且它可能太宽,无法在这里讨论。这个答案解决了问题中提出的问题。