File input for histogram (programming)

This is a discussion on File input for histogram (programming) within the C Programming forums, part of the General Programming Boards category; Below is my program where I have been refine from some seniors ' advises from this board . I need ...

  1. #1
    Unregistered
    Guest

    File input for histogram (programming)

    Below is my program where I have been refine from some seniors ' advises from this board . I need to write a program to count the max ,min ,average and print the histogram for a image .(513row*513column ,263169pixel ).
    However , when I check the result from the output is different from the given answer .

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

    int main ()

    {
    int histogram[256],i,ch,max,min,sum;
    sum=0;max=0;min=256;

    FILE *fp;
    for ( i = 0 ; i < 256 ; i++ )
    {histogram[i] = 0;}

    fp = fopen( "a.img", "rb" ); /* input is image file */

    while ( (ch=fgetc(fp)) != EOF )
    {histogram[ch/32]++;}

    fclose( fp );

    for ( i = 0 ; i < 256 ; i++ )
    {histogram[i] = 0;}

    fp = fopen( "a.img", "rb" );

    while ( (ch=fgetc(fp)) != EOF )
    { histogram[ch/32]++;
    sum += ch;
    if ( ch > max ) max = ch;
    if ( ch < min ) min = ch; }
    fclose( fp );

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

    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 like that .

    0 :2*
    1 :3*
    2 :5* /* until 256 */
    255:3*

    Total pixel values =====>1782
    Maximum value of the pixel===>255
    Minimum value of the pixel===>0
    Average====>0

    =====================================

    My problem :

    1. The program can't count the frequency of pixel value correctly .
    I think I should change my input file to other format . What format should I import or export from the image file (*.img) to ?

    2. The result of sum and average value is uncorrected . Can I fix the problem by changing file input format or change some command in the program ?

    3. Although I have put the command "histogram[ch/32]++;" , my output still show the frequency of each pixel value . How can I change to the form like this :

    0-31 : 12
    32-63 : 8
    64-95 :88
    /*utnil*/
    223-255:15

    4. How can I put the symbol * according to the repeatness ,like
    0-31 : 12 ************
    32-63 : 8 ******** and etc .

    Thanks you for reading .

  2. #2
    Unregistered
    Guest

    Unhappy Anybody can give advises ?

    Still looking for solutions . Hope seniors can give some commands and advises . Thank you .

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >histogram[ch/32]++;
    These are incorrect, I believe. If you are trying to count the occurences of each byte value within the file, do this instead:
    >histogram[ch]++;

    To determine the average value, you need to know the number of bytes in the file. You could do this various ways, here's one:
    Code:
    fp = fopen("a.img", "rb");
    /* error check the fopen */
    fseek( fp, 0L, SEEK_END );
    size_of_file = ftell( fp );
    fseek( fp, 0L, SEEK_SET );
    /* perform loop using fgetc() */
    ......
    
    printf("\n\nAverage====%d", sum / size_of_file);
    I'd recommend using a long int for the sum variable, as a normal int might overflow.

    Hope this helps....

    And don't bump your threads!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest

    Unhappy file input problem ?

    Dear Hammer , below is my refined program according to you suggestion .

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

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

    FILE *fp;

    fp = fopen("subset2.img", "rb");
    fseek( fp, 0L, SEEK_END );
    size_of_file = ftell( fp );
    fseek( fp, 0L, SEEK_SET );
    fclose(fp);

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

    for ( i = 0 ; i < 256 ; i++ )
    {histogram[i] = 0;}
    fp = fopen( "a.img", "rb" );
    while ( (ch=fgetc(fp)) != EOF ) {
    histogram[ch]++;
    sum += ch;
    if ( ch > max ) max = ch;
    if ( ch < min ) min = ch;
    }

    fclose( fp );

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


    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/size_of_file);

    }

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

    problems :

    1. My program still didn't show a oputput correctly . For example , there are not pixel value is 0 , but it show there are some frequency for that particular pixel value . And plus , this influence my max and min answer . All the value in output is uncorrect . Show I export my image input to other format ? What format is it ?

    2. How can I show the frequency in the range of 0-31 , 32-63 and etc .

    3. How can I use symbol * to represent the repeatness of the particular pixel value ?

    Thank again .

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here, have a look at this version, and see if it does what you want.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int         histogram[256], i, ch, max, min, tempsum;
        long int    size_of_file, sum;
        FILE        *fp;
        sum = 0;
        max = -1;
        min = 257;
    
        for (i = 0; i < 256; i++)
        {
            histogram[i] = 0;
        }
    
        fp = fopen("a.img", "rb");
    
        if (fp == NULL)
        {
            perror("a.img");
            return 1;
        }
    
        fseek(fp, 0L, SEEK_END);
        size_of_file = ftell(fp);
        fseek(fp, 0L, SEEK_SET);
    
        if (size_of_file == 0)
        {
            printf("Empty file\n");
            fclose(fp);
            return 1;
        }
    
        while ((ch = fgetc(fp)) != EOF)
        {
            histogram[ch]++;
            sum += ch;
            if (ch > max) max = ch;
            if (ch < min) min = ch;
        }
    
        fclose(fp);
    
        for (i = 0, tempsum = 0; i < 256; i++)
        {
            tempsum += histogram[i];
            if ((i + 1) % 32 == 0)
            {
                int j;
                printf("%03d:%03d %5d ", i - 31, i, tempsum);
                for (j = 0; j < tempsum; j++) putchar('*');
                putchar('\n');
                tempsum = 0;
            }
        }
    
        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 / size_of_file);
    
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest

    Thank Hammer .

    Thank you for your advise . I will try at home later . Now I'm in campus . Have a nice day .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21