Still can't print a Histogram from fail input

This is a discussion on Still can't print a Histogram from fail input within the C Programming forums, part of the General Programming Boards category; Althogh I have spend the whole day to my homework . I still can't made it . I need to ...

  1. #1
    Unregistered
    Guest

    Unhappy Still can't print a Histogram from fail input

    Althogh I have spend the whole day to my homework . I still can't made it . I need to print out a histogram from a image(subset1.img),there are array 512X512 or 262144 pixel value in the image . However , the array saiz is too large . How can I solve it . Besides , I really don't understand the command to print a histogram , it's quite complicated to me . I would like to print a histogram with bin range of 20 ( 0-20 , 21-40,41-60 and so on ) and each star '*' represent 25 frequency . I already try my best . I hope seniors can help me to produce the histogram . Below is my program code .

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

    #include <stdio.h> include <conio.h> include <math.h>

    FILE *input;

    void total (void),average ( void ), range ( void ) ,histogram ( void ) ;
    float aveg;
    int dn,sum,min,maks,choose=10;

    main()
    {
    min=1000;maks=0;
    input = fopen ("subset1.img","a+");

    printf("\n The Pixel Value Distribution Are As Below \n");

    total();range() ;average();histogram () ;

    printf(" \n\n\n End ! \n");

    getch ();
    fclose(input);
    return (0);
    }

    void total()
    {
    int i, j, sum = 0, subset1[512][512];/*there are 262144 pixel value to analysis*/
    input = fopen ("subset1.img","a+"); /*in array 512 X 512 , but there is a error*/
    for (i=0; i<512; i++) /*where the array saize is too large */
    for (j=0; j<512; j++)
    fscanf(input, "%d", &subset1[i][j]);
    for (i=0; i<512; i++)
    for (j=0; j<512; j++)
    {sum +=subset1[i][j];}
    fclose(input);
    }


    void range()
    {
    int i, j, subset1[512][512];
    sum=0;
    input = fopen ("subset1.img","a+");
    for (i=0; i<512; i++)
    for (j=0; j<512; j++)
    {
    fscanf(input, "%d", &subset1[i][j]);
    if(subset1[i][j]>maks)
    maks=subset1[i][j];
    if(subset1[i][j]<min)
    min=subset1[i][j];
    }
    for (i=0; i<512; i++)
    for (j=0; j<512; j++)
    {sum +=subset1[i][j];}
    fclose(input);
    }

    void average()
    {
    input = fopen ("subset1.img","a+");
    aveg=sum/262144;
    fclose(input);
    }

    void histogram ()
    {
    input = fopen ("subset1.img","a+"); /*I really don't know how to printf the */
    for (i=0; i<512; i++) /*histogram which the bin range of 20*/
    for (j=0; j<512; j++) /*from the range 0-255pixel value */
    } /*and each star '*' represent 25 frequency*/

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > fscanf(input, "%d", &subset1[i][j]);
    First off, we need to know exactly what format your file is in. You're assuming from this that your input file is ASCII text consisting of decimal numbers separated by spaces and newlines, like so

    1 2 3
    4 5 6

    Now your average .img file doesn't sound like a text file, but more like a binary file containing exactly 512x512 bytes, with absolutely no formatting. If this is so, then fscanf is not the right thing to be using.

    > subset1[512][512];
    I see you're attempting to store the whole file when there is no need to store the whole file. You can read the whole file one value at a time, and decide which histogram slot to update based on that value.

    There is also no sign of something like
    int histogram[20];

  3. #3
    Unregistered
    Guest

    Unhappy Dear Salem

    >First off, we need to know exactly what format your file is in.
    The fail I use is image fail . I have tried to copy the pixel value from the image to textpad into format filename.dat , but the fail is too big . Textpad is not afford for that . How can I format my image into ASCII format ?

    > fscanf is not the right thing to be using
    If fscanf is not the right for reading image fail input , what command should I use ?

    >You can read the whole file one value at a time, and decide which histogram slot to update based on that value.
    Actually I can't do that , the array saiz is too large , I can't compiles it .

    >There is also no sign of something like 'int histogram[20];'
    Where should I define it . I'm really don't know how to do that . I have tried to get the histogram programming code , but I can't understand . And plus , input failname.img can not be compiled ( too large ) . If I try array saiz of [20][20] and the format fail is filename.dat , I can get both the max and min pixels value .
    How could I read with the format failname.img ? And what the simplest command to print the histogram .

  4. #4
    Unregistered
    Guest

    Unhappy Still looking for solution !

    What the format of input fail I need to use ?
    Still can get the point to print a histogram , did I need to use array to do that or use if......else command ?
    What parameter I should define ?
    A beginner must have lots of problems loh .

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > Actually I can't do that , the array saiz is too large , I can't compiles it .
    The array you specified is the file, not the histogram


    Code:
    #include <stdio.h>
    
    int main ( ) {
        int histogram[256];
        int i;
        FILE *fp;
        int ch;
    
        // initialise
        for ( i = 0 ; i < 256 ; i++ ) {
            histogram[i] = 0;
        }
    
        // read the file, and build the histogram
        fp = fopen( "a.exe", "rb" );
        while ( (ch=fgetc(fp)) != EOF ) {
            histogram[ch]++;
        }
        fclose( fp );
    
        // print it
        for ( i = 0 ; i < 20 ; i++ ) {
            printf( "%2d: %d\n", i, histogram[i] );
        }
    }
    A simple start, showing how to read a binary file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. print input parameter values
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 06-30-2008, 03:33 AM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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