将背景颜色更改为黑色 [英] Change the background color to black
本文介绍了将背景颜色更改为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将此背景更改为原始黑色.此背景不是纯黑色.它的值包含1、2或3.使用以下代码后,我得到的背景值非常接近黑色,但不是黑色.尽管背景看起来是黑色的
I want to change this background into the original black. This background is not pure black. Its values contain 1, 2 or 3. After using the following code I got the background value very near to black but not black. Although the background looks black
img = cv2.imread("images.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2. THRESH_BINARY)
img[thresh == 5] = 0
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
erosion = cv2.erode(img, kernel, iterations = 1)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow("image", erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
推荐答案
尝试替换以下内容:
ret, thresh = cv2.threshold(gray, 0, 255, cv2. THRESH_BINARY)
img[thresh == 5] = 0
与此:
# threshold to 10% of the maximum
threshold = 0.10 * np.max(img)
img[gray <= threshold] = 0
问题在于cv2.threshold()
不会为您计算阈值,而是应用一个阈值,例如,代码中的thresh
已经是阈值图像.
The issue is that cv2.threshold()
does not compute a threshold for you, but applies one and, for example, thresh
in your code is already the thresholded image.
(已编辑)
这篇关于将背景颜色更改为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文