def _compensate_rotation_shift(self, img, scale):
"""This is an auxiliary method used by extract_from_image.
It is needed due to particular specifics of the skimage.transform.rotate implementation.
Namely, when you use rotate(... , resize=True), the rotated image is rotated and shifted by certain amount.
Thus when we need to cut out the box from the image, we need to account for this shift.
We do this by repeating the computation from skimage.transform.rotate here.
TODO: This makes the code uncomfortably coupled to SKImage (e.g. this logic is appropriate for skimage 0.12.1, but not for 0.11,
and no one knows what happens in later versions). A solution would be to use skimage.transform.warp with custom settings, but we can think of it later.
"""
ctr = np.asarray([self.center[1]*scale, self.center[0]*scale])
tform1 = transform.SimilarityTransform(translation=ctr)
tform2 = transform.SimilarityTransform(rotation=np.pi/2 - self.angle)
tform3 = transform.SimilarityTransform(translation=-ctr)
tform = tform3 + tform2 + tform1
rows, cols = img.shape[0], img.shape[1]
corners = np.array([
[0, 0],
[0, rows - 1],
[cols - 1, rows - 1],
[cols - 1, 0]
])
corners = tform.inverse(corners)
minc = corners[:, 0].min()
minr = corners[:, 1].min()
maxc = corners[:, 0].max()
maxr = corners[:, 1].max()
# SKImage 0.11 version
out_rows = maxr - minr + 1
out_cols = maxc - minc + 1
# fit output image in new shape
return ((cols - out_cols) / 2., (rows - out_rows) / 2.)
评论列表
文章目录