-
August 3rd, 2017, 08:24 AM
#1
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]);
}
-
August 3rd, 2017, 11:45 AM
#2
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)
-
August 3rd, 2017, 12:12 PM
#3
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?
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");
}
-
August 3rd, 2017, 01:32 PM
#4
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)
-
August 5th, 2017, 06:46 AM
#5
Re: histogram of an image C kode
Originally Posted by 2kaud
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
-
August 5th, 2017, 07:21 AM
#6
Re: histogram of an image C kode
Originally Posted by yasiner
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)
-
August 5th, 2017, 07:46 AM
#7
Re: histogram of an image C kode
my codes gave me 256 units zero. VS2010 ULTIMATE.
I choosed an image file. But you wrote an txt file's name. Which one is true?
-
August 5th, 2017, 08:08 AM
#8
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)
-
August 5th, 2017, 08:18 AM
#9
Re: histogram of an image C kode
Originally Posted by 2kaud
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
-
August 5th, 2017, 08:24 AM
#10
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)
-
August 5th, 2017, 08:32 AM
#11
Re: histogram of an image C kode
Originally Posted by 2kaud
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
-
August 5th, 2017, 08:53 AM
#12
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)
-
August 5th, 2017, 09:05 AM
#13
Re: histogram of an image C kode
Originally Posted by 2kaud
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?
-
August 5th, 2017, 11:40 AM
#14
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)
-
August 5th, 2017, 01:03 PM
#15
Re: histogram of an image C kode
So thanks for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|