#include <opencv2/opencv.hpp> #include <iostream> #include <vector> using namespace CV;
using namespace Std;
int main () {Mat srcimage = Imread ("1.jpg");
Imshow ("original", "Srcimage");
Configure the variable for the calculation histogram//First is the channel of the image to be computed, that is, which channel of the image needs to be computed (BGR space needs to be determined to calculate B or G cargo r space) int channels = 0;
Then is the configuration output the result storage space, uses the MATND type to store the result matnd dsthist; Next is the number of bars for each dimension of the histogram (that is, grouping the values, how many groups are in total) int histsize[] = {32}; If this is written as int histsize = 256; Then the following call to calculate the function of the histogram, the variable needs to write &histsize//finally is to determine the range of values for each dimension, that is, the total number of horizontal axis//First to define a variable to store a single dimension of the value of the range of values float midranges[] = {0,
256};
const FLOAT *ranges[] = {Midranges};
Calchist (&srcimage, 1, &channels, Mat (), dsthist, 1, histsize, ranges, true, false); After the Calchist function call is finished, the dsthist variable will store the information of the histogram with the Dsthist template function at<type> (i) to get the value of the I bar//at<type> (i, J) Get the first and the value of the J Bar//start the intuitive display histogram--draw histogram//first create a black bottom image, in order to display color, so the drawing image is a 8-bit 3-Channel image Mat B_drawimage = Mat::zeros (Size 256,
), CV_8UC3); Because the total number of pixels in any one image can be a lot more than the size of the defined image, in this case, the number of firstMinmaxloc function to get the maximum number of pixels after the histogram is computed double g_dhistmaxvalue = 0;
Minmaxloc (dsthist, 0, &g_dhistmaxvalue, 0, 0); Integrate the number of pixels into the maximum range of the image//traverse the histogram to get the data for (int i = 0; i <; i++) {int value = Cvround (dsthist.at<float> (i) *
* 0.9/g_dhistmaxvalue); Rectangle (b_drawimage, point (i * 8, b_drawimage.rows-1), point (i * 8 + 256/32, b_drawimage.rows-1-value), Scal
AR (255, 0, 0),-1);
} imshow ("The Histogram of the Blue channel" ", B_drawimage);
Draw Green Channel channels = 1;
Calchist (&srcimage, 1, &channels, Mat (), dsthist, 1, histsize, ranges, true, false);
Mat g_drawimage = Mat::zeros (Size (n, N), CV_8UC3);
Minmaxloc (dsthist, 0, &g_dhistmaxvalue, 0, 0);
for (int i = 0; i < i++) {int value = Cvround (dsthist.at<float> (i) * 0.9/g_dhistmaxvalue); Rectangle (g_drawimage, point (i * 8, g_drawimage.rows-1), point (i * 8 + 256/32, g_drawimage.rows-1-value), Scal
AR (0, 255, 0),-1);
} imshow ("The Histogram of the green channel" ", G_drawimage);
Draw Red Channelchannels = 2;
Calchist (&srcimage, 1, &channels, Mat (), dsthist, 1, histsize, ranges, true, false);
Mat r_drawimage = Mat::zeros (Size (n, N), CV_8UC3);
Minmaxloc (dsthist, 0, &g_dhistmaxvalue, 0, 0);
for (int i = 0; i < i++) {int value = Cvround (dsthist.at<float> (i) * 0.9/g_dhistmaxvalue); Rectangle (r_drawimage, point (i * 8, r_drawimage.rows-1), point (i * 8 + 256/32, r_drawimage.rows-1-value), Scal
AR (0, 0, 255),-1);
} imshow ("The Histogram of the Red channel" ", R_drawimage);
Waitkey (0);
return 0; }