虚线是算术溢出的结果。
放大后,我们可以看到所有的值都是正数,但有些不是0也不是255:
在NumPy中求和(例如):uint8(255) + uint8(56)时,由于溢出,结果为55。
BGR = (55, 55, 55) 的像素值被视为深灰色。
在白色掩码示例中,我们希望在将正值添加到255 时得到255。
为避免溢出,您可以使用cv.add 而不是+=。
cv.add 具有溢出保护(它将结果剪辑为 255)。
将warped_mask += cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)) 替换为:
warped_mask = cv2.add(warped_mask, cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)))
使用时的结果:cv2.add:
使用该模式时,使用cv2.max而不是cv2.add时效果更好。
将warped_mask += cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)) 替换为:
warped_mask = cv2.max(warped_mask, cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)))
结果使用模式和cv2.max:
完整代码:
import cv2
import numpy as np
warped_mask = np.zeros((480,640,3),dtype="uint8")
h_base, w_base = 480,640
#white_subject = np.ones((480,640,3),dtype="uint8")*255
white_subject = cv2.resize(cv2.imread('pattern.jpg'), (640, 480))
h_white, w_white = white_subject.shape[:2]
pts2 = np.float32([[20, 20], [100, 20], [150, 300], [20, 250]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
#warped_mask += cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base))
warped_mask = cv2.max(warped_mask, cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)))
pts4 = np.float32([[100, 20],[300,20], [300, 300], [150, 300]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts4)
#warped_mask += cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base))
warped_mask = cv2.max(warped_mask, cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base)))
cv2.imshow("canvas", warped_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
为了更好的效果,我们可能会添加填充:
import cv2
import numpy as np
warped_mask = np.zeros((480,640,3),dtype="uint8")
h_base, w_base = 480, 640
white_subject = np.ones((480,640,3),dtype="uint8")*255
white_subject = cv2.imread('pattern.jpg')
h_white, w_white = white_subject.shape[:2]
padded_warped_mask = np.pad(warped_mask, ((1, 1), (1, 1), (0, 0)), mode='edge')
padded_white_subject = np.pad(white_subject, ((1, 1), (1, 1), (0, 0)), mode='edge')
pts2 = np.float32([[20, 20], [100, 20], [150, 300], [20, 250]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])
# Transform (0,0) coordinate to (1,1)
top2one = np.float64([[1, 0, -1],
[0, 1, -1],
[0, 0, 1]])
# Transform (1,1) coordinate to (0,0)
one2top = np.float64([[1, 0, 1],
[0, 1, 1],
[0, 0, 1]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
# Compensate the transformation for applying the padded two rows and columns
#transformation_matrix_white = one2top @ transformation_matrix_white @ top2one
transformation_matrix_white = top2one @ transformation_matrix_white @ one2top
#warped_mask = cv2.add(warped_mask, cv2.warpPerspective(padded_white_subject, transformation_matrix_white, (w_base, h_base)))
warped_mask = cv2.max(warped_mask, cv2.warpPerspective(padded_white_subject, transformation_matrix_white, (w_base, h_base)))
pts4 = np.float32([[100, 20],[300,20], [300, 300], [150, 300]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts4)
# Compensate the transformation for applying the padded two rows and columns
#transformation_matrix_white = one2top @ transformation_matrix_white @ top2one
transformation_matrix_white = top2one @ transformation_matrix_white @ one2top
#warped_mask = cv2.add(warped_mask, cv2.warpPerspective(padded_white_subject, transformation_matrix_white, (w_base, h_base)))
warped_mask = cv2.max(warped_mask, cv2.warpPerspective(padded_white_subject, transformation_matrix_white, (w_base, h_base)))
cv2.imshow("canvas", warped_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: