如何使cv2仿射变换和PIL仿射变换具有相同的输出?

时间:2019-11-28 10:05:13

标签: opencv python-imaging-library affinetransform

我用以下代码测试了cv2和PIL的仿射变换,发现结果不一样,如何使它们的行为相同?

def shear_x_func(img, factor, fill=0):
    H, W = img.shape[0], img.shape[1]
    M = np.float32([[1, -factor, 0], [0, 1, 0]])
    out = cv2.warpAffine(img, M, (W, H), borderValue=fill, flags=cv2.INTER_CUBIC).astype(np.uint8)
    return out
pth = './pic.jpg'
impil = Image.open(pth)
imcv = cv2.imread(pth)
out_cv = shear_x_func(imcv, 0.2).astype(np.float32)[:, :, ::-1]
out_pil = np.array(impil.transform(impil.size, Image.AFFINE, (1, 0.2, 0, 0, 1, 0), Image.BICUBIC, fillcolor=0)).astype(np.float32)
print(np.max(np.abs(out_pil - out_cv)))
print(np.min(np.abs(out_pil - out_cv)))

它表明错误可能高达255。我错过了让他们具有相同行为的原因吗?

0 个答案:

没有答案