Ask Your Question
0

How can I create histogram from a set of values in OpenCV?

asked 2013-03-09 00:05:52 -0600

Nihad gravatar image

I have a text file which consist of several values. These values represent distance from origin to any point in the triangle. How can I create histogram from these values? Is there any functions in OpenCV which can create histogram from these values? Below my sample values are given:

..........................

3.4 1.2 6.9 0.2 5.1 2.9

.........................

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-03-09 03:14:41 -0600

berak gravatar image

once you've got those values inside a cv::Mat, you can call calcHist on that.

// read it in:
#include <fstream>

std::vector<float> vec;
ifstream  is("my.txt");
while ( ! is.eof() ) {
    float f;
    is >> f;
    vec.push_back(f);
}

// now make a Mat of that, easy ;)
cv::Mat m(vec);
edit flag offensive delete link more

Comments

I tried in this way , but output is not acceptable.

int main(.......) { ...................... double histogram_sample[1][6]={1.0,2.0,3.0,4.0,1.0,2.0};

Mat histogram(1,6,CV_64F,(void*)histogram_sample);


int histSize = 4;
float range[] = { 1.0, 4.0 } ;
const float* histRange = { range };

bool uniform = true; bool accumulate = false;

Mat hist;

calcHist( &amp;histogram, 1, 0, Mat(), hist, 1, &amp;histSize, &amp;histRange, uniform, accumulate );

cout&lt;&lt;"hist:"&lt;&lt;hist&lt;&lt;endl;
    ...........................
    }

Output: hist:[0;0;0;0]

Nihad gravatar imageNihad ( 2013-03-09 03:55:33 -0600 )edit

oh, i tried your example, and it only worked for float, not for double

`float histogram_sample[6]={1.0,2.0,3.0,4.0,1.0,2.0};

Mat histogram(1,6,CV_32F,(void*)histogram_sample); `

[2; 2; 1; 0]

also, if you want the value for 4.0 to show up, you have to set the upper range to 5.0 (maxVal+1)

float range[] = { 1.0, 5.0 } ;

[2; 2; 1; 1]

berak gravatar imageberak ( 2013-03-09 04:08:54 -0600 )edit

Thank you. I did same as you. I have one more question. Why total number of frequency is 5 (2+2+1+0) in histogram bin. I think it should be 6 because array size is 6.

Nihad gravatar imageNihad ( 2013-03-09 04:36:17 -0600 )edit

Thank you. It works fine for this array.

Nihad gravatar imageNihad ( 2013-03-09 04:45:15 -0600 )edit

Question Tools

Stats

Asked: 2013-03-09 00:05:52 -0600

Seen: 233 times

Last updated: Mar 09 '13