Set Python OpenCV background warpPerspective
When used warpPerspective
to zoom out, there is a black area around it. It can be like:
or
How do I make black borders white?
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))
How do I remove black borders after warpPerspective
?
source to share
If you look at the documentation for the function warpPerspective
from the OpenCV online documentation ( http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html ) then there is a parameter you can give the function to specify a constant border color:
cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
Where
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.
So something like:
cv2.warpPerspective(dist, M, (width, height), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, 255)
Change the border to permanent white.
source to share
Until it is listed in the documentation as a possible borderMode, you can also set borderMode = cv2.BORDER_TRANSPARENT and it will not create any borders. It will keep the unchanged pixel settings of the target image. This way you can make the border white or the border with an image of your choice.
For example, for an image with a white border:
white_image = np.zeros(dsize, np.uint8)
white_image[:,:,:] = 255
cv2.warpPerspective(src, M, dsize, white_image, borderMode=cv2.BORDER_TRANSPARENT)
Creates a white border for the converted image. In addition to the border, you can also upload everything as a background image as long as it is the same size as the destination. For example, if I had a background panorama in which I distorted the image, I could use the panorama as the background.
Inverted panorama:
panorama = cv2.imread("my_panorama.jpg")
cv2.warpPerspective(src, M, panorama.shape, borderMode=cv2.BORDER_TRANSPARENT)
source to share