The histogram of the single channel image is calculated and drawn. Drawing a histogram in Matlab is a very simple thing, but in the C environment, it turns into a problem. All kinds of implementation methods are available, and they must be reprogrammed by themselves. Fortunately, there is OpenCV. The following code requires OpenCV4.0 support and compiled in VC6.
Transfer from a bare code /// / to do a histogram of single channel images ///
#include "cv.h" #include "highgui.h" #include
INT Main (int Argc, char ** argv) {iblimage * src = 0; iplimage * histimg = 0; cvhistogram * hist = 0; int hdims = 50; // Division of HIST, more precise, Float Hranges_arr [ ] = {0,255}; float * hranges = HRANGES_ARR; int bin_w; float max_val; int i; if (argc! = 2 || (src = CVLoadImage (Argv [1], 0)) == null) // force to Gray Image Return -1; CvnamedWindow ("Histogram", 1); Hist = CvcreateHist (1, & HDIMS, CV_HIST_ARRAY, & HRANGES, 1); // Calculation Temperature Figure Histimg = CVCReateImage (Cvsize (320, 200), 8, 3);
Cvzero (Histimg); Cvcalchist (& SRC, HIST, 0, 0); // Calculate histogram CvgetminmaxHistValue (Hist, 0, & max_val, 0, 0); // only look for maximum cvconvertscale (hist-> bins, hist-> BINS, MAX_VAL? 255. / max_val: 0., 0); // Zoom BIN to the interval [0,255] cvzero (histimg); bin_w = histimg-> width / hdims; // hdims: Bin number, bin_w is Width // Painted Directive Map for (i = 0; i