interpolation.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef __OPENCV_TEST_INTERPOLATION_HPP__
  43. #define __OPENCV_TEST_INTERPOLATION_HPP__
  44. #include "opencv2/core.hpp"
  45. #include "opencv2/imgproc.hpp"
  46. template <typename T> T readVal(const cv::Mat& src, int y, int x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
  47. {
  48. if (border_type == cv::BORDER_CONSTANT)
  49. return (y >= 0 && y < src.rows && x >= 0 && x < src.cols) ? src.at<T>(y, x * src.channels() + c) : cv::saturate_cast<T>(borderVal.val[c]);
  50. return src.at<T>(cv::borderInterpolate(y, src.rows, border_type), cv::borderInterpolate(x, src.cols, border_type) * src.channels() + c);
  51. }
  52. template <typename T> struct NearestInterpolator
  53. {
  54. static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
  55. {
  56. return readVal<T>(src, int(y), int(x), c, border_type, borderVal);
  57. }
  58. };
  59. template <typename T> struct LinearInterpolator
  60. {
  61. static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
  62. {
  63. int x1 = cvFloor(x);
  64. int y1 = cvFloor(y);
  65. int x2 = x1 + 1;
  66. int y2 = y1 + 1;
  67. float res = 0;
  68. res += readVal<T>(src, y1, x1, c, border_type, borderVal) * ((x2 - x) * (y2 - y));
  69. res += readVal<T>(src, y1, x2, c, border_type, borderVal) * ((x - x1) * (y2 - y));
  70. res += readVal<T>(src, y2, x1, c, border_type, borderVal) * ((x2 - x) * (y - y1));
  71. res += readVal<T>(src, y2, x2, c, border_type, borderVal) * ((x - x1) * (y - y1));
  72. return cv::saturate_cast<T>(res);
  73. }
  74. };
  75. template <typename T> struct CubicInterpolator
  76. {
  77. static float bicubicCoeff(float x_)
  78. {
  79. float x = fabsf(x_);
  80. if (x <= 1.0f)
  81. {
  82. return x * x * (1.5f * x - 2.5f) + 1.0f;
  83. }
  84. else if (x < 2.0f)
  85. {
  86. return x * (x * (-0.5f * x + 2.5f) - 4.0f) + 2.0f;
  87. }
  88. else
  89. {
  90. return 0.0f;
  91. }
  92. }
  93. static T getValue(const cv::Mat& src, float y, float x, int c, int border_type, cv::Scalar borderVal = cv::Scalar())
  94. {
  95. const float xmin = ceilf(x - 2.0f);
  96. const float xmax = floorf(x + 2.0f);
  97. const float ymin = ceilf(y - 2.0f);
  98. const float ymax = floorf(y + 2.0f);
  99. float sum = 0.0f;
  100. float wsum = 0.0f;
  101. for (float cy = ymin; cy <= ymax; cy += 1.0f)
  102. {
  103. for (float cx = xmin; cx <= xmax; cx += 1.0f)
  104. {
  105. const float w = bicubicCoeff(x - cx) * bicubicCoeff(y - cy);
  106. sum += w * readVal<T>(src, (int) floorf(cy), (int) floorf(cx), c, border_type, borderVal);
  107. wsum += w;
  108. }
  109. }
  110. float res = (!wsum)? 0 : sum / wsum;
  111. return cv::saturate_cast<T>(res);
  112. }
  113. };
  114. #endif // __OPENCV_TEST_INTERPOLATION_HPP__