-
August 6th, 2017, 04:34 PM
#16
Re: histogram of an image C kode
Originally Posted by 2kaud
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");
}
-
August 6th, 2017, 04:42 PM
#17
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");
}
-
August 7th, 2017, 04:50 AM
#18
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
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)
-
August 7th, 2017, 05:18 AM
#19
Re: histogram of an image C kode
Originally Posted by 2kaud
The premise of your code is wrong. You are assuming the format of the file used is
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();
}
-
August 7th, 2017, 05:43 AM
#20
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)
-
August 7th, 2017, 05:47 AM
#21
Re: histogram of an image C kode
Originally Posted by 2kaud
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?
-
August 7th, 2017, 05:58 AM
#22
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)
-
August 7th, 2017, 07:59 AM
#23
Re: histogram of an image C kode
Thanks for this thread, it helped me aswell
-
August 7th, 2017, 08:23 AM
#24
Re: histogram of an image C kode
Originally Posted by Larnaco
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)
-
August 7th, 2017, 09:23 AM
#25
Re: histogram of an image C kode
Originally Posted by 2kaud
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
-
August 8th, 2017, 02:26 AM
#26
Re: histogram of an image C kode
Originally Posted by 2kaud
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
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
|