【问题标题】:cv2.warpPerspective produces Black dotted edgescv2.warpPerspective 产生黑色虚线边缘
【发布时间】:2022-01-17 23:42:49
【问题描述】:

我正在尝试使用 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)) 

囤积板图片

皮卡丘图片

输出图像

图案图像

输出图像

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

【问题讨论】:

  • 我不明白你如何将扭曲的主题与背景结合起来。你只是带着面具复制吗?
  • 我正在使用 transformation_matrix_white 创建蒙版图像。在面具图像的帮助下,我将皮卡丘图像叠加在了围板上。
  • 您需要通过模糊边缘来消除遮罩的锯齿。例如,请参阅我如何在 stackoverflow.com/questions/63001988/…stackoverflow.com/questions/64208431/… 使用 Gaussianblur 和 skimage.exposure.rescale_intensity()
  • 这里我的意思是平滑边缘就像输出应该有像原始图像边缘一样的边缘。
  • 如果你没有一个常量颜色,那么在warpPerspective中为borderConstant使用cv.BORDER_REFLECT。这可能有助于缓解黑色虚线轮廓。

标签: python opencv computer-vision


【解决方案1】:

这是在 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)

抗锯齿扭曲蒙版:

扭曲的图像:

合成结果:

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多