c++中的直方图

Histogram in C++

本文关键字:直方图 c++      更新时间:2023-10-16

我正在用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, matrizGrey[2][0] = 255,matrizGrey[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";
  }