cv2.warpPerspective 产生黑色虚线边缘

May*_*ngh 4 python opencv computer-vision

我正在尝试使用 warpPerspective 变换在围板上实现皮卡丘图像。输出没有平滑的边缘,而是有点。

import cv2
import numpy as np

image = cv2.imread("base_img.jpg")


h_base, w_base = image.shape[0], image.shape[1]

white_subject =  np.ones((480,640,3),dtype="uint8")*255
h_white, w_white = white_subject.shape[:2]

subject = cv2.imread('subject.jpg')


h_sub, w_sub = subject.shape[:2]

pts2 = np.float32([[109,186],[455,67],[480,248],[90,349]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])

transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
mask = cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)) 
image[mask==255] = 0

pts3 = np.float32([[0, 0], [w_sub, 0], [w_sub, h_sub], [0, h_sub]])
transformation_matrix = cv2.getPerspectiveTransform(pts3, pts2)
warped_image = cv2.warpPerspective(subject, transformation_matrix, (w_base, h_base)) 
Run Code Online (Sandbox Code Playgroud)

围板图片

在此输入图像描述

皮卡丘图片

在此输入图像描述

输出图像

在此输入图像描述

图案图像

在此输入图像描述

输出图像 在此输入图像描述

请帮助我获得边缘没有虚点的输出。

fmw*_*w42 7

这是在 Python/OpenCV 中进行抗锯齿合成的一种方法。请注意,我在 warpPerspective 的 borderVal 常量中使用叠加图像的背景颜色来设置背景颜色,因为它是常量。在进行合成之前,我还会模糊蒙版。

背景图像:

在此输入图像描述

叠加图像:

在此输入图像描述


import cv2
import numpy as np
import skimage.exposure

image = cv2.imread("base_img.jpg")
h_base, w_base = image.shape[0], image.shape[1]

white_subject =  np.ones((480,640,3),dtype="uint8")*255
h_white, w_white = white_subject.shape[:2]

subject = cv2.imread('subject.jpg')
h_sub, w_sub = subject.shape[:2]

# get background color from first pixel at (0,0) and its BGR components
yellow = subject[0:1, 0:1][0][0]
blue = yellow[0]
green = yellow[1]
red = yellow[2]
print(yellow)
print(blue, green, red)

pts2 = np.float32([[109,186],[455,67],[480,248],[90,349]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])

transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
mask = cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)) 

pts3 = np.float32([[0, 0], [w_sub, 0], [w_sub, h_sub], [0, h_sub]])
transformation_matrix = cv2.getPerspectiveTransform(pts3, pts2)
# do warping with borderVal = background color
warped_image = cv2.warpPerspective(subject, transformation_matrix, (w_base, h_base), borderMode = cv2.BORDER_CONSTANT, borderValue=(int(blue),int(green),int(red))) 

# anti-alias mask
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(mask, in_range=(0,128), out_range=(0,255))

# convert mask to float in range 0 to 1
mask = mask.astype(np.float64)/255

# composite warped image over base and convert back to uint8
result =  (warped_image * mask + image * (1 - mask))
result = result.clip(0,255).astype(np.uint8)

# save results
cv2.imwrite('warped_mask.png',(255*mask).clip(0,255).astype(np.uint8))
cv2.imwrite('warped_image.png',warped_image)
cv2.imwrite('warped_image_over_background.png',result)

cv2.imshow("mask", mask)
cv2.imshow("warped_image", warped_image)
cv2.imshow("result", result)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

抗锯齿扭曲蒙版:

在此输入图像描述

扭曲的图像:

在此输入图像描述

所得复合材料:

在此输入图像描述