CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    For a binary file like this, consider
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main()
    {
    	char filename[100] = { 0 };
    	unsigned int histogram[256] = { 0 };
    	FILE* infile;
    
    	printf("Enter the image file name: ");
    	scanf("%99s", filename);
    
    	if ((infile = fopen(filename, "rb")) == NULL) {
    		puts("file error");
    		return 1;
    	}
    
    	for (int ch = 0; (ch = fgetc(infile)) >= 0; ++histogram[ch % 256]);
    	for (int h = 0; h < 256; printf("%i ", histogram[h++]));
    	puts("");
    }
    Actually i need to write this program with my codes. Because it is my homework but i tried with your codes and i couldn't it. i want to fix my codes. Can you help me to fix my codes?
    My codes:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>

    int get_image(char *filename, int im[][500], int *xsize, int *ysize)
    {
    FILE *infile;
    int i,j;
    if((infile=fopen(filename,"r"))!=NULL)
    {
    fscanf(infile, "%d %d", xsize, ysize);
    for (i=0; i<*ysize; i++)
    for(j=0; j<*xsize;j++)
    fscanf(infile,"%d", &im[i][j]);
    return 1; }
    else
    return 0;
    }

    void find_histogram(int image[][500], int xsize,int ysize, int hist[256])
    {
    int i,j;
    for(i=0;i<256;hist[i++]=0);
    for(i=0;i<ysize;i++)
    for(j=0;j<xsize;j++)

    hist[image[i][j]++];

    }

    void print_histogram(int his[256])
    {
    int i;
    for(i=0;i<256;i++)
    printf("Hist[%d]=%d\n",i,his[i]);
    }

    void main(void)
    {
    char filename[100];
    int image[500][500], histogram[256], xsize, ysize,file_ok;
    printf("Enter the image file name:");
    scanf("%s",filename);
    file_ok=get_image(filename, image, &xsize, &ysize);
    if(file_ok==0)
    {
    printf("File error.\n");
    }
    else{
    find_histogram(image, xsize, ysize, histogram);
    print_histogram(histogram);

    }
    system("PAUSE");
    }

  2. #17
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int get_image(char *filename, int im[][500], int *xsize, int *ysize)
    {
    	FILE *infile;
    	int i,j;
    	if((infile=fopen(filename,"r"))!=NULL)
    	{
    		fscanf(infile, "%d %d", xsize, ysize);
    		for (i=0; i<*ysize; i++)
    			for(j=0; j<*xsize;j++)
    				fscanf(infile,"%d", &im[i][j]);
    		return 1; }
    	else
    		return 0;
    }
    
    void find_histogram(int image[][500], int xsize,int ysize, int hist[256])
    {
    	int i,j;
    	for(i=0;i<256;hist[i++]=0);
    	for(i=0;i<ysize;i++)
    		for(j=0;j<xsize;j++)
    		
    			hist[image[i][j]++];
    
    }
    
    void print_histogram(int his[256])
    {
    	int i;
    	for(i=0;i<256;i++)
    		printf("Hist[%d]=%d\n",i,his[i]);
    }
    
    void main(void)
    {
    	char filename[100];
    	int image[500][500], histogram[256], xsize, ysize,file_ok;
    	printf("Enter the image file name:");
    	scanf("%s",filename);
    	file_ok=get_image(filename, image, &xsize, &ysize);
    	if(file_ok==0)
    	{
    		printf("File error.\n");
    	}
    	else{
    		find_histogram(image, xsize, ysize, histogram);
    		print_histogram(histogram);
    		
    	}
    	system("PAUSE");
    }

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: histogram of an image C kode

    i want to fix my codes. Can you help me to fix my codes?
    The premise of your code is wrong. You are assuming the format of the file used is
    Code:
    x y d d d ...
    where x, y are the xsize and ysize and d is the date value as decimal values. This would give as an example a file of format
    Code:
    5 5
    1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
    but a .png file isn't in this format - and so this simple code can't be used for a file that isn't in the expected format.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    The premise of your code is wrong. You are assuming the format of the file used is
    Code:
    x y d d d ...
    where x, y are the xsize and ysize and d is the date value as decimal values. This would give as an example a file of format
    Code:
    5 5
    1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
    but a .png file isn't in this format - and so this simple code can't be used for a file that isn't in the expected format.

    I wrote a new code but i have a mistake again.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int get_image(char *filename, int im[][500], int *xsize, int *ysize)
    {
    	FILE *infile;
    	int i,j;
    	if((infile=fopen(filename,"r+"))!=NULL)
    	{
    		fscanf(infile, "%d %d", xsize, ysize);
    		printf("\nwidth: %d, height: %d \n", *xsize,*ysize);
    
    		for (i=0; i<*ysize; i++)
    			for(j=0; j<*xsize;j++)
    				fscanf(infile,"%d", &im[i][j]);
    		return 1; }
    	else
    		return 0;
    }
    
    void find_histogram(int image[][500], int xsize,int ysize, int hist[256])
    {
    	int i,j;
    	for(i=0;i<256;hist[i++] = 0);
    
    	for(i=0;i<ysize;i++)
    		for(j=0;j<xsize;j++)
    		
    			hist[image[i][j]]++;
    
    }
    
    void print_histogram(int hist[256])
    {
    	int i;
    	for(i=0;i<256;i++)
    		printf("Hist[%d]=%d\n",i+1,hist[i]);
    }
    
    int main()
    {
    	char filename[100], path[100] = "C:\\Images\\";
    	int image[500][500], histogram[256], xsize, ysize,file_ok;
    	printf("Enter the image file name:");
    	scanf("%s",filename);
    
    	strcat(path, filename);
    	strcpy(filename, path);
    	strcat(filename, ".ascii.pgn");
    
    	file_ok=get_image(filename, image, &xsize, &ysize);
    
    	if(file_ok==0)
    	{
    		printf("File error.\n");
    	}
    	else
    	{
    		find_histogram(image, xsize, ysize, histogram);
    		print_histogram(histogram);
    		
    	}
    	getchar();
    	getchar();
    
    
    }

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: histogram of an image C kode

    Like I said in post #18, with a .png file your algorithm used is not correct. You can't read xsize and ysize from the file and then read numbers. The format of a .png file isn't like that. For a binary file like a .png file you ned to do it similar to the code in post #14.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #21
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    Like I said in post #18, with a .png file your algorithm used is not correct. You can't read xsize and ysize from the file and then read numbers. The format of a .png file isn't like that. For a binary file like a .png file you ned to do it similar to the code in post #14.
    so, what can ı do for a png or jpg file?

  7. #22
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: histogram of an image C kode

    Use the code in post #14!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #23
    Join Date
    Jul 2017
    Posts
    14

    Re: histogram of an image C kode

    Thanks for this thread, it helped me aswell

  9. #24
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: histogram of an image C kode

    Quote Originally Posted by Larnaco View Post
    Thanks for this thread, it helped me aswell
    Are you all doing the same course with the same assignment? What, actually, is the assignment?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    Are you all doing the same course with the same assignment?
    I guess, no.
    All (s)he is doing here at CG is, IMHO, some type of trollng.
    Just look at his/her posts!
    Victor Nijegorodov

  11. #26
    Join Date
    Jul 2017
    Posts
    14

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    For a binary file like this, consider
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main()
    {
    	char filename[100] = { 0 };
    	unsigned int histogram[256] = { 0 };
    	FILE* infile;
    
    	printf("Enter the image file name: ");
    	scanf("%99s", filename);
    
    	if ((infile = fopen(filename, "rb")) == NULL) {
    		puts("file error");
    		return 1;
    	}
    
    	for (int ch = 0; (ch = fgetc(infile)) >= 0; ++histogram[ch % 256]);
    	for (int h = 0; h < 256; printf("%i ", histogram[h++]));
    	puts("");
    }
    I think this is a good solution

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured