-1

I need to create a single greyscale histogram of a PGM image for a class project. I already have the code to read the PGM image. However I don't completely understand how to count the unique pixel values. I don't need to output a graph or anything like that. Any help would be greatly appreciated!

Code for reading PGM image http://sun.iwu.edu/~shelley/sie/zoo/journal/pgm.c.html

richieboy89
  • 1
  • 1
  • 2
  • You need to be more specific - do you want separate histograms for the R/G/B components, or do you want a single histogram for intensity, or some other quantity ? – Paul R Jul 20 '13 at 19:25
  • I believe one single histogram. The assignment just says to compute the greyscale histogram. – richieboy89 Jul 20 '13 at 19:29
  • OK - except the code you linked to seems to create an RGB image. If your input file is greyscale though then I presume the R/G/B components will all have the same value ? If so then you can just histogram one of them ? – Paul R Jul 20 '13 at 19:33
  • potential duplicated question http://stackoverflow.com/questions/7455343/displaying-histogram-pixel-values-from-pgm-image-p5-using-pure-c-without-any-i – rhzs Aug 30 '13 at 23:52

1 Answers1

2

Pseudo code, assuming 8 bit greyscale image:

int histogram[256] = { 0 };     ; init histogram of all possible grey values to all zero

for i = 0 to rows - 1           ; for each row
    for j = 0 to cols - 1       ; for each col
        p = image[i][j]         ; get pixel value
        histogram[p]++          ; increment histogram bin
Paul R
  • 208,748
  • 37
  • 389
  • 560