C ++中的直方图

时间:2016-11-04 19:35:02

标签: c++ arrays image c++11 histogram

我在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] << " ";
    }

例如,使用此&#34;图像&#34;:matrizGrey [0] [0] = 100,matrizGrey [0] [1] = 200,matrizGrey [1] [0] = 125,matrizGrey [1 ] [1] = 0,matrizGrey [2] [0] = 255, matrizGrey [2] [1] = 7,

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

感谢您的关注。

1 个答案:

答案 0 :(得分:1)

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

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";
  }