opencv代码中的断言失败错误

时间:2013-11-19 06:38:07

标签: c++ opencv

我正在尝试计算直方图帧的HSV直方图。我的代码给出了Assertion失败错误。错误在函数hsv_histogram中。如果我为2维直方图做它,它对我来说很完美,但是当我添加第三维值时,它给出了断言失败错误。

    // newproject.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

class frameprocessing{

Mat hsv_base;
Mat hist_base;

public:
    void hsv_histogram(Mat Frame)
    {
        cvtColor( Frame, hsv_base, CV_BGR2HSV );
        int h_bins = 50; 
        int s_bins = 32;
        int v_bins = 10;

        int histSize[] = { h_bins, s_bins };

        float h_ranges[] = { 0, 256 };
        float s_ranges[] = { 0, 180 };
        float v_ranges[] = { 0, 256 };

        const float* ranges[] = { h_ranges, s_ranges ,v_ranges};
        int channels[] = { 0, 1 ,2};
        calcHist( &hsv_base, 1, channels, Mat(), hist_base, 3, histSize, ranges, true, false );
    }
};

class video{    

    Mat frame;
    string filename;
    double dWidth;
    double dHeight;

public:
    video()
    {

    }

    video(string videoname)
    {
        vector<Mat> videoframes;
        filename = videoname;
        VideoCapture capture(filename); 

        if( !capture.isOpened() )
        {
            exit(0);
        }

        dWidth   = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
        dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
        frameprocessing obj;

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

    //      Mat tmp=frame.clone();
            obj.hsv_histogram(frame);
    //      videoframes.push_back(tmp);
        }
        //displayvideo(videoframes);
        //writer(videoframes);
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    video obj("video.avi");
}

1 个答案:

答案 0 :(得分:1)

你可能忘了设置v hist size?

我想那个

int histSize[] = { h_bins, s_bins };

应该是

int histSize[] = { h_bins, s_bins, v_bins };