test_invariance_utils.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #ifndef __OPENCV_TEST_INVARIANCE_UTILS_HPP__
  5. #define __OPENCV_TEST_INVARIANCE_UTILS_HPP__
  6. namespace opencv_test { namespace {
  7. Mat generateHomography(float angle)
  8. {
  9. // angle - rotation around Oz in degrees
  10. float angleRadian = static_cast<float>(angle * CV_PI / 180);
  11. Mat H = Mat::eye(3, 3, CV_32FC1);
  12. H.at<float>(0,0) = H.at<float>(1,1) = std::cos(angleRadian);
  13. H.at<float>(0,1) = -std::sin(angleRadian);
  14. H.at<float>(1,0) = std::sin(angleRadian);
  15. return H;
  16. }
  17. Mat rotateImage(const Mat& srcImage, const Mat& srcMask, float angle, Mat& dstImage, Mat& dstMask)
  18. {
  19. // angle - rotation around Oz in degrees
  20. float diag = std::sqrt(static_cast<float>(srcImage.cols * srcImage.cols + srcImage.rows * srcImage.rows));
  21. Mat LUShift = Mat::eye(3, 3, CV_32FC1); // left up
  22. LUShift.at<float>(0,2) = static_cast<float>(-srcImage.cols/2);
  23. LUShift.at<float>(1,2) = static_cast<float>(-srcImage.rows/2);
  24. Mat RDShift = Mat::eye(3, 3, CV_32FC1); // right down
  25. RDShift.at<float>(0,2) = diag/2;
  26. RDShift.at<float>(1,2) = diag/2;
  27. Size sz(cvRound(diag), cvRound(diag));
  28. Mat H = RDShift * generateHomography(angle) * LUShift;
  29. warpPerspective(srcImage, dstImage, H, sz);
  30. warpPerspective(srcMask, dstMask, H, sz);
  31. return H;
  32. }
  33. float calcCirclesIntersectArea(const Point2f& p0, float r0, const Point2f& p1, float r1)
  34. {
  35. float c = static_cast<float>(cv::norm(p0 - p1)), sqr_c = c * c;
  36. float sqr_r0 = r0 * r0;
  37. float sqr_r1 = r1 * r1;
  38. if(r0 + r1 <= c)
  39. return 0;
  40. float minR = std::min(r0, r1);
  41. float maxR = std::max(r0, r1);
  42. if(c + minR <= maxR)
  43. return static_cast<float>(CV_PI * minR * minR);
  44. float cos_halfA0 = (sqr_r0 + sqr_c - sqr_r1) / (2 * r0 * c);
  45. float cos_halfA1 = (sqr_r1 + sqr_c - sqr_r0) / (2 * r1 * c);
  46. float A0 = 2 * acos(cos_halfA0);
  47. float A1 = 2 * acos(cos_halfA1);
  48. return 0.5f * sqr_r0 * (A0 - sin(A0)) +
  49. 0.5f * sqr_r1 * (A1 - sin(A1));
  50. }
  51. float calcIntersectRatio(const Point2f& p0, float r0, const Point2f& p1, float r1)
  52. {
  53. float intersectArea = calcCirclesIntersectArea(p0, r0, p1, r1);
  54. float unionArea = static_cast<float>(CV_PI) * (r0 * r0 + r1 * r1) - intersectArea;
  55. return intersectArea / unionArea;
  56. }
  57. void scaleKeyPoints(const vector<KeyPoint>& src, vector<KeyPoint>& dst, float scale)
  58. {
  59. dst.resize(src.size());
  60. for (size_t i = 0; i < src.size(); i++) {
  61. dst[i] = src[i];
  62. dst[i].pt.x *= scale;
  63. dst[i].pt.y *= scale;
  64. dst[i].size *= scale;
  65. }
  66. }
  67. }} // namespace
  68. #endif // __OPENCV_TEST_INVARIANCE_UTILS_HPP__