Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Histogram outputs always same picture

Hi, i have to compute a histogram for two picture. I then made a function to compute histograms of gray images. The problem is that at the end i always get the same histogram result,regardless to the image. This is my code

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;

Mat histo(Mat &src)
{
/// Establish the number of bins
int histSize = 256;

/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 } ;
const float* histRange = { range };

bool uniform = true; bool accumulate = false;

Mat b_hist;

/// Compute the histograms:
calcHist( &src, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );

// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );

Mat histImage( hist_h, hist_w, CV_8UC3, Scalar(0,0,0) );

/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

/// Draw for each channel
for( int i = 1; i < histSize; i++ )
{
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
                     Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
                     255, 2, 8, 0  );

}


return histImage;
}

int main( int argc, char** argv )
{
    if( argc != 2)
    {
    cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
    return -1;
    }

    Mat image,grayImage;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.


    cvtColor( image, grayImage, CV_BGR2GRAY );

    namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Gray image", grayImage );                   // Show our image inside it.

   Mat saturedImage=grayImage;

    for( int y = 0; y < image.rows; y++ )
    {
        for( int x = 0; x < image.cols; x++ )
        {
            saturedImage.at<uchar>(y,x) = saturate_cast<uchar>(saturedImage.at<uchar>(y,x)+50) ;
        }
    }


    namedWindow( "Image3", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Image3", saturedImage );                   // Show our image inside it.

// Gray picture Histogram
    namedWindow("calcHist Grey image", CV_WINDOW_AUTOSIZE );
    imshow("calcHist Grey image", histo(grayImage) );

 //Satured image histo
    namedWindow("calcHist Satured image", CV_WINDOW_AUTOSIZE );
    imshow("calcHist Satured image", histo(saturedImage) );


    waitKey(0);

    return 0;
}