Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have written a code to plot the histogram and i need to count the number of peaks in the histogram plot. Since i am new to it i was facing problem in finding the peaks, so please help me in solving this problem

#include "stdio.h"

include "iostream"

include "opencv\cxcore.h"

include "opencv\highgui.h"

include "opencv\cv.h"

include "opencv2\core\core.hpp"

include "opencv2\highgui\highgui.hpp"

include "opencv2\imgproc\imgproc.hpp"

using namespace cv; using namespace std;

//int main( int argc, char** argv ) //{ // Mat src,hsv; // src=imread("DSC02712.JPG", CV_LOAD_IMAGE_COLOR);

int main( int argc, char** argv )

{ Mat src, dst,src1;

/// Load image src = imread( "1.png", CV_LOAD_IMAGE_COLOR ); cvtColor(src,src1,CV_RGB2GRAY);

if( !src.data ) { return -1; }

/// Separate the image in 3 places ( B, G and R ) //vector<mat> bgr_planes; //split( src, bgr_planes );

/// 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( &src1, 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 the histogram 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)) ), Scalar( 255, 0, 0), 2, 8, 0 ); }

/// Display namedWindow("Histgram", CV_WINDOW_AUTOSIZE ); imshow("Histgram", histImage ); waitKey(0); }

click to hide/show revision 2
No.2 Revision

count number of peaks in histogram

I have written a code to plot the histogram and i need to count the number of peaks in the histogram plot. Since i am new to it i was facing problem in finding the peaks, so please help me in solving this problem

#include "stdio.h"

include "iostream"

include "opencv\cxcore.h"

include "opencv\highgui.h"

include "opencv\cv.h"

include "opencv2\core\core.hpp"

include "opencv2\highgui\highgui.hpp"

include "opencv2\imgproc\imgproc.hpp"

#include "stdio.h"
#include "iostream"

#include "opencv\cxcore.h"
#include "opencv\highgui.h"
#include "opencv\cv.h"

#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"


using namespace cv;
using namespace std;

std; //int main( int argc, char** argv ) //{ // Mat src,hsv; // src=imread("DSC02712.JPG", CV_LOAD_IMAGE_COLOR);

CV_LOAD_IMAGE_COLOR);
int main( int argc, char** argv )

{ Mat src, dst,src1;

dst,src1; /// Load image src = imread( "1.png", CV_LOAD_IMAGE_COLOR ); cvtColor(src,src1,CV_RGB2GRAY);

cvtColor(src,src1,CV_RGB2GRAY); if( !src.data ) { return -1; }

} /// Separate the image in 3 places ( B, G and R ) //vector<mat> //vector<Mat> bgr_planes; //split( src, bgr_planes );

); /// Establish the number of bins int histSize = 256;

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

}; bool uniform = true; bool accumulate = false;

false; Mat b_hist;

b_hist; /// Compute the histograms:

histograms: calcHist( &src1, 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 the histogram for( int i = 1; i < histSize; i++ ) { line( histImage, Point( bin_w(i-1), bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , Point( bin_w(i), bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); }

} /// Display namedWindow("Histgram", CV_WINDOW_AUTOSIZE ); imshow("Histgram", histImage ); waitKey(0); }

}