Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. //=============================================================================
  2. // NEW IN ASSIGNMENT 2
  3. //=============================================================================
  4.  
  5. //--------------------------------------------------------
  6. Image Image::clamp(float aMinValue, float aMaxValue) const
  7. //--------------------------------------------------------
  8. {
  9. // Copy the Image instance into a temp one
  10. Image temp = *this;
  11.  
  12. // Process every pixel
  13. for (std::vector<float>::iterator ite = temp.m_p_image.begin();
  14. ite != temp.m_p_image.end();
  15. ++ite)
  16. {
  17. // If needed clamp the pixel value between aMinValue and aMaxValue
  18. if (*ite < aMinValue) *ite = aMinValue;
  19. else if (*ite > aMaxValue) *ite = aMaxValue;
  20. }
  21. return (temp);
  22. }
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. std::vector<unsigned int> Image::getHistogram(unsigned int aNumberOfBins) const
  27. //-----------------------------------------------------------------------------
  28. {
  29. vector<unsigned int> p_histogram(aNumberOfBins, 0);
  30. int size = p_histogram.size();
  31.  
  32. int minVal = -0.83714;
  33. int startFigure = 6.23304 / aNumberOfBins;
  34. int values[] = { size };
  35.  
  36. for (int i = 0; i < aNumberOfBins; i++)
  37. {
  38. p_histogram[i] = minVal = +startFigure;
  39. }
  40. // Return the histogram
  41. return (p_histogram);
  42. }
  43.  
  44. //----------------------------------------------------------
  45. void Image::writeHistogram(const std::string& aFileName,
  46. unsigned int aNumberOfBins) const
  47. //----------------------------------------------------------
  48. {
  49. std::ofstream output_stream(aFileName);
  50.  
  51. if (!output_stream.is_open()) // if the file is not open throw error
  52. {
  53. std::string error_message = "Cannot open file (\"";
  54. error_message += aFileName;
  55. error_message += "\").";
  56. throw (error_message); // Throw an error
  57. }
  58. else {
  59. std::ostream_iterator<std::string> output_iterator(output_stream, "\n");
  60. std::copy(getHistogram(5).begin(), getHistogram(5).end(), output_iterator);
  61. }
  62. output_stream.close(); // Close the file
  63. }
  64.  
  65.  
  66. //---------------------------------------------
  67. float Image::getSSE(const Image& anImage) const
  68. //---------------------------------------------
  69. {
  70. float sse = 0.00;
  71.  
  72. for (float i = 0; i < m_p_image.size; i++)
  73. {
  74. float sse = +pow(std::abs(m_p_image[i] - anImage.m_p_image[i]), 2);
  75. }
  76. return (sse);
  77. }
  78.  
  79.  
  80. //---------------------------------------------
  81. float Image::getMSE(const Image& anImage) const
  82. //---------------------------------------------
  83. {
  84. float mse = getSSE(anImage) / m_p_image.size();
  85. return (mse);
  86. }
  87.  
  88.  
  89. //----------------------------------------------
  90. float Image::getRMSE(const Image& anImage) const
  91. //----------------------------------------------
  92. {
  93. float rmse = sqrt(getMSE(anImage));
  94. return (rmse);
  95. }
  96.  
  97.  
  98. //----------------------------------------------
  99. float Image::getZNCC(const Image& anImage) const
  100. //----------------------------------------------
  101. {
  102. {
  103. float num, x = 0.0;
  104. float std_dev1 = getVariance();
  105. float std_dev2 = anImage.getVariance();
  106. float avg1 = getAverage();
  107. float avg2 = anImage.getAverage();
  108.  
  109. for (x = 0; x < m_p_image.size(); x++)
  110. {
  111. num += ((m_p_image[x] - avg1) * (anImage[x] - avg2)) / (std_dev1 * std_dev2);
  112. }
  113. return (num / m_p_image.size());
  114. }
  115. }
  116.  
  117.  
  118. //------------------------------------------------
  119. Image Image::convolve(const float* apKernel) const
  120. //------------------------------------------------
  121. {
  122. {
  123. // Create a new temp image
  124. Image temp(getWidth(), getHeight(), 0.0);
  125.  
  126. float i, j, m, n, mm, nn;
  127. int kCenterX, kCenterY; // center index of kernel
  128. float sum; // temp accumulation buffer
  129.  
  130. // find center position of kernel (half of kernel size)
  131.  
  132.  
  133. for (i = 0; i < getWidth(); ++i) // rows
  134. {
  135. for (j = 0; j < getHeight(); ++j) // columns
  136. {
  137. sum = 0; // init to 0 before sum
  138. for (m = 0; m < *apKernel; ++m) // kernelY rows !working
  139. {
  140. mm = *apKernel - 1 - m; // row index of flipped kernel
  141.  
  142. for (n = 0; n < *apKernel; ++n) // kernelX columns !working
  143. {
  144. nn = *apKernel - 1 - n; // column index of flipped kernel
  145. }
  146. }
  147. }
  148. }
  149.  
  150. // Return the new image
  151. return (temp);
  152. }
  153. }
  154.  
  155. //-----------------------------
  156. Image Image::meanFilter() const
  157. //-----------------------------
  158. {
  159. // Create a new temp image
  160. Image temp(getWidth(), getHeight(), 0.0);
  161.  
  162. float kernelMean[] = { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0,
  163. 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0,
  164. 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 };
  165.  
  166. // Return the new image from matrix
  167. return (temp = convolve(kernelMean));
  168. }
  169.  
  170.  
  171. //---------------------------------
  172. Image Image::gaussianFilter() const
  173. //---------------------------------
  174. {
  175. // Create a new temp image
  176. Image temp(getWidth(), getHeight(), 0.0);
  177.  
  178. float kernelGaussian[] = {1, 2, 1,
  179. 2, 4, 2,
  180. 1, 2, 1 };
  181.  
  182. // Return the new image
  183. return (temp = convolve(kernelGaussian));
  184. }
  185.  
  186. //----------------------------------
  187. Image Image::laplacianFilter() const
  188. //----------------------------------
  189. {
  190. // Create a new temp image
  191. Image temp(getWidth(), getHeight(), 0.0);
  192.  
  193.  
  194. float kernelLaplacian[] = { 0, 1, 0,
  195. 1, -4, 1,
  196. 0, 1, 0 };
  197.  
  198. // Return the new image
  199. return (temp = convolve(kernelLaplacian));
  200. }
  201.  
  202.  
  203. //-------------------------------
  204. Image Image::sobelXFilter() const
  205. //-------------------------------
  206. {
  207. // Create a new temp image
  208. Image temp(getWidth(), getHeight(), 0.0);
  209.  
  210.  
  211. float kernelSobelX[] = { -1, 0, 1,
  212. -2, 0, 2,
  213. -1, 0, 1 };
  214.  
  215. // Return the new image
  216. return (temp = convolve(kernelSobelX));
  217. }
  218.  
  219.  
  220. //-------------------------------
  221. Image Image::sobelYFilter() const
  222. //-------------------------------
  223. {
  224. // Create a new temp image
  225. Image temp(getWidth(), getHeight(), 0.0);
  226.  
  227.  
  228. float kernelSobelY[] = { 1, 2, 1,
  229. 0, 0, 0,
  230. -1, -2, -1 };
  231.  
  232. // Return the new image
  233. return (temp = convolve(kernelSobelY));
  234. }
  235.  
  236.  
  237. //----------------------
  238. Image Image::abs() const
  239. //----------------------
  240. {
  241. // Create a new temp image
  242. Image temp(getWidth(), getHeight(), 0.0);
  243.  
  244.  
  245. //=========================
  246. // Write your own code here
  247. //=========================
  248.  
  249.  
  250. // Return the new image
  251. return (temp);
  252. }
  253.  
  254.  
  255. //------------------------------------
  256. Image Image::gradientMagnitude() const
  257. //------------------------------------
  258. {
  259. // Create a new temp image
  260. Image temp(getWidth(), getHeight(), 0.0);
  261.  
  262.  
  263. //=========================
  264. // Write your own code here
  265. //=========================
  266.  
  267.  
  268. // Return the new image
  269. return (temp);
  270. }
  271.  
  272.  
  273. //----------------------------------------
  274. Image Image::sharpening(float alpha) const
  275. //----------------------------------------
  276. {
  277. // Create a new temp image
  278. Image temp(getWidth(), getHeight(), 0.0);
  279.  
  280. float kernelSharpening[] = { 0, -1, 0,
  281. -1, 5, -1,
  282. 0, -1, 0 };
  283.  
  284. // Return the new image
  285. return (temp = convolve(kernelSharpening));
  286. }
  287.  
  288. //-----------------------------------------------
  289. Image Image::thresholding(float aThreshold) const
  290. //-----------------------------------------------
  291. {
  292. // Create a new temp image
  293. Image temp(getWidth(), getHeight(), 0.0);
  294.  
  295.  
  296. for (int i = 0; i < temp.getWidth; i++)
  297. for (int j = 0; j < temp.getHeight; j++)
  298. {
  299. {
  300. if (temp.getPixel(i, j) >= aThreshold)
  301. {
  302. temp.getPixel(i, j) == 0;
  303. }
  304. }
  305.  
  306. // Return the new image
  307. return (temp);
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement