Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.0 KiB

3 years ago
  1. '''The function below is ported from the OpenCV project's
  2. contourMoments function in opencv/modules/imgproc/src/moments.cpp,
  3. licensed as follows:
  4. ----------------------------------------------------------------------
  5. By downloading, copying, installing or using the software you agree to
  6. this license. If you do not agree to this license, do not download,
  7. install, copy or use the software.
  8. License Agreement
  9. For Open Source Computer Vision Library
  10. (3-clause BSD License)
  11. Copyright (C) 2000-2016, Intel Corporation, all rights reserved.
  12. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  13. Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
  14. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  15. Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
  16. Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
  17. Third party copyrights are property of their respective owners.
  18. Redistribution and use in source and binary forms, with or without
  19. modification, are permitted provided that the following conditions are
  20. met:
  21. * Redistributions of source code must retain the above copyright
  22. notice, this list of conditions and the following disclaimer.
  23. * Redistributions in binary form must reproduce the above copyright
  24. notice, this list of conditions and the following disclaimer in
  25. the documentation and/or other materials provided with the
  26. distribution.
  27. * Neither the names of the copyright holders nor the names of the
  28. contributors may be used to endorse or promote products derived
  29. from this software without specific prior written permission.
  30. This software is provided by the copyright holders and contributors
  31. "as is" and any express or implied warranties, including, but not
  32. limited to, the implied warranties of merchantability and fitness for
  33. a particular purpose are disclaimed. In no event shall copyright
  34. holders or contributors be liable for any direct, indirect,
  35. incidental, special, exemplary, or consequential damages (including,
  36. but not limited to, procurement of substitute goods or services; loss
  37. of use, data, or profits; or business interruption) however caused and
  38. on any theory of liability, whether in contract, strict liability, or
  39. tort (including negligence or otherwise) arising in any way out of the
  40. use of this software, even if advised of the possibility of such
  41. damage.
  42. '''
  43. def moments_from_contour(xypoints):
  44. '''Create shape moments from points sampled from the outline of an
  45. ellipse (note this is numerically inaccurate even for arrays of 1000s
  46. of points). Included in this project primarily for testing purposes.
  47. '''
  48. assert len(xypoints.shape) == 3
  49. assert xypoints.shape[1:] == (1, 2)
  50. xypoints = xypoints.reshape((-1, 2))
  51. a00 = 0
  52. a10 = 0
  53. a01 = 0
  54. a20 = 0
  55. a11 = 0
  56. a02 = 0
  57. xi_1, yi_1 = xypoints[-1]
  58. for xy in xypoints:
  59. xi, yi = xy
  60. xi2 = xi * xi
  61. yi2 = yi * yi
  62. dxy = xi_1 * yi - xi * yi_1
  63. xii_1 = xi_1 + xi
  64. yii_1 = yi_1 + yi
  65. a00 += dxy
  66. a10 += dxy * xii_1
  67. a01 += dxy * yii_1
  68. a20 += dxy * (xi_1 * xii_1 + xi2)
  69. a11 += dxy * (xi_1 * (yii_1 + yi_1) + xi * (yii_1 + yi))
  70. a02 += dxy * (yi_1 * yii_1 + yi2)
  71. xi_1 = xi
  72. yi_1 = yi
  73. if a00 > 0:
  74. db1_2 = 0.5
  75. db1_6 = 0.16666666666666666666666666666667
  76. db1_12 = 0.083333333333333333333333333333333
  77. db1_24 = 0.041666666666666666666666666666667
  78. else:
  79. db1_2 = -0.5
  80. db1_6 = -0.16666666666666666666666666666667
  81. db1_12 = -0.083333333333333333333333333333333
  82. db1_24 = -0.041666666666666666666666666666667
  83. m00 = a00 * db1_2
  84. m10 = a10 * db1_6
  85. m01 = a01 * db1_6
  86. m20 = a20 * db1_12
  87. m11 = a11 * db1_24
  88. m02 = a02 * db1_12
  89. inv_m00 = 1. / m00
  90. cx = m10 * inv_m00
  91. cy = m01 * inv_m00
  92. mu20 = m20 - m10 * cx
  93. mu11 = m11 - m10 * cy
  94. mu02 = m02 - m01 * cy
  95. return m00, m10, m01, mu20, mu11, mu02