OpenCV warpAffine and inverse warpAffine inconsistence

扶醉桌前 提交于 2020-08-06 02:03:51

问题


I'm using the OpenCV warpAffine function to do some image processing. The weird thing is that I found after applying a warpAffine and then an inverse warpAffine. The processed image is inconsistent with the original image, where there is one-pixel border padding at the bottom.

img_path = '140028_199844.jpg'
img = cv2.imread(img_path,cv2.IMREAD_COLOR)
plt.imshow(img[:,:,::-1])
h,w,_=img.shape # h=220 w=173

src = np.array([[ 86., 109.5], [ 86. , 0. ], [-23.5, 0. ]])
dst = np.array([[192., 192.], [192. , 0.], [  0. , 0.]])
trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
inv_trans = cv2.getAffineTransform(np.float32(dst), np.float32(src))
input = cv2.warpAffine(
    img,
    trans,
    (384, 384),
    flags=cv2.INTER_LINEAR,
    borderMode=cv2.BORDER_CONSTANT,
    borderValue=(0, 0, 0))
plt.imshow(input[:,:,::-1])

output = cv2.warpAffine(
        input,
        inv_trans,
        (w, h),
        flags=cv2.INTER_LINEAR,
        borderMode=cv2.BORDER_CONSTANT,
        borderValue=(0,0,0))
plt.imshow(output[:,:,::-1])

So what is the possible reseason for such problem?


回答1:


It is probably a numerical issue, since the warped coordinates get mapped back to integer indices (i.e. in range w and h).

You will likely see a worse effect if you do this multiple times, i.e. warp, invert, warp, invert, etc.




回答2:


The Scipy ndimage.rotate(img, angle) helped me when rotating images 180 degrees. It seems slower but for a few images it is not much time.



来源:https://stackoverflow.com/questions/56353524/opencv-warpaffine-and-inverse-warpaffine-inconsistence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!