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, 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";
}
相关文章:
- 如何声明一个标准::提升直方图的向量?提升直方图的类型是什么?
- 二进制图像的空间直方图
- 如何在c++中仅使用for循环打印此代码的垂直直方图
- 创建直方图以显示每个类别/箱中带有星号的项目数量
- 打印带C++的直方图
- 将直方图的 2D 数组传递给函数 (C++)?
- 如何在OpenCV 3.4.7中计算CV_32FC1 cv::GpuMat图像的(范围)直方图
- 如何平行化直方图添加?
- 根C++直方图中的错误
- 动态分配许多 Boost::直方图对象
- 使用 OpenCV 原始指针和 lambda 作为直方图的不同结果
- 如何计算图像中每个块的直方图,然后将它们连接起来?
- C++:使用数组、指针和 for 循环制作直方图。我就在那里,我可能想得太辛苦了
- 将两个 1D 直方图合并为一个
- 使用STD :: MAP在直方图峰周围的平均值
- 使用标准库或Boost库从一组数据中计算直方图
- OPENCV MAT问题:直方图和此循环之间的差异
- 优化直方图更新
- 在OPENCV中归一化函数中的直方图
- 从直方图OPENCV中的峰中获取色调值