In fact, at the beginning of the time, read a lot of books and tutorials on drawing and color images, etc., but I think it is still the first to learn the grayscale histogram, because the grayscale dims is 1, if dims is 3 is color, but also know the front color image of the pixel access, I believe it will soon be able to migrate past.
First, a different angle to understand the image (histogram)
The first is when we face the image, we are faced with an abstract matrix, for example, the following is a 0-255 gray image representation, dense
Then we do the histogram, in fact, is the statistics of these pixel values, see, where Bin is the number, data and range is the interpretation of the diagram, a look to understand
II. Preparation of knowledge
If you want to draw out a histogram, you need to know several functions
(1) The Point class data structure represents points in a two-dimensional coordinate system
Point Point=point;
(2) Calchist () Draw histogram
void Calchist (const mat* Arrays, intnarrays, const int* channels, Inputarray mask, Outputarray hist, int dims,const int* h Istsize, const float** ranges, bool uniform=true, boolaccumulate=false);
Parameter explanation:
- Arrays: The input image pointer, can be multiple images, all images must have the same depth (cv_8u orcv_32f). At the same time, a picture can have multiple channes.
- Narrays: The number of images entered.
- Channels: An array of channes used to calculate the histogram. For example, the input is 2 pairs of images, the first image has 0,1,2 a total of three channel, the second image has only 1 channel, then the input is a total of 4 channes, if int channels[3] = {3, 2, 0}, Then the histogram is calculated using the first channel of the second pair of images and the 2nd and No. 0 channels of the first pair of images.
- Mask: Mask. If mask is not empty, then it must be a 8-bit (cv_8u) array, and its size is the same size as arrays[i], and a point with a value of 1 will be used to calculate the histogram.
- hist: Calculated Histogram
- Dims: The dimension of the computed histogram.
- Histsize: The number of histograms on each dimension. Simply think of the histogram as a vertical bar, that is, the number of vertical bars on each dimension.
- Ranges: The range used for statistics. For example, float rang1[] = {0, 20};float rang2[] = {30, 40}; Const float*rangs[] = {rang1, rang2}; Then the values for the 0,20 and 30,40 ranges are counted.
- Uniform: Whether the width of each vertical bar is equal.
- Accumulate: Whether or not to accumulate. If true, Hist will not be emptied first at the next calculation.
Draw a line, draw a color in the image img, the thickness is thickness, and the line of type Linetype
(3) line () rectangle () Draw histogram
void Line (mat& img, point pt1, Pointpt2, const scalar& color, int thickness=1, int linetype=8, int shift=0)//Two Point to confirm a straight line. Linetype: line type//shift: Coordinate decimal number dimension//Draw a single real rectangle void Rectangle (mat& img, point pt1,point pt2, const scalar& color, int Thickness=1, int linetype=8, int shift=0)//two vertices of a diagonal determines that a rectangle//pt1 and pt2 are mutually negative for vertex//thickness indicates that the rectangle is a real rectangle
Three, draw one-dimensional gray histogram
<span style= "FONT-SIZE:18PX;" > #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream>using Namespace Cv;using namespace Std;void help () {printf ("\n\n\t\t\t Welcome to the world of histograms! \ n ");p rintf (" \ n----------------------------------------------------------------------------\ n ");} int main () {Mat srcimage = Imread ("Lena.jpg", 0), Imshow ("original", Srcimage), if (!srcimage.data) {cout << "fail to load IMA GE "<< Endl; return 0;} System ("Color 1F"); Help (); Matnd dsthist; In CV with cvhistogram *hist = cvcreatehistint dims = 1;float hranges[2] = {0, 255};const float *ranges[1] = {Hranges}; Here it is necessary for the const type int size = 256;int channels = 0;//to calculate the histogram of the image calchist (&srcimage, 1, &channels, Mat (), Dsthist, dims, & Amp;size, ranges); CV is cvcalchistint scale = 1; Mat dstimage (Size * scale, size, cv_8u, Scalar (0));//Gets the maximum and minimum values double minValue = 0;double MaxValue = 0;minmaxloc (DSTHIST,&A Mp;minvalue, &maxvalue, 0, 0); In CV, it's cvgetminmaxhistvalue/./Draw out histogram int hpt = saturate_cast<int> (0.9 * size); for (int i = 0; i <; i++) {Float Binvalue = dsthist.at<float > (i); Note that hist is a float type int realvalue = saturate_cast<int> (Binvalue * hpt/maxvalue);//rectangle (Dstimage,point (I*sca Le, Size-1), point ((i+1) *scale-1, Size-realvalue), Scalar (255)), Line (Dstimage,point (i*scale,size-1), point ((i+1) * Scale-1,size-realvalue), Scalar (255));} Imshow ("One-dimensional histogram", dstimage); Waitkey (0); return 0;} </span>
In fact, we sometimes want to change the quantification, the above expression is 256, we can 50 100 can, here used the knowledge of the slider, so do not want to speak more, just provide a reference for others to write, respect the original author, write very good
<span style= "FONT-SIZE:18PX;" > #include "cv.h" #include "highgui.h" #include <stdio.h> #include <ctype.h>using namespace std;using namespace Cv;iplimage *src = 0;iplimage *histimg = 0; Cvhistogram *hist = 0; int hdims = 50; Divide the initial number of HIST, the higher the more accurate//scrollbar function void HIST (int t) {float hranges_arr[] = {0,255}; float* hranges = Hranges_arr; int bin_w; int Bin_u; float Max; int i; Char string[10]; Cvfont font; Cvinitfont (&font, cv_font_hershey_plain,1, 1, 0, 1, 8);//font structure initialization if (hdims==0) {printf ("The histogram bar number cannot be zero! \ n ");} else{hist = cvcreatehist (1, &hdims, Cv_hist_array, &hranges, 1); Create Histogram histimg = Cvcreateimage (Cvsize (800,512), 8, 3); Cvzero (HISTIMG); Cvcalchist (&src, hist, 0, 0); Calculate the histogram cvgetminmaxhistvalue (hist,null,&max,null,null);//Find the maximum value and its position//printf ("max_val:%f \ n", max_val); Cvzero (HISTIMG); Double bin_w = (double) histimg->width/hdims; Hdims: The number of bars, then bin_w the width of the bar Double Bin_u = (double) histimg->height/max; Max: The number of pixels in the highest bar, the Bin_u is the height of a single pixel//histogram for (int i=0;i
Three, color histogram
Here do not want to introduce too much, later when the color image will be specific to say
Four, Matlab auxiliary
A imhist () function is done.
<span style= "FONT-SIZE:18PX;" >clear;%% read into image a=imread (' cameraman.tif '); Imhist (a); title (' Histogram of original cameraman image ');</span>
OpenCV image recognition from zero to proficient (8)-----Grayscale Histogram