Python 如何使用opencv将透明背景的图像更改为白色背景
我正在尝试在opencv中定位轮廓,并且正在使用具有透明背景的图像。将图像加载到内存并显示图像后,透明背景已重新存储为围绕图像焦点的黑白矩形Python 如何使用opencv将透明背景的图像更改为白色背景,python,opencv,opencv-contour,Python,Opencv,Opencv Contour,我正在尝试在opencv中定位轮廓,并且正在使用具有透明背景的图像。将图像加载到内存并显示图像后,透明背景已重新存储为围绕图像焦点的黑白矩形 image = cv.imread('C:/Users/H/Desktop/overhead.png') cv.namedWindow('image', cv.WINDOW_NORMAL) cv.imshow('image', image) cv.waitKey(0) 是我当前使用的代码 图像周围没有黑色像素,而是有几个大的白色块(被检测为轮廓)。 我
image = cv.imread('C:/Users/H/Desktop/overhead.png')
cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', image)
cv.waitKey(0)
是我当前使用的代码
图像周围没有黑色像素,而是有几个大的白色块(被检测为轮廓)。
我找到了一个合适的解决办法
但是,现在未检测到右上角的~圆形。所有3个矩形都可以找到
我找到了一个合适的解决办法
但是,现在未检测到右上角的~圆形。所有3个矩形都可以找到
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5, 5), 0)
thresh = cv.threshold(blurred, 103, 255, cv.THRESH_BINARY)[1]
cnts = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the contour and center of the shape on the image
cv.drawContours(image, [c], -1, (0, 255, 0), 2)
cv.circle(image, (cX, cY), 7, (255, 255, 255), -1)
cv.putText(image, "center", (cX - 20, cY - 20),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# show the image
cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', image)
cv.waitKey(0)