image.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef _IMAGE_HPP_
  2. #define _IMAGE_HPP_
  3. #include <cassert>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. typedef unsigned char pixel_t;
  10. typedef std::vector<pixel_t> image_t;
  11. // write image
  12. std::string writePgm(const std::string &filename,
  13. int width,
  14. int height,
  15. const image_t & data)
  16. {
  17. std::ofstream os(filename);
  18. if (not os)
  19. return "writePgm: failed to open output file";
  20. // write magic number
  21. os << "P2" << std::endl;
  22. // write size
  23. os << width << ' ' << height << std::endl;
  24. // write max value
  25. os << "255" << std::endl;
  26. // write data
  27. for (unsigned char pixel : data)
  28. os << (unsigned)pixel << '\n';
  29. return "";
  30. }
  31. // read image
  32. std::string readPgm(const std::string &filename,
  33. int & width,
  34. int & height,
  35. image_t & data)
  36. {
  37. std::ifstream is(filename);
  38. if (not is)
  39. return "readPgm: failed to open input file";
  40. std::string tmp;
  41. // read magic number
  42. std::getline(is, tmp);
  43. if (tmp != "P2")
  44. return "readPnm: not a ASCII PGM file";
  45. // read comments
  46. std::getline(is, tmp);
  47. while (tmp[0] == '#')
  48. std::getline(is, tmp);
  49. // read size
  50. std::stringstream ss(tmp);
  51. ss >> width >> height;
  52. // read max value
  53. std::getline(is, tmp);
  54. // read data
  55. data = image_t(width*height);
  56. for (unsigned k=0; k<data.size(); k++)
  57. {
  58. int pixel;
  59. is >> pixel;
  60. data[k] = pixel;
  61. }
  62. return "";
  63. }
  64. // Returns a blurred subimage of data1 (using the specified radius for the convolution kernel).
  65. // The size of data1 is width*height.
  66. // The blurred region is, in data1, (x0, y0, x1, y1).
  67. // Therefore the size of the subimage is (x1-x0)*(y1-y0)
  68. image_t blur(const image_t & data1, int width, int height, int radius, int x0, int y0, int x1, int y1)
  69. {
  70. int width2 = x1-x0;
  71. int height2 = y1-y0;
  72. assert(data1.size() >= unsigned(width*height));
  73. assert(width2 > 0);
  74. assert(height2 > 0);
  75. image_t data2(width2*height2);
  76. for (int x=x0; x<x1; x++)
  77. {
  78. for (int y=y0; y<y1; y++)
  79. {
  80. unsigned s = 0;
  81. unsigned n = 0;
  82. for (int lx=-radius; lx<radius; lx++)
  83. {
  84. for (int ly=-radius; ly<radius; ly++)
  85. {
  86. int xx = x+lx;
  87. int yy = y+ly;
  88. if (xx >= 0 and xx < width and yy >= 0 and yy < height)
  89. {
  90. s += data1[(y+ly)*width + (x+lx)];
  91. n++;
  92. }
  93. }
  94. }
  95. int pixel = n>0 ? s/n : data1[y*width + x];
  96. if (x==x0 or y==y0 or x==x1-1 or y==y1-1) pixel = 0; // draw border
  97. data2[(y-y0)*width2 + (x-x0)] = pixel;
  98. }
  99. }
  100. return data2;
  101. }
  102. image_t computeLaplacian(const image_t & data1, int width, int height, double scaling)
  103. {
  104. assert(data1.size() >= unsigned(width*height));
  105. // return the pixel value of (x,y) in [0,1]
  106. auto ind = [&data1,width](int x, int y) {return double(data1[y*width + x])/255.0;};
  107. // compute laplacian image
  108. image_t data2(width*height);
  109. for (int x=1; x<width-1; x++)
  110. {
  111. for (int y=1; y<height-1; y++)
  112. {
  113. // compute laplacian value in [-4,4]
  114. double v = -4.0*ind(x,y) + ind(x,y-1) + ind(x,y+1) + ind(x-1,y) + ind(x+1,y);
  115. // scale the value in [0,255]
  116. // and write this value in output image
  117. int i255 = 255.0 * (scaling*v+4.0)/8.0;
  118. data2[y*width + x] = std::min(255, std::max(0, i255));
  119. }
  120. }
  121. return data2;
  122. }
  123. #endif