Region.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from image_features_extraction import MyException
  2. class Region(object):
  3. """
  4. Object refering to a single image region
  5. """
  6. def __init__(self, obj_region):
  7. if obj_region is None:
  8. raise MyException.MyException
  9. self.__obj_region = obj_region
  10. def prop_value(self, prop_name):
  11. """
  12. Measure the specified property name (e.g., 'area')
  13. :param prop_name: name of the property to measure (e.g, 'area')
  14. :type prop_name: string
  15. :returns: value of the property name
  16. :rtype: int,float,list
  17. :example:
  18. >>> import image_features_extraction as fe
  19. >>> imgs = fe.Images(folder_name)
  20. >>> img = imgs.item(1)
  21. >>> regs = img.Regions()
  22. >>> reg = regs.Region()
  23. >>> area = reg.prop_value('area')
  24. The following properties can be accessed as attributes or keys:
  25. **area** : int
  26. Number of pixels of region.
  27. **bbox** : tuple
  28. Bounding box ``(min_row, min_col, max_row, max_col)``.
  29. Pixels belonging to the bounding box are in the half-open interval
  30. ``[min_row; max_row)`` and ``[min_col; max_col)``.
  31. **bbox_area** : int
  32. Number of pixels of bounding box.
  33. **centroid** : array
  34. Centroid coordinate tuple ``(row, col)``.
  35. **convex_area** : int
  36. Number of pixels of convex hull image.
  37. **convex_image** : (H, J) ndarray
  38. Binary convex hull image which has the same size as bounding box.
  39. **coords** : (N, 2) ndarray
  40. Coordinate list ``(row, col)`` of the region.
  41. **eccentricity** : float
  42. Eccentricity of the ellipse that has the same second-moments as the
  43. region. The eccentricity is the ratio of the focal distance
  44. (distance between focal points) over the major axis length.
  45. The value is in the interval [0, 1).
  46. When it is 0, the ellipse becomes a circle.
  47. **equivalent_diameter** : float
  48. The diameter of a circle with the same area as the region.
  49. **euler_number** : int
  50. Euler characteristic of region. Computed as number of objects (= 1)
  51. subtracted by number of holes (8-connectivity).
  52. **extent** : float
  53. Ratio of pixels in the region to pixels in the total bounding box.
  54. Computed as ``area / (rows * cols)``
  55. **filled_area** : int
  56. Number of pixels of filled region.
  57. **filled_image** : (H, J) ndarray
  58. Binary region image with filled holes which has the same size as
  59. bounding box.
  60. **image** : (H, J) ndarray
  61. Sliced binary region image which has the same size as bounding box.
  62. **inertia_tensor** : (2, 2) ndarray
  63. Inertia tensor of the region for the rotation around its mass.
  64. **inertia_tensor_eigvals** : tuple
  65. The two eigen values of the inertia tensor in decreasing order.
  66. **intensity_image** : ndarray
  67. Image inside region bounding box.
  68. **label** : int
  69. The label in the labeled input image.
  70. **local_centroid** : array
  71. Centroid coordinate tuple ``(row, col)``, relative to region bounding
  72. box.
  73. **major_axis_length** : float
  74. The length of the major axis of the ellipse that has the same
  75. normalized second central moments as the region.
  76. **max_intensity** : float
  77. Value with the greatest intensity in the region.
  78. **mean_intensity** : float
  79. Value with the mean intensity in the region.
  80. **min_intensity** : float
  81. Value with the least intensity in the region.
  82. **minor_axis_length** : float
  83. The length of the minor axis of the ellipse that has the same
  84. normalized second central moments as the region.
  85. **moments** : (3, 3) ndarray
  86. Spatial moments up to 3rd order::
  87. m_ji = sum{ array(x, y) * x^j * y^i }
  88. where the sum is over the `x`, `y` coordinates of the region.
  89. **moments_central** : (3, 3) ndarray
  90. Central moments (translation invariant) up to 3rd order::
  91. mu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }
  92. where the sum is over the `x`, `y` coordinates of the region,
  93. and `x_c` and `y_c` are the coordinates of the region's centroid.
  94. **moments_hu** : tuple
  95. Hu moments (translation, scale and rotation invariant).
  96. **moments_normalized** : (3, 3) ndarray
  97. Normalized moments (translation and scale invariant) up to 3rd order::
  98. nu_ji = mu_ji / m_00^[(i+j)/2 + 1]
  99. where `m_00` is the zeroth spatial moment.
  100. **orientation** : float
  101. Angle between the X-axis and the major axis of the ellipse that has
  102. the same second-moments as the region. Ranging from `-pi/2` to
  103. `pi/2` in counter-clockwise direction.
  104. **perimeter** : float
  105. Perimeter of object which approximates the contour as a line
  106. through the centers of border pixels using a 4-connectivity.
  107. **solidity** : float
  108. Ratio of pixels in the region to pixels of the convex hull image.
  109. **weighted_centroid** : array
  110. Centroid coordinate tuple ``(row, col)`` weighted with intensity
  111. image.
  112. **weighted_local_centroid** : array
  113. Centroid coordinate tuple ``(row, col)``, relative to region bounding
  114. box, weighted with intensity image.
  115. **weighted_moments** : (3, 3) ndarray
  116. Spatial moments of intensity image up to 3rd order::
  117. wm_ji = sum{ array(x, y) * x^j * y^i }
  118. where the sum is over the `x`, `y` coordinates of the region.
  119. **weighted_moments_central** : (3, 3) ndarray
  120. Central moments (translation invariant) of intensity image up to
  121. 3rd order::
  122. wmu_ji = sum{ array(x, y) * (x - x_c)^j * (y - y_c)^i }
  123. where the sum is over the `x`, `y` coordinates of the region,
  124. and `x_c` and `y_c` are the coordinates of the region's weighted
  125. centroid.
  126. **weighted_moments_hu** : tuple
  127. Hu moments (translation, scale and rotation invariant) of intensity
  128. image.
  129. **weighted_moments_normalized** : (3, 3) ndarray
  130. Normalized moments (translation and scale invariant) of intensity
  131. image up to 3rd order::
  132. wnu_ji = wmu_ji / wm_00^[(i+j)/2 + 1]
  133. where ``wm_00`` is the zeroth spatial moment (intensity-weighted area).
  134. """
  135. try:
  136. return getattr(self.__obj_region, prop_name)
  137. except Exception as e:
  138. print(e.args)
  139. return None