Hello, Im programming an image reader and I need to print out a histogram that shows how many times a given pixel value is present in a image - actually it doesn't have to be the graph, just a table with two columns would be enough

i have written a class for the whole program but i have everything else working except the histogram, any suggestions would be appreciated

# include <iostream>						
# include <fstream>
# include <string>
# include <cmath>
# include <iomanip>
# include <ctime>

using namespace std;

class image									
{
private:									
    int height, width, depth;
    char header[100];
    char line[100];
    char comment;
    short **data;
    
public:										
    ifstream fin;
    ofstream fout;
    string filename;


    void reader();							
    void writer();
    void rotate_clock();
    void rotate_anti();
    void resize();
    void histogram();
    void delete_p();
};

and heres the function i have written

void image::histogram()
{
	int counter[256];

	for (int z=0; z<256; z++)
	{
		counter[z]=0;
	}
	counter[264]=depth;

	for (int value=0; value<=depth; value++)
	{
		cout<<setw(7)<<value<<setw(17)<<counter[depth]<<endl;
		counter[depth]++;
	}
}

i have no idea how to get it to work

Dani AI

Generated

: the histogram needs two things you currently do not do — scan every pixel in data and increment a counter keyed by the pixel value. was right in principle: count occurrences per pixel value. The posted function instead used out‑of‑range indices and printed/incremented the wrong slot.

Use a counter array sized to the image's maximum value (not a hardcoded 256 unless the format guarantees 8 bits). Check data and depth first, then walk every pixel and increment the appropriate bin. After counting, print value / count pairs. Example (safe, simple approach):

void image::histogram()
{
    if (height <= 0 || width <= 0 || depth < 0 || data == nullptr) return;

    std::vector<unsigned long long> counts((size_t)depth + 1, 0ULL);

    for (int r = 0; r < height; ++r)
        for (int c = 0; c < width; ++c) {
            int v = data[r][c];
            if (v >= 0 && v <= depth)
                ++counts[v];
            else {
                // optional: handle unexpected values (log, clamp, or skip)
            }
        }

    for (int v = 0; v <= depth; ++v)
        std::cout << std::setw(7) << v << std::setw(17) << counts[v] << '\n';
}

Troubleshooting notes: if data uses signed short, cast to int before indexing; for very large images use 64‑bit counters; for color images build separate histograms per channel; and ensure depth really is the maximum pixel value, not the number of bytes or channels.

> char comment;
How big a comment?

> counter[264]=depth;
Do array bounds mean anything to you?

What is depth?

The basics for a histogram is
counter[pixel]++
for all pixels

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.