vector <mat>传入函数逻辑错误</mat>

时间:2013-11-21 12:19:33

标签: c++ opencv

我传递矢量从一个函数传递到另一个函数,然后传递大小返回正确的值,但在另一个函数大小中总是返回零。我在我的代码中以直方图函数传递它,并在下面给出的代码中将其作为bhattacharyya中的参数接收。请帮助我,我做错了什么。在此先感谢您的帮助。     // newproject.cpp:定义控制台应用程序的入口点。

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

vector<Mat> histograms;

class frameprocessing{

Mat hsv_base;
MatND hist_base;

public:
    void hsv_histogram(Mat Frame)
    {
        vector<Mat> Histograms;

        for(int i=0;i<Frame.cols-9;i=i+9)
            for(int j=0;j<Frame.rows-9;j=j+9)
            {
                Mat imagepart = Frame( Range( j, j+9 ), Range( i, i+9 ) );

                cvtColor( imagepart, hsv_base, CV_BGR2HSV );
                int h_bins = 8; 
                int s_bins = 8;
                int v_bins = 16;

                int histSize =h_bins+s_bins+v_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, 1, &histSize, ranges, true, false );
                Mat temp = hist_base.clone();

                Histograms.push_back(temp);
            //  double a = cvQueryHistValue_1D(hist_base, 0);
            //  imshow("Histogram",temp);

                //waitKey();
                //  Mat temp;
                //  temp = hist_base.clone();
                //  histograms.push_back(temp);

                cout<<hist_base.rows<<endl;
                //  cout<<hist_base.cols<<endl;
                //  cout<<hist_base.size<<endl;

            }

            cout<<Histograms.size()<<endl;
            bhattacharyya(Histograms);
            //getch();
    }

    void bhattacharyya(vector<Mat> hitograms)
    {
        cout<<"Called"<<endl;
        cout<<histograms.size()<<endl;
        getch();
        while(!histograms.empty())
        {
            int i=0;
            Mat temp1 = histograms.back();
            while(!histograms.empty())
            {
            //  histograms.pop_back();
                Mat temp2 = histograms.at(i);
                double base_base = compareHist( temp1, temp2, 4 );
                i++;
                cout<<base_base<<endl;
            }
        }
    }
};

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);
    }

    void writer(vector<Mat> video)
    {
        Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
        VideoWriter oVideoWriter ("D:/MyVideo.avi", -1, 20, frameSize, true); //initialize the VideoWriter object 
        if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
        {
            cout << "ERROR: Failed to write the video" << endl;
            exit(0);
        }
        while(!video.empty()) //Show the image captured in the window and repeat
        {
            Mat temp;
            temp = video.back();
            video.pop_back();
            oVideoWriter<<temp;
        }
    }

    void displayvideo(vector<Mat> videoframe)
    {
        Mat tempframe;
        while(!videoframe.empty()) //Show the image captured in the window and repeat
        {
            tempframe = videoframe.back();
            imshow("video", tempframe);
            videoframe.pop_back();
            waitKey(20); // waits to display frame
        }   
    }

    void displayframe(Mat frame)
    {
        imshow("video", frame);
        waitKey(20); // waits to display frame
    }
};

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

2 个答案:

答案 0 :(得分:0)

  

void bhattacharyya(vector hitograms)

你正在使用传递值。使用参考传递:

  

void bhattacharyya(vector&amp; hitograms)

答案 1 :(得分:0)

你的密码中有错别字!因为Nik suggested更好地使用传递参考。

试试这个:

void bhattacharyya(vector<Mat>& histograms)
{
    cout<<"Called"<<endl;
    cout<<histograms->size()<<endl;

    while(!histograms->empty())
    {
        int i=0;
        Mat temp1 = histograms->back();
        while(!histograms->empty())
        {
            Mat temp2 = histograms.at(i);
            double base_base = compareHist( temp1, temp2, 4 );
            i++;
            cout<<base_base<<endl;
        }
    }
}