The second job needs to print out a grayscale histogram, of course, you can't use OCV's own calchist function to get the mat object ... Results online Search How to use their own data to create a histogram, the search is directly with the function of _ (: З"∠) _
The result this place dragged for a long time hehe hehe hehe oh. Finally, try to resist the line to draw a gray histogram of the code to see exactly what the mystery, the results found actually, nothing, big Deal (laughter) (Cry).
Basic ideas:
Create a Mat object as the histogram canvas, and draw the 256 gray-level values straight with rectangle or line one.
...... It's so simple that I can't imagine _ (: З"∠) _ On the importance of tolerance (.
In addition, we need to know the coordinate system in OCV. The origin is the upper-left corner of the image, the x-axis is the edge above the image, and the y-axis is the left edge of the image. This is very important for drawing basic graphics.
First reprint the online CSDN blog Xiaowei_cqu Gray histogram of the code.
//To draw a grayscale histogramintMain () {Mat src,gray; SRC=imread ("baboon.jpg"); Cvtcolor (Src,gray,cv_rgb2gray); intBins = the; intHist_size[] ={bins}; floatRange[] = {0, the }; Const float* ranges[] ={range}; Matnd hist; intChannels[] = {0};//calculate grayscale histogram using OCV self-band functionCalchist (&gray,1, channels, Mat (),//Don't use maskhist1, hist_size, ranges,true,//The histogram is uniform false );//Draw a histogram DoubleMax_val; Minmaxloc (hist,0, &max_val,0,0); intScale =2; inthist_height= the; Mat hist_img= Mat::zeros (hist_height,bins*Scale , CV_8UC3); for(intI=0; i<bins;i++) { floatBin_val = hist.at<float>(i); intintensity = Cvround (bin_val*hist_height/max_val);//the height to drawRectangle (Hist_img,point (i*scale,hist_height-1), point ((I+1) *scale-1, Hist_height-intensity), Cv_rgb (255,255,255)); } imshow ("Source", SRC); Imshow ("Gray Histogram", hist_img); Waitkey (10000000000); return 0;}
What I need to focus on is the "draw a histogram" piece of code.
It can be found that this histogram is to locate each interval length 2pixel, the length of the histogram canvas is "interval number * interval Length" = 256*2 = 512, height is 256, initialized to a black canvas.
Into the histogram stage, simply speaking, is to take out the image of the gray frequency table (each gray value of the total number of pixels in the image) of each value, calculate the height of the drawing, according to the height of the drawing to underline or rectangle.
Draw height, is "rectangle height * Canvas height/maximum rectangle height", why this set, should be related to the probability statistic histogram ...
The rectangle is used here to represent each interval.
The parameters are: Bearer object (Mat here), lower left corner , Upper right corner , color, thickness, size rectangle (picture,a,center,scalar (255,0,0));
Of course, you can also use line as a segment to represent an interval.
The parameters are: Bearer object (Mat here), starting point, end point, color, thickness, Linetype line (Picture,a,center,scalar (255,0,0));
Since the OCV general coordinate system as described above, so corresponding to the customary coordinate systems, the x-axis should be coordinates (x, 255) of the line, as to why 255, is to be able to show a height of 0 of the interval.
and rectangle's two-point argument why, let me draw a picture.
Suppose you want to draw a rectangle at this time (point (0, Hist_height-1), (1, hist_height-intensity)).
According to the above code changes, I can also use their own data to draw a histogram of the qaq sense of the sky-moving ground.
The above contents refer to these several blogs, including the histogram drawing code, OCV coordinate system and the function of drawing basic graphics, thanks for sharing!
http://blog.csdn.net/panda1234lee/article/details/14162965
http://blog.csdn.net/thefutureisour/article/details/7523925
http://blog.csdn.net/xiaowei_cqu/article/details/8833799
http://blog.csdn.net/xiaowei_cqu/article/details/7600666
Digital Image processing Jobs using OPENCV-Custom histograms