python - 如何使用opencv将一系列灰色像素转换为黑色?
问题描述
我正在使用 OpenCV 和 Tesseract。我试图将一系列灰色像素转换为黑色。例子:
如您所见,有一种“明亮”的多灰色,我需要将其转换为全黑,以便 OCR 能够更好地阅读它。
有谁知道该怎么做?
解决方案
import cv2
import numpy as np I = cv2.imread('imgPath')
## If you are interested in thresholding based on brightness,
## using grey channel or brightness channel from HSV is always a good idea :)
# Convert input RGB to HSV
HSV = cv2.cvtColor(I, cv2.COLOR_BGR2HSV)
# Get brightness channel
V = HSV[:, :, 2]
# Threshold all the very bright pixels
bw = 255*np.uint8(V < 200)
# save the image you want
cv2.imwrite("bw.png", bw)
推荐阅读
- string - valueerror:无法在 python 2.7 中将字符串转换为浮点数
- python - 工作 Azure HTTP 函数应用在发布时中断
- database - Sembast 中的 put 和 add 有什么区别?
- reactjs - 为什么我不能在状态 Reactjs 中保存这个请求
- javascript - npm 在安装包时显示错误
- python - 如何使用烧瓶获取上传文件的绝对url路径
- node.js - Docker 正在跳过 package.json 中的“@name/package”依赖项
- oracle - 从查询 IN PLSQL 创建表
- java - 从另一个表复制数据并继续时,在 JPA 实体中生成重复的 id 值
- javascript - 使用moment.js从给定日期查找一周中的日期