如何使用open将透明背景的图像更改为白色背景

2024-04-25 14:55:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在opencv中定位轮廓,并使用一个透明背景的图像。在将图像加载到内存并显示图像后,透明背景被重新存储为围绕图像焦点的黑白矩形。你知道吗

image = cv.imread('C:/Users/H/Desktop/overhead.png')

cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', image)
cv.waitKey(0)

是我当前使用的代码

图像周围没有黑色像素,而是有几个大的白色块(被检测为轮廓)。你知道吗


Tags: 内存定位图像imagepngopencvuserscv
1条回答
网友
1楼 · 发布于 2024-04-25 14:55:45

Convert White Pixels to Black in OpenCV python

我找到了一个合适的解决办法。你知道吗

但是现在右上角的~圆形没有被检测到。所有3个矩形都可以找到。tresh

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)

相关问题 更多 >

    热门问题