This question is challenging to get more understanding on Image Processing using pure C. I have done a simple program reading non-binary PGM file using C compiled with GCC. Now, it is becoming a problem when I try to read binary PGM file. This binary PGM file can be acquired by converting JPG to PGM using IrvanView.
NOTE: Please don't answer with any image processing library (such as: OpenCV).
My current code is:
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 1024
#define HEIGHT 768
#define READ_IMAGE_NAME "MY_PGM_FILE_NAME.pgm"
void print_histogram_table(int *histog);
main() {
FILE *fp;
int i,j, height= HEIGHT, width=WIDTH;
char line[100];
// Color depth is 255.
unsigned char pixel_value;
fp = fopen(READ_IMAGE_NAME,"r");
// get the first four lines.
fgets (line,100,fp);
fgets (line,100,fp);
fgets (line,100,fp);
fgets (line,100,fp);
// Histogram helper
int histo[65536];
int x;
for ( x =0; x < 65536; x++) {
histo[x] = 0;
}
for(j=0;j<height;j++) {
for(i=0;i<width;i++) {
fread(&pixel_value, sizeof(unsigned char), 1, fp);
// Turn on the code below, if you want to check color on specific row and column.
// printf("row num. %d column num. %d pixel value=%d
",j,i,pixel_value);
histo[pixel_value]++;
}
}
// Make histogram
print_histogram_table(histo);
fclose(fp);
getch();
}
void print_histogram_table(int *histog)
{
int x;
for (x= 0; x < 255; x++) {
if ( histog[x] != 0)
printf("Color number %d count %d
", x, histog[x]);
}
}
I have read some pages related to my problem [How to read .PGM format file?] , but I cannot find any clear and simple answer. I apologize for any mistake in my code. Any suggestion and critic regarding my code would be appreciated.
My script above could not display correct color histogram, because if you think rationally, you might get pixel color above 100 (not only below 100). So, the main question is How to fix this problem?
EDIT I
EDIT II
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…