# Max & Min value and refine histogram

• 07-19-2002
Unregistered
Max & Min value and refine histogram
Hi again . I need to write a program to calculate maximum , minimum , average pixel value for a image file ( 513 row X 513 columm , there are 263169 pixel ) . Besides , a histogram for the pixel distribution is needed to print . Below is my whole program .

*************************************************
#include <stdio.h>

int histogram[256] , i , ch , max , min , sum ;
FILE *fp;

int main ()
{
int histogram[256] , i , ch , sum ;
sum=0 ;

FILE *fp;

for ( i = 0 ; i < 256 ; i++ )
{histogram[i] = 0;}
fp = fopen( "filename.img", "rb" );
while ( (ch=fgetc(fp)) != EOF )
{histogram[ch]++;}
fclose( fp );

for ( i = 0 ; i < 256 ; i++ )
{printf( "%2d: %d * \n", i, histogram[i] , histogram[i] );}

{sum +=histogram[256];}

printf("\n\nTotal pixel values ===>%d" , sum);
printf("\n\nMaximum value of the pixel===>%d",max);
printf("\n\nMinimum value of the pixel===>%d",min);
printf("\n\nAverage====%d",sum/263169);

}

**************************************************
The output is as below :

0: 2 * //0-255 is pixel value , 2*,100* is the frequency for that particular pixel value .//
1: 100*
3: 5*
(0 until 255)
255: 8*

Total pixel value ===>1306
Maximum value for the pixel ===>0
Minimum value for the pixel ====>0
Average =====>0

**************************************************

1. How can I print histogram output like this (or similiar format ):

*
*...........*
*...........*.....* .....................*
*...........*.....*.....*...............*......... .* ..........................
( 202) (150) (98) . ( 48 ) ... ( 102 ) . ( 50) .. .( 10 ) .. ( 0 )
-----------------------------------------------------------------------------------
0-31 32-63 64-95 96-127 128-159 160-191 192--223 224-255

where one * represent 50 repeatness . For example , there are 202 pixel value in the range of 0-31 and represented by **** .

2. Besides , this program can't not show the correct total pixel value , max , min and average pixel value .
For example , the correct total pixel value is 32895940 , but the program show 1306 . How to solve these problems ?

3. Thank a lot to read this .

Have a nice day :P
• 07-19-2002
Unregistered
Any solution ?
I still try to get it work .......
• 07-19-2002
Salem
> int histogram[256] , i , ch , max , min , sum ;
> FILE *fp;
These global variables are redundant, since you also declare them inside main

Well except for max and min, which you should also move to main as well

Before you read the file, set the max and min values like so
min = 256;
max = 0;

Then make your while loop like this
Code:

```while ( (ch=fgetc(fp)) != EOF ) {     histogram[ch]++;     sum += ch;     if ( ch > max ) max = ch;     if ( ch < min ) min = ch; }```
> {sum +=histogram[256];}
Remove this, it does nothing but pick up an illegal array element.

> 1. How can I print histogram output like this (or similiar format ):
Well rather than having an histogram of all values, create a histogram for ranges of values, which you would do like so

histogram[ch/32]++;

This would make histogram[0] the sum of all the pixel values in the range 0..31, histogram[1] the sum of all the pixel values in the range 32..63 etc
• 07-20-2002
Unregistered
Thank Salem !
Thanks you Salem . You are very friendly and kindly . I try to put your solution into my program . Thank you . :P
• 07-20-2002
red_baron
i made a program a little similar to that once for an assignment for school, its not very well coded but i was very new to programming at the time, basically its got a nice histogram (well at least i think so :D )