Ask Your Question
0

Histogram outputs always same picture

asked 2013-09-20 05:39:16 -0500

GiulSDU gravatar image

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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-09-20 05:49:43 -0500

Moster gravatar image

Your gray image and saturated image are the same.

Mat saturedImage=grayImage;

This does not copy the image. saturatedImage points to the same data.

Mat saturatedImage;   
grayImage.copyTo(saturatedImage)

This should work if Im not mistaken

edit flag offensive delete link more

Comments

Thank you very much!! is working now.....but..how can that affect the histogram???

GiulSDU gravatar imageGiulSDU ( 2013-09-20 05:54:15 -0500 )edit

In your original version, saturatedImage is exactly the same as grayImage. So if you modify the saturatedImage, you also modify the grayImage -> histogram is the same. You need to explicitly copy grayImage to solve this.

Moster gravatar imageMoster ( 2013-09-20 05:55:38 -0500 )edit

Ok now is clear!! That helped a lot!! thank you very much :)

GiulSDU gravatar imageGiulSDU ( 2013-09-20 05:56:54 -0500 )edit

The histogram is calculated on the same data - thus it has also the same output The assign-operator in Mat saturatedImage=grayImage does NOT copy the data it just creates another Mat-header around the same set of data. Since you calculate the histogram of the grayImage AFTER you did the saturation-cast-stuff, the histogram is already calculated on the saturated data. Try to move the line imshow("calcHist Grey image", histo(grayImage) ); before Mat saturedImage=grayImage;, then it should also work with your original code (but it's not what you intend to achieve ;))

ThomasB gravatar imageThomasB ( 2013-09-20 05:57:36 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-20 05:39:16 -0500

Seen: 115 times

Last updated: Sep 20 '13