Instead of using the openCV's ibuilt method of calculating the histogram and its normalization, i have written my own code for it as we are creating histogram only for hue channel. Have a look at my code.
int main()
{
Mat input = imread("jan31/class4Jan31.jpg",1);
Mat hsv_input;
int h_bins = 5;
Mat hist_input = Mat::zeros( 1, h_bins, CV_32FC1);
int h_range = 179;
int totalNumberPixels = 0;
cvtColor(input, hsv_input, CV_RGB2HSV);
Mat hsv_channels[3];
split( hsv_input, hsv_channels );
for (int i=0; i<hsv_channels[0].rows; i++)
{
for (int j=0; j<hsv_channels[0].cols; j++)
{
if( (int)hsv_channels[1].at<uchar>(i,j)>10 && (int)hsv_channels[1].at<uchar>(i,j)>100)
{
totalNumberPixels++;
int pixel_value = (int)hsv_channels[0].at<uchar>(i,j);
int corresponding_bin = ( pixel_value * h_bins ) / h_range;
hist_input.at<float>( 0, corresponding_bin ) = ( hist_input.at<float>( 0, corresponding_bin ) + 1 );
}
}
}
cout<<"\n total pixels: "<<totalNumberPixels;
for(int i=0; i<hist_input.rows; i++)
{
for (int j=0; j<hist_input.cols; j++)
{
float pixel = hist_input.at<float>(i,j);
hist_input.at<float>(i,j) = pixel / totalNumberPixels;
pixel = hist_input.at<float>(i,j);
cout<<"\n Pixel: "<<pixel;
}
}
cv::waitKey(0);
return 0;
}