简体   繁体   English

OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型)

[英]OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat

When I am trying to comment out the cvtColor function from my code I am getting the following error: "OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat" 当我试图从我的代码中注释掉cvtColor函数时,我收到以下错误:“cvGetMat中的OpenCV错误:错误标志(参数或结构字段)(无法识别或不支持的数组类型)”

If I am using the cvtColor function then the program runs just fine. 如果我使用cvtColor函数,那么程序运行正常。

I am trying to comment it out because I want the original color frame and not grayed out. 我试图评论它,因为我想要原始的颜色框架而不是灰色。 Here is my code: 这是我的代码:

#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;

}

The error is because, if you comment out that line: 该错误是因为,如果您注释掉该行:

  1. you never assign a value to gray_diff , so the following threshold function will work on an invalid matrix, and 你永远不会给gray_diff ,所以下面的threshold函数将对无效矩阵起作用,并且

  2. when you compute the absdiff , frame and prev_frame are of type CV_8UC3 , and so will be also the result frame_diff . 当你计算absdiffframeprev_frameCV_8UC3类型CV_8UC3 ,结果也是frame_diff But threshold expects an input of type CV_8UC1 . 但是threshold需要CV_8UC1类型的输入。 So you need to convert frame_diff to single channel gray_diff in order to work on it with threshold . 因此,您需要将frame_diff转换为单通道gray_diff ,以便使用threshold

So, here you need to use cvtColor , and I don't see any reason why you want to remove it. 所以,在这里你需要使用cvtColor ,我没有看到任何你想要删除它的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM