image.hpp 4.5 KB

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