Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One very primitive way to try to identify what is pictured in an image is to loo

ID: 3763192 • Letter: O

Question

One very primitive way to try to identify what is pictured in an image is to look at the image's histogram. A histogram summarizes a collection of numeric data by counting how many times the different data values appear in the data. It is very common for histograms to count data values in ranges (intervals), which are also called bins. In C++, a histogram can be stored in a 1D array of integers.

The histogram of a grayscale image with floating point intensity values consists of a count of how may pixels there are in a given intensity range. For example, we could divide the range of intensities between 0.0 and 1.0 into ten equal ranges (bins for values: 0.0-0.1, 0.1-0.2, 0.2-0.3, etc), and count the number of pixels whose intensity is in each range. The histogram shows the distribution of graylevel values within an image, which can sometimes tell us something about the objects or scene pictured in the image, particularly if different objects or scenes of interest have quite different grayscale distributions.

In C++ we can store a grayscale image in a 2-dimensional array of type floatdouble (use type double throughout this question instead of floats -- we have discovered problems with using float in this question; still use integers where it says to use integers). Here's an example of the data for image that is 5 pixels wide and 5 pixels high as a C++ array:

To compute the histogram of this image

Count the number of pixels whose intensity is greater or equal to 0.0 and less than 0.1; there are four such pixels (the corners).

Count the number of pixels with intensity greater or equal to 0.1 and less than 0.2. There are no such pixels, so the second number in the histogram is 0.

Count the number of pixels with intensity greater or equal to 0.2 and less than 0.3. There are 8 such pixels (the ones with intensity 0.2 and 0.25), so the third number in the histogram would be 8.

This proceeds in the same way for the remainder of the ten equal ranges between 0.0 and 1.0.

Note: the last interval includes 1.0, so the last number in the histogram is the number of pixels greater or equal to 0.9 and less than or equal to 1.0.

Convince yourself that the histogram for the above image is the sequence of ten numbers: 4, 0, 8, 0, 2, 4, 2, 0, 2, 3. Note that the ten numbers in the histogram should add up to the number of pixels in the image since each pixel in the image contributes a count of 1 to exactly one bin. Note how 4+0+8+0+2+4+2+0+2+3 = 25, which is the number of pixels in the original 5x5 image.

Your Tasks:

Write a function called fillHistogram that takes three parameters: I: a 2D array of type double with a fixed width 5 (but can be of any height); height: an integer specifying the height of I; and H: a one-dimensional integer array of length 10, i.e., the histogram array. The function should fill H with the values for the histogram of the image I.

In your main() function:

Create a 2D array for the 5x5 image shown in the above example.

Use your histogram function to construct its histogram and print the histogram to the console to verify the correctness of your function.

Make up an second image with width 5 and height 10, declare and initialize an array to store it, and use your function from step 1 to compute its histogram, and print it to the console.

Explanation / Answer

If you have any further queries, just get back to me.

#include <iostream>
#include <iomanip>
using namespace std;

void histogram(double image[5][10], int rows, int cols)
{
int count[10];
for(int i = 0; i < 10; i++)
count[i] = 0;
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
{
if(image[i][j] == 1.0)
count[9]++;
else
count[(int)(image[i][j]*10)]++;
}
cout<<"The count array: ";
for(int i = 0; i < 10; i++)
cout<<count[i]<<" ";
  
cout<<endl<<"The histogram is: "<<endl;
for(int i = 0; i < 10; i++)
{
cout<<fixed<<setprecision(1)<<i/10.0<<" - "<<fixed<<setprecision(1)<<(i+1)/10.0<<" ";
for(int j = 0; j < count[i]; j++)
cout<<"*";
cout<<endl;
}
}
int main()
{
double image1[5][5] = {0.00, 0.25, 0.50, 0.25, 0.00
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.25, 0.50, 1.00, 0.50, 0.25
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.00, 0.25, 0.50, 0.25, 0.00};
  
double image2[5][10];
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
image2[i][j] = image1[i][j];
histogram(image2, 5, 5);
double image3[5][10] = {0.00, 0.25, 0.50, 0.25, 0.00
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.25, 0.50, 1.00, 0.50, 0.25
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.00, 0.25, 0.50, 0.25, 0.00
                           ,0.00, 0.25, 0.50, 0.25, 0.00
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.25, 0.50, 1.00, 0.50, 0.25
                           ,0.20, 0.40, 0.60, 0.80, 1.00
                           ,0.00, 0.25, 0.50, 0.25, 0.00};

for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
image2[i][j] = image3[i][j];
histogram(image2, 5, 10);
}