设置 Python OpenCV warpPerspective 的背景
Set background of Python OpenCV warpPerspective
使用warpPerspective
缩小图片时,周围有黑色区域。它可能是这样的:
或
如何把黑边变成白边?
pts1 = np.float32([[minx,miny],[maxx,miny],[minx,maxy],[maxx,maxy]])
pts2 = np.float32([[minx + 20, miny + 20,
[maxx - 20, miny - 20],
[minx - 20, maxy + 20],
[maxx + 20, maxy + 20]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(dst, M, (width, height))
如何去除warpPerspective
后的黑边?
如果您查看在线 OpenCV 文档 (http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html) 中 warpPerspective
函数的文档,您会说有一个参数可以提供给该函数以指定常量边框颜色:
cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
其中
src – input image.
dst – output image that has the size dsize and the same type as src .
M – 3\times 3 transformation matrix.
dsize – size of the output image.
flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( \texttt{dst}\rightarrow\texttt{src} ).
borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
borderValue – value used in case of a constant border; by default, it equals 0.
所以是这样的:
cv2.warpPerspective(dist, M, (width, height), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, 255)
应该将边框更改为常亮的白色。
虽然文档中没有将其列为可能的 borderMode,但您也可以设置 borderMode=cv2.BORDER_TRANSPARENT 并且它不会创建任何边框。它将保持目标图像的像素设置不变。通过这种方式,您可以将边框设为白色或将边框设为您选择的图像。
例如带有白色边框的图片:
white_image = np.zeros(dsize, np.uint8)
white_image[:,:,:] = 255
cv2.warpPerspective(src, M, dsize, white_image, borderMode=cv2.BORDER_TRANSPARENT)
将为转换后的图像创建白色边框。除了边框,您还可以加载任何内容作为背景图像,只要它与目标大小相同即可。例如,如果我有一个背景全景图,我正在将图像扭曲到它上面,我可以使用全景图作为背景。
叠加了扭曲图像的全景图:
panorama = cv2.imread("my_panorama.jpg")
cv2.warpPerspective(src, M, panorama.shape, borderMode=cv2.BORDER_TRANSPARENT)
已接受的答案不再有效。尝试用白色填充曝光区域。
outImg = cv2.warpPerspective(img, tr, (imgWidth, imgHeight),
borderMode=cv2.BORDER_CONSTANT,
borderValue=(255, 255, 255))
使用warpPerspective
缩小图片时,周围有黑色区域。它可能是这样的:
或
如何把黑边变成白边?
pts1 = np.float32([[minx,miny],[maxx,miny],[minx,maxy],[maxx,maxy]])
pts2 = np.float32([[minx + 20, miny + 20,
[maxx - 20, miny - 20],
[minx - 20, maxy + 20],
[maxx + 20, maxy + 20]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(dst, M, (width, height))
如何去除warpPerspective
后的黑边?
如果您查看在线 OpenCV 文档 (http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html) 中 warpPerspective
函数的文档,您会说有一个参数可以提供给该函数以指定常量边框颜色:
cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
其中
src – input image.
dst – output image that has the size dsize and the same type as src .
M – 3\times 3 transformation matrix.
dsize – size of the output image.
flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( \texttt{dst}\rightarrow\texttt{src} ).
borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
borderValue – value used in case of a constant border; by default, it equals 0.
所以是这样的:
cv2.warpPerspective(dist, M, (width, height), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, 255)
应该将边框更改为常亮的白色。
虽然文档中没有将其列为可能的 borderMode,但您也可以设置 borderMode=cv2.BORDER_TRANSPARENT 并且它不会创建任何边框。它将保持目标图像的像素设置不变。通过这种方式,您可以将边框设为白色或将边框设为您选择的图像。
例如带有白色边框的图片:
white_image = np.zeros(dsize, np.uint8)
white_image[:,:,:] = 255
cv2.warpPerspective(src, M, dsize, white_image, borderMode=cv2.BORDER_TRANSPARENT)
将为转换后的图像创建白色边框。除了边框,您还可以加载任何内容作为背景图像,只要它与目标大小相同即可。例如,如果我有一个背景全景图,我正在将图像扭曲到它上面,我可以使用全景图作为背景。
叠加了扭曲图像的全景图:
panorama = cv2.imread("my_panorama.jpg")
cv2.warpPerspective(src, M, panorama.shape, borderMode=cv2.BORDER_TRANSPARENT)
已接受的答案不再有效。尝试用白色填充曝光区域。
outImg = cv2.warpPerspective(img, tr, (imgWidth, imgHeight),
borderMode=cv2.BORDER_CONSTANT,
borderValue=(255, 255, 255))