使用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)

应该将边框更改为恒定的白色。

10-07 18:24