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