C++ 中的直方图

Histogram in C++

我正在用 C++ 为确定的二维数组(其中填充了表示像素中颜色的数值的数字 -> matrizGrey)编写直方图。您必须通过参数 (argv[4]) 引入直方图将具有的部分数,范围在 0 到 255 之间。 因此,根据部分的数量,直方图会发生变化。我明白了它是如何工作的(比较范围的第一个元素和下一个元素之间的矩阵元素)但我的问题是我不能递归地做它。 下面的代码显示了我此时的进度:

  if (strcmp(argv[1], "-u") == 0 && *argv[2] == '0' && strcmp(argv[3], "-t") == 0) {
  unsigned int sections = atoi(argv[4]);
  unsigned int histogram[sections] = {};
  unsigned int k = 0;
  float limits[sections];
  float value = (float)255/sections;

  if (sections > 0) {
    cout << "The number of sections " << sections << "\n";
    cout << "Each section is " << value << "\n";

    cout << "The array of limits is : " << "\n";  
    for (unsigned int i = 0; i < sections; ++i) {
        limits[i] = value*i;
        cout << limits[i] << " ";
    }

       for (unsigned int i = 0; i < length; ++i) {
        for (unsigned int j = 0; j < width; ++j) {
            if (matrizGrey[i][j] >= 0 && matrizGrey[i][j] < limits[1] ) {
                histogram[0] = histogram[0]+1;
            }
            else if (matrizGrey[i][j] >= limits[1] && matrizGrey[i][j] < limites[2] ) {
                histogram[1] = histogram[1]+1;
            }
            else if (matrizGrey[i][j] >= limits[2] && matrizGrey[i][j] < limites[3] ) {
                histogram[2] = histogram[2]+1;
            }

        }
       }

    cout << endl;
    cout << "The final result will be : " << "\n";
    for (unsigned int i = 0; i < sections; ++i) {
        //histogram[i] = 0;
        cout << histogram[i] << " ";
    }

例如,"image":matrizGrey[0][0] = 100,matrizGrey[0][1] = 200,matrizGrey[1][0] = 125,matrizGrey[1] [1] = 0, 矩阵灰[2][0] = 255, 矩阵灰色[2][1] = 7,

如果我引入的节数为 2(这意味着我的范围将为 [0, 127]、[128, 255]),则结果需要为“4 2”。 这是一个小例子,但我需要对随机数量的部分和随机数量的像素执行此操作。

感谢您的关注。

我想我找到了一个更好的近似解决方案,但我仍然有问题,因为如果部分的数量很大,它不会显示所有元素。

if (strcmp(argv[1], "-u") == 0 && *argv[2] == '0' && strcmp(argv[3], "-t") == 0) {
  unsigned int sections = atoi(argv[4]);
  unsigned int histogram[sections] = {};
  unsigned int k = 0;
  float limits[sections];
  float value = (float)255/sections;

  if (sections > 0) {
    cout << "The number of sections " << sections << "\n";
    cout << "Each section is " << value << "\n";

    cout << "The array of limits is : " << "\n";   
    for (unsigned int i = 0; i < sections; ++i) {
        limits[i] = value*i;
        cout << limits[i] << " ";
    }

      for (unsigned int i = 0; i < length; ++i) {
        for (unsigned int j = 0; j < width; ++j) {
            px = matrizGrey[i][j];
            k = 0;
            while (k < sections) {
                if (px > limits[k] && px <= limits[k+1] ) {
                histogram[k]++;
                break;
                }
                else {
                k++;
                }   
            }
        }
      } 

    cout << endl;
    cout << "The final result will be: " << "\n";
    for (unsigned int i = 0; i < sections; ++i) {
        //histogram[i] = 0;
        cout << histogram[i] << " ";
    }

    cout << "\n";
  }