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

    histogram of an image C kode

    I need to get a histogram of an image with the codes I have, but the codes give me errors when I write in visual studio.
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include<iostream>
    #include<conio.h>
    #include<iomanip>
    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);
    	}
    
    }
    
    
    
    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 hist[256])
    {
    	int i;
    	for (i = 0; i < 256; i++)
    		printf("%d \n", hist[i]);
    }

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

    Re: histogram of an image C kode

    main() has a return type of int, not void.

    You haven't provided the forward declarations for the functions used within main(). After the includes, you need
    Code:
    int get_image(char *filename, int im[][500], int *xsize, int *ysize);
    void find_histogram(int image[][500], int xsize, int ysize, int hist[256]);
    void print_histogram(int hist[256]);
    also
    Code:
    fscanf(infile, "%d %d", &xsize, &ysize);
    xsize and ysie are already pointers so you don't need the & to obtain an address - what you are doing here is obtaining the address of the pointer when you just need the pointer.
    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)

  3. #3
    Join Date
    Aug 2017
    Posts
    2

    Re: histogram of an image C kode

    My new codes are working and this works, but the screen prints 255 zeros. What is the reason of this?Name:  asddddd.jpg
Views: 962
Size:  21.3 KB
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.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]);
    		fclose(infile);
    		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("%d \n", hist[i]);
    }
    int 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");
    }

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

    Re: histogram of an image C kode

    It works for me using my test data

    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
    displays

    Code:
    Enter the image file name: hist.txt
    0
    5
    5
    5
    5
    5
    0
    0
    ....
    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)

  5. #5
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    It works for me using my test data

    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
    displays

    Code:
    Enter the image file name: hist.txt
    0
    5
    5
    5
    5
    5
    0
    0
    ....
    Can you give me running program's code? I have same problem

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

    Re: histogram of an image C kode

    Quote Originally Posted by yasiner View Post
    Can you give me running program's code? I have same problem
    It's the code from post #3 - no changes. VS2017.

    What's the data?
    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)

  7. #7
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    my codes gave me 256 units zero. VS2010 ULTIMATE.
    Name:  forum.jpg
Views: 924
Size:  19.2 KB
    I choosed an image file. But you wrote an txt file's name. Which one is true?

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

    Re: histogram of an image C kode

    My test file, based upon the input requirements of the program in post #3 are as in post #4. This test file is called hist.txt. If the program is not producing the expected output, then it is likely that the format of the data in the specified file is not as expected. If you attach to a post the input file used, I'll have a look.
    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)

  9. #9
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    My test file, based upon the input requirements of the program in post #3 are as in post #4. This test file is called hist.txt. If the program is not producing the expected output, then it is likely that the format of the data in the specified file is not as expected. If you attach to a post the input file used, I'll have a look.
    my input file a image file. max quality 500x500
    Name:  ad.PNG
Views: 853
Size:  6.2 KB

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

    Re: histogram of an image C kode

    Yes, but you haven't attached the image to the post as a file, just pasted it so I can't try it and have a look at the file.
    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)

  11. #11
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    Yes, but you haven't attached the image to the post as a file, just pasted it so I can't try it and have a look at the file.

    https://drive.google.com/open?id=0Bw...FRScUZXRFZhMjQ

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

    Re: histogram of an image C kode

    That file is not in the required format for the program - hence that's why the program is displaying all 0's.
    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)

  13. #13
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    Quote Originally Posted by 2kaud View Post
    That file is not in the required format for the program - hence that's why the program is displaying all 0's.
    what can i do for an image file? can you give me your running file?

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

    Re: histogram of an image C kode

    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("");
    }
    Last edited by 2kaud; August 5th, 2017 at 12:01 PM.
    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)

  15. #15
    Join Date
    Aug 2017
    Posts
    10

    Re: histogram of an image C kode

    So thanks for your help

Page 1 of 2 12 LastLast

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