gpt4 book ai didi

image - 如何通过 opencv 和 python 只保留图像中具有特定颜色的文本?

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:11 25 4
gpt4 key购买 nike

我有一些发票图片和一些文字重叠,这给后期处理带来了一些麻烦,而我只有黑色的文字。有些我想删除其他颜色的文本。

有什么办法可以实现吗?

附上图片作为示例。

我试过用opencv解决,还是解决不了:

import numpy as np import cv2
img = cv2.imread('11.png')

lower = np.array([150,150,150])

upper = np.array([200,200,200])

mask = cv2.inRange(img, lower, upper)
res = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite('22.png',res)

[具有多种颜色的图像][1]

[1]: /image/nWQrV.pngstrong文本

最佳答案

文本颜色较深且饱和度较低。正如@J.D. 所建议的那样。 HSV 颜色空间很好。但是他的范围是错误的。

在 OpenCV 中,H 的范围是 [0, 180],而 S/V 的范围是 [0, 255]

这是我去年做的colormap,我觉得很有用。


(1) 使用cv2.inRange

(2) 仅阈值 V(HSV) channel :

th, threshed = cv2.threshold(v, 150, 255, cv2.THRESH_BINARY_INV)

(3) 仅对 S(HSV) channel 设置阈值:

th, threshed2 = cv2.threshold(s, 30, 255, cv2.THRESH_BINARY_INV)

结果:

enter image description here


演示代码:

# 2018/12/30 22:21 
# 2018/12/30 23:25

import cv2

img = cv2.imread("test.png")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

mask = cv2.inRange(hsv, (0,0,0), (180, 50, 130))
dst1 = cv2.bitwise_and(img, img, mask=mask)

th, threshed = cv2.threshold(v, 150, 255, cv2.THRESH_BINARY_INV)
dst2 = cv2.bitwise_and(img, img, mask=threshed)

th, threshed2 = cv2.threshold(s, 30, 255, cv2.THRESH_BINARY_INV)
dst3 = cv2.bitwise_and(img, img, mask=threshed2)

cv2.imwrite("dst1.png", dst1)
cv2.imwrite("dst2.png", dst2)
cv2.imwrite("dst3.png", dst3)

  1. How to detect colored patches in an image using OpenCV?

  2. How to define a threshold value to detect only green colour objects in an image :Opencv

关于image - 如何通过 opencv 和 python 只保留图像中具有特定颜色的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53977777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com