This commit is contained in:
Alexander Shishkov 2012-03-05 11:08:59 +00:00
parent ec5bdc7de8
commit 1494da339d
3 changed files with 122 additions and 121 deletions

View File

@ -88,79 +88,80 @@ Code
.. code-block:: cpp .. code-block:: cpp
#include "opencv2/highgui/highgui.hpp" #include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/imgproc/imgproc.hpp"
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
/** @function main */ /**
int main( int argc, char** argv ) * @function main
{ */
Mat src, dst; int main( int argc, char** argv )
{
Mat src, dst;
/// Load image /// Load image
src = imread( argv[1], 1 ); src = imread( argv[1], 1 );
if( !src.data ) if( !src.data )
{ return -1; } { return -1; }
/// Separate the image in 3 places ( R, G and B ) /// Separate the image in 3 places ( B, G and R )
vector<Mat> rgb_planes; vector<Mat> bgr_planes;
split( src, rgb_planes ); split( src, bgr_planes );
/// Establish the number of bins /// Establish the number of bins
int histSize = 255; int histSize = 256;
/// Set the ranges ( for R,G,B) ) /// Set the ranges ( for B,G,R) )
float range[] = { 0, 255 } ; float range[] = { 0, 256 } ;
const float* histRange = { range }; const float* histRange = { range };
bool uniform = true; bool accumulate = false; bool uniform = true; bool accumulate = false;
Mat r_hist, g_hist, b_hist; Mat b_hist, g_hist, r_hist;
/// Compute the histograms: /// Compute the histograms:
calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for R, G and B // Draw the histograms for B, G and R
int hist_w = 400; int hist_h = 400; int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize ); int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) ); Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
/// Normalize the result to [ 0, histImage.rows ] /// Normalize the result to [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// Draw for each channel /// Draw for each channel
for( int i = 1; i < histSize; i++ ) for( int i = 1; i < histSize; i++ )
{ {
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 ); Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 ); Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 ); Scalar( 0, 0, 255), 2, 8, 0 );
} }
/// Display /// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage ); imshow("calcHist Demo", histImage );
waitKey(0); waitKey(0);
return 0; return 0;
}
}
Explanation Explanation
=========== ===========
@ -184,25 +185,25 @@ Explanation
.. code-block:: cpp .. code-block:: cpp
vector<Mat> rgb_planes; vector<Mat> bgr_planes;
split( src, rgb_planes ); split( src, bgr_planes );
our input is the image to be divided (this case with three channels) and the output is a vector of Mat ) our input is the image to be divided (this case with three channels) and the output is a vector of Mat )
#. Now we are ready to start configuring the **histograms** for each plane. Since we are working with the R, G and B planes, we know that our values will range in the interval :math:`[0,255]` #. Now we are ready to start configuring the **histograms** for each plane. Since we are working with the B, G and R planes, we know that our values will range in the interval :math:`[0,255]`
a. Establish number of bins (5, 10...): a. Establish number of bins (5, 10...):
.. code-block:: cpp .. code-block:: cpp
int histSize = 255; int histSize = 256; //from 0 to 255
b. Set the range of values (as we said, between 0 and 255 ) b. Set the range of values (as we said, between 0 and 255 )
.. code-block:: cpp .. code-block:: cpp
/// Set the ranges ( for R,G,B) ) /// Set the ranges ( for B,G,R) )
float range[] = { 0, 255 } ; float range[] = { 0, 256 } ; //the upper boundary is exclusive
const float* histRange = { range }; const float* histRange = { range };
c. We want our bins to have the same size (uniform) and to clear the histograms in the beginning, so: c. We want our bins to have the same size (uniform) and to clear the histograms in the beginning, so:
@ -215,26 +216,26 @@ Explanation
.. code-block:: cpp .. code-block:: cpp
Mat r_hist, g_hist, b_hist; Mat b_hist, g_hist, r_hist;
e. We proceed to calculate the histograms by using the OpenCV function :calc_hist:`calcHist <>`: e. We proceed to calculate the histograms by using the OpenCV function :calc_hist:`calcHist <>`:
.. code-block:: cpp .. code-block:: cpp
/// Compute the histograms: /// Compute the histograms:
calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
where the arguments are: where the arguments are:
.. container:: enumeratevisibleitemswithsquare .. container:: enumeratevisibleitemswithsquare
+ **&rgb_planes[0]:** The source array(s) + **&bgr_planes[0]:** The source array(s)
+ **1**: The number of source arrays (in this case we are using 1. We can enter here also a list of arrays ) + **1**: The number of source arrays (in this case we are using 1. We can enter here also a list of arrays )
+ **0**: The channel (*dim*) to be measured. In this case it is just the intensity (each array is single-channel) so we just write 0. + **0**: The channel (*dim*) to be measured. In this case it is just the intensity (each array is single-channel) so we just write 0.
+ **Mat()**: A mask to be used on the source array ( zeros indicating pixels to be ignored ). If not defined it is not used + **Mat()**: A mask to be used on the source array ( zeros indicating pixels to be ignored ). If not defined it is not used
+ **r_hist**: The Mat object where the histogram will be stored + **b_hist**: The Mat object where the histogram will be stored
+ **1**: The histogram dimensionality. + **1**: The histogram dimensionality.
+ **histSize:** The number of bins per each used dimension + **histSize:** The number of bins per each used dimension
+ **histRange:** The range of values to be measured per each dimension + **histRange:** The range of values to be measured per each dimension
@ -246,26 +247,26 @@ Explanation
.. code-block:: cpp .. code-block:: cpp
// Draw the histograms for R, G and B // Draw the histograms for R, G and B
int hist_w = 400; int hist_h = 400; int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize ); int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) ); Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
#. Notice that before drawing, we first :normalize:`normalize <>` the histogram so its values fall in the range indicated by the parameters entered: #. Notice that before drawing, we first :normalize:`normalize <>` the histogram so its values fall in the range indicated by the parameters entered:
.. code-block:: cpp .. code-block:: cpp
/// Normalize the result to [ 0, histImage.rows ] /// Normalize the result to [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
this function receives these arguments: this function receives these arguments:
.. container:: enumeratevisibleitemswithsquare .. container:: enumeratevisibleitemswithsquare
+ **r_hist:** Input array + **b_hist:** Input array
+ **r_hist:** Output normalized array (can be the same) + **b_hist:** Output normalized array (can be the same)
+ **0** and**histImage.rows**: For this example, they are the lower and upper limits to normalize the values of **r_hist** + **0** and**histImage.rows**: For this example, they are the lower and upper limits to normalize the values of **r_hist**
+ **NORM_MINMAX:** Argument that indicates the type of normalization (as described above, it adjusts the values between the two limits set before) + **NORM_MINMAX:** Argument that indicates the type of normalization (as described above, it adjusts the values between the two limits set before)
+ **-1:** Implies that the output normalized array will be the same type as the input + **-1:** Implies that the output normalized array will be the same type as the input
@ -273,35 +274,35 @@ Explanation
#. Finally, observe that to access the bin (in this case in this 1D-Histogram): #. Finally, observe that to access the bin (in this case in this 1D-Histogram):
.. code-block:: cpp .. code-block:: cpp
/// Draw for each channel /// Draw for each channel
for( int i = 1; i < histSize; i++ ) for( int i = 1; i < histSize; i++ )
{ {
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 ); Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 ); Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 ); Scalar( 0, 0, 255), 2, 8, 0 );
} }
we use the expression: we use the expression:
.. code-block:: cpp .. code-block:: cpp
r_hist.at<float>(i) b_hist.at<float>(i)
where :math:`i` indicates the dimension. If it were a 2D-histogram we would use something like: where :math:`i` indicates the dimension. If it were a 2D-histogram we would use something like:
.. code-block:: cpp .. code-block:: cpp
r_hist.at<float>( i, j ) b_hist.at<float>( i, j )
#. Finally we display our histograms and wait for the user to exit: #. Finally we display our histograms and wait for the user to exit:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -25,57 +25,57 @@ int main( int argc, char** argv )
if( !src.data ) if( !src.data )
{ return -1; } { return -1; }
/// Separate the image in 3 places ( R, G and B ) /// Separate the image in 3 places ( B, G and R )
vector<Mat> rgb_planes; vector<Mat> bgr_planes;
split( src, rgb_planes ); split( src, bgr_planes );
/// Establish the number of bins /// Establish the number of bins
int histSize = 255; int histSize = 256;
/// Set the ranges ( for R,G,B) ) /// Set the ranges ( for B,G,R) )
float range[] = { 0, 255 } ; float range[] = { 0, 256 } ;
const float* histRange = { range }; const float* histRange = { range };
bool uniform = true; bool accumulate = false; bool uniform = true; bool accumulate = false;
Mat r_hist, g_hist, b_hist; Mat b_hist, g_hist, r_hist;
/// Compute the histograms: /// Compute the histograms:
calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for R, G and B // Draw the histograms for B, G and R
int hist_w = 400; int hist_h = 400; int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize ); int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) ); Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
/// Normalize the result to [ 0, histImage.rows ] /// Normalize the result to [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// Draw for each channel /// Draw for each channel
for( int i = 1; i < histSize; i++ ) for( int i = 1; i < histSize; i++ )
{ {
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 ); Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 ); Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 ); Scalar( 0, 0, 255), 2, 8, 0 );
} }
/// Display /// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
imshow("calcHist Demo", histImage ); imshow("calcHist Demo", histImage );
waitKey(0); waitKey(0);
return 0; return 0;
} }