Calculate Histogram of Hue [closed]

asked 2015-01-08 07:02:48 -0500

I want to compare my object by objects in database. So I decided to calculate their histogram of Hue and then compare my new Histogram by previous pictures. I decided to calculate Hue's histogram, Because I thought that it has low dependency to light.

This is my code: I have split the HSV picture and then I think that the first element of vector will have the Hue value. But it is surprising that I get the second line of output for every picture on 1080.

First I want to know if this code has written true for the purpose and then It would be appreciated if you help me by improving my code in both algorithm and programming manner.

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

class mycalchist
{
public:
    Mat src_hsv, src_h, hist_h;
    mycalchist(Mat);
    void setHistSize(int);
    void run();
    int histSize;

private:
    float range[2];
    const float* histRange;
    bool uniform; bool accumulate;
    vector<Mat> src_hsv_vector;
};

int main(int argc, char** argv)
{
    Mat src;
    if (argc > 1)
    {

        src = imread(argv[1]);
        if (!src.data)
        {
            return -1;
        }
    }
    else
        cout << "Usage: " << argv[0] << " <image file name>" << endl;


    mycalchist image(src);
    image.run();

    for (int i=0;i<image.histSize; i++)
        cout << image.hist_h.at<float>(0,i) << endl;
    imshow("src",src);
    waitKey(0);
    return 0;

}

mycalchist::mycalchist(Mat src)
{
    cvtColor(src,this->src_hsv,CV_RGB2HSV);
    split (this->src_hsv,src_hsv_vector);
    this->src_h = src_hsv_vector[0];
    this->range[0] = 0;
    this->range[1] = 256;
    this->histRange = {this->range};
    this->uniform = true;
    this->accumulate = false;
    this->histSize = 10;
}

void mycalchist::run()
{
    calcHist( &this->src_h, 1, 0, Mat(), this->hist_h, 1, &this->histSize, &this->histRange, this->uniform, this->accumulate );
    normalize(this->hist_h, this->hist_h, 0, this->src_h.rows, NORM_MINMAX, -1, Mat() );
}

output:

55.3835
1080
196.67
12.1901
3.30496
0.169019
0.27043
0
0
0
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-16 17:47:38.646534