giorgioW - 3 months ago 48

C++ Question

I'm programming an histogram for an determinate bidimensional array (which is filled with numbers that represent a numeric value of a color in a pixel -> matrizGrey) in C++. You have to introduce by parameter (argv[4]) the number of sections that the histogram will have, and the range is between 0 and 255.

So, depending on the number of sections, the histogram will change. I catched how it works (comparing the matrix element between the first element of the range and the next one) but my problem is that I can't do it recursive.

The following code show my progress at this moment:

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

}

For example, with this "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,

If I introduce a section number of 2 (which mean that my range will be [0, 127], [128, 255]) the result needs to be "4 2".

This is a little example, but I need to do this for a random number of sections and for a random number of pixels too.

Thanks for your attention.

Answer

I think I found a better aproximation to the solution, but I still have problems, because if the number of sections is big, it doesn't show all the elements.

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