def extract_from_image(self, img, scale=1.0, margin_width=5, margin_height=5):
"""Extracts the contents of this box from a given image.
For that the image is "unrotated" by the appropriate angle, and the corresponding part is extracted from it.
Returns an image with dimensions height*scale x width*scale.
Note that the box coordinates are interpreted as "image coordinates" (i.e. x is row and y is column),
and box angle is considered to be relative to the vertical (i.e. np.pi/2 is "normal orientation")
:param img: a numpy ndarray suitable for image processing via skimage.
:param scale: the RotatedBox is scaled by this value before performing the extraction.
This is necessary when, for example, the location of a particular feature is determined using a smaller image,
yet then the corresponding area needs to be extracted from the original, larger image.
The scale parameter in this case should be width_of_larger_image/width_of_smaller_image.
:param margin_width: The margin that should be added to the width dimension of the box from each size.
This value is given wrt actual box dimensions (i.e. not scaled).
:param margin_height: The margin that should be added to the height dimension of the box from each side.
:return: a numpy ndarray, corresponding to the extracted region (aligned straight).
TODO: This could be made more efficient if we avoid rotating the full image and cut out the ROI from it beforehand.
"""
rotate_by = (np.pi/2 - self.angle)*180/np.pi
img_rotated = transform.rotate(img, angle=rotate_by, center=[self.center[1]*scale, self.center[0]*scale], resize=True)
# The resizeable transform will shift the resulting image somewhat wrt original coordinates.
# When we cut out the box we will compensate for this shift.
shift_c, shift_r = self._compensate_rotation_shift(img, scale)
r1 = max(int((self.center[0] - self.height/2 - margin_height)*scale - shift_r), 0)
r2 = int((self.center[0] + self.height/2 + margin_height)*scale - shift_r)
c1 = max(int((self.center[1] - self.width/2 - margin_width)*scale - shift_c), 0)
c2 = int((self.center[1] + self.width/2 + margin_width)*scale - shift_c)
return img_rotated[r1:r2, c1:c2]
评论列表
文章目录