Guest User

Untitled

a guest
Jun 4th, 2012
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. Displaying histogram pixel values from PGM image (p5) using Pure C without any image processing library
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define WIDTH 1024
  5. #define HEIGHT 768
  6. #define READ_IMAGE_NAME "MY_PGM_FILE_NAME.pgm"
  7.  
  8. void print_histogram_table(int *histog);
  9.  
  10. main() {
  11. FILE *fp;
  12.  
  13. int i,j, height= HEIGHT, width=WIDTH;
  14. char line[100];
  15.  
  16. // Color depth is 255.
  17. unsigned char pixel_value;
  18.  
  19. fp = fopen(READ_IMAGE_NAME,"r");
  20.  
  21. // get the first four lines.
  22. fgets (line,100,fp);
  23. fgets (line,100,fp);
  24. fgets (line,100,fp);
  25. fgets (line,100,fp);
  26.  
  27. // Histogram helper
  28. int histo[65536];
  29. int x;
  30. for ( x =0; x < 65536; x++) {
  31. histo[x] = 0;
  32. }
  33.  
  34. for(j=0;j<height;j++) {
  35. for(i=0;i<width;i++) {
  36. fread(&pixel_value, sizeof(unsigned char), 1, fp);
  37. // Turn on the code below, if you want to check color on specific row and column.
  38. // printf("row num. %d column num. %d pixel value=%dn",j,i,pixel_value);
  39.  
  40. histo[pixel_value]++;
  41. }
  42. }
  43.  
  44. // Make histogram
  45. print_histogram_table(histo);
  46.  
  47. fclose(fp);
  48. getch();
  49. }
  50.  
  51. void print_histogram_table(int *histog)
  52. {
  53. int x;
  54. for (x= 0; x < 255; x++) {
  55. if ( histog[x] != 0)
  56. printf("Color number %d count %dn", x, histog[x]);
  57. }
  58. }
  59.  
  60. int histo[65536];
  61. // ...
  62. print_histogram_table(histo);
  63. // ...
  64. void print_histogram_table(int *histog)
  65. {
  66. int x;
  67. for (x= 0; x < 255; x++) {
  68. if ( histog[x] != 0)
  69. printf("Color number %d count %dn", x, histog[x]);
  70. }
  71.  
  72. fp = fopen(READ_IMAGE_NAME,"r");
  73. // change to:
  74. fp = fopen(READ_IMAGE_NAME,"rb");
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79.  
  80. int main(int argc, char* argv[])
  81. {
  82. char line[100];
  83. int width, height, maxval;
  84. unsigned char* image;
  85. unsigned int* histo;
  86. int i;
  87. FILE *fd;
  88.  
  89. fd = fopen(argv[1], "rb");
  90. if (fd == NULL) {
  91. printf("could not open image filen");
  92. exit(1);
  93. }
  94.  
  95. // magic number
  96. fgets(line, sizeof(line), fd);
  97. if (strcmp(line, "P5n") != 0) {
  98. printf("image is not in PGM format!n");
  99. fclose(fd);
  100. exit(1);
  101. }
  102.  
  103. // skip comment
  104. fgets(line, sizeof(line), fd);
  105.  
  106. // read header
  107. if (fscanf(fd, "%d %d %dn", &width, &height, &maxval) != 3) {
  108. printf("invalid headern");
  109. fclose(fd);
  110. exit(1);
  111. }
  112. if (maxval > 255) {
  113. printf("sorry, only 8-bit PGMs are supported at this time!n");
  114. fclose(fd);
  115. exit(1);
  116. }
  117.  
  118. printf("width: %dnheight: %dnmaxval: %dn", width, height, maxval);
  119.  
  120. // read image data
  121. image = malloc(width * height);
  122. if (fread(image, sizeof(unsigned char), width * height, fd) != width * height) {
  123. printf("could not read imagen");
  124. fclose(fd);
  125. exit(1);
  126. }
  127. fclose(fd);
  128.  
  129. // compute histogram
  130. histo = (int*)calloc(maxval+1, sizeof(int));
  131. for (i = 0; i < width * height; i++)
  132. histo[image[i]]++;
  133.  
  134. // print histogram
  135. for (i = 0; i <= maxval; i++)
  136. printf("Color number %d count %dn", i, histo[i]);
  137.  
  138. // release memory
  139. free(image);
  140. free(histo);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment