in the bag.
- Grayscale histogram
- Color histogram
Sunflower TreasureThe theory of histograms is also very rich, and there are many applications, such as:Histogram equalizationhistogram Matching (meanshift,camshift)
Here, let me introduce the basics. How to draw a histogram of an image.
take a grayscale image. The histogram is the corresponding number of different shades of gray, the horizontal axis (x) is [0,256], vertical axes (y) is the corresponding number of such as, respectively, gray histogram and color histogram
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvywjjzde5oti3mtln/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "Width=" height= "461" >
Initial Knowledge API
-
C + +: void calchist (Const mat*
ImagesInt
nimages, const int*
Channels, Inputarray
Mask, sparsemat&
histInt
dims, const int*
histsize, const float**
Ranges, BOOL
Uniform=true, BOOL
Accumulate=false )
-
|
- Images – is an array of images. For simplicity's sake. We all just pass an image.
- nimages – the image array size. We fixed it at 1.
- Channels – is an array, grayscale image of 0 can be, color image to 0,1,2
- Mask – we don't need it here. The mat () will be able to
- hist – Histogram of output
- dims – calculate color RGB to 3,gray pass 1
- histsize – is an array. The general content is 256.
- Ranges – is a two-dimensional array in which each array contains a range, [0,255] most often used, the following will see
- Uniform – it is sufficient to use the default here.
- Accumulate – it is sufficient to use the default here.
|
if we want to calculate the histogram of the grayscale image, for example, the following call can:int histsize[1];//Number of bins
float hranges[2]; Min and Max pixel value
Const float* RANGES[1];
int channels[1]; Only 1 channel used here
histsize[0]= 256;
hranges[0]= 0.0;
Hranges[1]= 255.0;
Ranges[0]= hranges;
channels[0]= 0; By default, we look at channel 0
Computes the 1D histogram.
Mat hist;
Compute histogram
Calchist (&image,1,channels,mat (), hist,1,histsize,ranges);
Gunmenfirst look at a grayscale class of designThe first method is a constructorthe second method is to get a histogramthe third method is to draw a histogram
Class HISTOGRAM1D {private:int histsize[1];//number of binsfloat hranges[2];//min and Max Pixel valueconst float* rang Es[1];int Channels[1]; Only 1 channel used herepublic:histogram1d () {histsize[0]= 256;hranges[0]= 0.0;hranges[1]= 255.0;ranges[0]= hranges; channels[0]= 0; By default we are at Channel 0}//computes the 1D histogram. Mat Gethistogram (const Cv::mat &image) {Mat hist;//Compute histogramcalchist (&image,1,channels,mat (), hist,1, histsize,ranges); return hist;} Mat gethistogramimage (const Cv::mat &image) {//Compute histogram Firstmat hist= gethistogram (image);//Get min and Max Bin valuesdouble maxval=0;double Minval=0;minmaxloc (hist, &minval, &maxval, 0, 0);//Image on which to display hi Stogrammat histimg (histsize[0], histsize[0],cv_8u,scalar (255));//Set highest point at 90% of nbinsint HPT = STATIC_CAST&L T;int> (0.9*histsize[0]);//Draw a vertical line for each binfor (int h = 0; h < histsize[0]; h++) {Float Binval = h Ist.at<float> (h); int intensity = static_cast<int> (binval*hpt/maxval);//This function draws a line between 2 Pointsline (histimg,p Oint (H,histsize[0]), point (H,histsize[0]-intensity), Scalar::all (0)); return histimg;};
The main function then calls the
Mat image,gray;image = Imread (argv[1], 1); if (!image.data)return-1; Cvtcolor (image, Gray, Cv_bgr2gray); HISTOGRAM1D H;namedwindow ("histogram"); Imshow ("Histogram", H.gethistogramimage (gray));
Gray histogram has been painted, the following color histogramthe process of drawing the color histogram and gray histogram almost identical, is to separate the RGB three channels separately, the respective calculation
Separate the image in 3 places (B, G and R) vector<mat> bgr_planes;split (SRC, bgr_planes);
Now you have 3 mats stored in Bgr_planes, again stressed. OpenCV the first channel of the color image inside is BLUE,BGR OHand then. You can use our previous HISTOGRAM1D class to call each other.
Extrapolatesometimes. We can also draw histograms of different color spacesbelow we draw 2D hue-saturation histogram for a color image
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvywjjzde5oti3mtln/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" >
Because I didn't learn about the HSV color space theory. So the following is the code to post the official tutorial
#include <cv.h> #include
Computer Vision Discussion Group: 162501053reprint Please specify: http://blog.csdn.net/abcd1992719g
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
OpenCV2 first Marathon 8 ring--Draw a bar chart