# niboshi's profile - activity

2014-05-20 09:04:05 -0500 commented answer cv2.warpAffine() results in an image shifted by 0.5 pixel

OK, it's clear now. In cv2.warpAffine(), pixel values are considered located at grid cross-points (which is more mathematically consistent), not at the centers of grid squares (visually consistent). I was confused with cv2.resize() whose behaviour is more like the latter.

2014-05-17 02:59:56 -0500 asked a question cv2.warpAffine() results in an image shifted by 0.5 pixel

Warping an image with cv2.warpAffine() results in an image shifted by 0.5 pixel.

Is this behaviour expected? If so, what's the reasons behind?

## Code

Below is a simple example where an image is scaled with affine transform.

import cv2
import numpy as np

def warpScale(im, scale):
affine = np.array([
[scale, 0, 0],
[0, scale, 0],
[0, 0, 1],
], np.float32)
return cv2.warpAffine(im, affine[:2, :], (int(im.shape*scale), int(im.shape*scale)))

scale = 8

# Scale up
im1 = warpScale(im, scale)
cv2.imwrite("1.png", im1)     # 16x16

# Scale down
im2 = warpScale(im1, 1. / scale)
cv2.imwrite("2.png", im2)     # 2x2


Input 2x2 image: Output-1 (scaled up by a factor of 8): Output-2 (scaled down back to the original): ## Workaround

When I rewrite the warpScale() function as blow, it becomes as I expected. It's like doing the following operations in order:

• Shift the original image by (+0.5, +0.5)
• Apply the intended affine transform
• Shift the transformed image by (-0.5, -0.5)

:

def warpScale(im, scale):
affine = np.dot(
np.array([
[1, 0, -0.5],
[0, 1, -0.5],
[0, 0, 1],
], np.float32),
np.dot(
np.array([
[scale, 0, 0],
[0, scale, 0],
[0, 0, 1],
], np.float32),
np.array([
[1, 0, 0.5],
[0, 1, 0.5],
[0, 0, 1],
], np.float32)
)
)
return cv2.warpAffine(im, affine[:2, :], (int(im.shape*scale), int(im.shape*scale)))


Output-1 (scaled up by a factor of 8): Output-2 (scaled down back to the original): 