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];