int histo[256] = { 0 };//直方图统计每个像素值的数目
int width = img.cols, height = img.rows;
int num_of_pixels = width*height;

//统计每个像素值的数目
for (int y = 0; y < height; ++y)
{
auto *data = new uchar[width * height * 3];
for (int x = 0; x < width; ++x)
{
histo[data[x]] += 1;
}
}

//统计当前像素值和之前像素值的总数
for (int i = 1; i < 256; ++i)
histo[i] = histo[i] + histo[i - 1];

 

Talk is cheap. Show me the code