在 OpenCV python 中将白色像素转换为黑色 [英] Convert White Pixels to Black in OpenCV python
问题描述
我正在尝试使用 python OpenCV 将输入图像的白色背景转换为黑色.但所有白色像素并未完全转换为黑色.我附上了输入和输出图像.
输入图像:
输出图像:
我使用了以下代码进行转换:
img[np.where((img==[255,255,255]).all(axis=2))] = [0,0,0];
我该怎么办?
我知道这已经得到了回答.我有一个编码的 python 解决方案给你.
首先我发现了
另一个测试图像:
编辑这是一种更好、更短的方法.在@ZdaR 评论循环遍历图像矩阵后,我查看了它.
[更新代码]
img = cv2.imread("图片/test.pnt")灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)img[thresh == 255] = 0内核 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))侵蚀= cv2.erode(img,内核,迭代= 1)cv2.namedWindow('图像', cv2.WINDOW_NORMAL)cv2.imshow(图像",侵蚀)cv2.waitKey(0)cv2.destroyAllWindows()
[旧代码]
img = cv2.imread("Images/test.png")灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)white_px = np.asarray([255, 255, 255])black_px = np.asarray([0, 0, 0])(row, col) = thresh.shapeimg_array = np.array(img)对于范围(行)中的 r:对于范围(col)中的c:像素 = 阈值 [r][c]如果全部(px == white_px):img_array[r][c] = black_px内核 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))侵蚀= cv2.erode(img_array,内核,迭代= 1)cv2.namedWindow('图像', cv2.WINDOW_NORMAL)cv2.imshow(图像",侵蚀)cv2.waitKey(0)cv2.destroyAllWindows()
使用的其他来源:OpenCV 形态变换
I am trying to convert the white background of the input image into black using python OpenCV.But all the white pixels are not completely getting converted to black. I have attached the input and output images.
Input Image :
Output Image:
I have used the following code for conversion:
img[np.where((img==[255,255,255]).all(axis=2))] = [0,0,0];
What should I do?
I know this has already been answered. I have a coded python solution for you.
Firstly I found this thread explaining how to remove white pixels.
The Result:
Another Test img:
Edit This is a way better and shorter method. I looked into it after @ZdaR commented on looping over an images matrix.
[Updated Code]
img = cv2.imread("Images/test.pnt")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
img[thresh == 255] = 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()
[Old Code]
img = cv2.imread("Images/test.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
white_px = np.asarray([255, 255, 255])
black_px = np.asarray([0, 0, 0])
(row, col) = thresh.shape
img_array = np.array(img)
for r in range(row):
for c in range(col):
px = thresh[r][c]
if all(px == white_px):
img_array[r][c] = black_px
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
erosion = cv2.erode(img_array, kernel, iterations = 1)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow("image", erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
Other Sources used: OpenCV Morphological Transformations
这篇关于在 OpenCV python 中将白色像素转换为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!