Python OpenCV:将背景颜色更改为白色

时间:2018-09-06 06:23:22

标签: python opencv

任何人都可以共享用于将附加图像的背景颜色更改为白色的代码,以便我可以使用OCR软件识别数字的前景吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要转换为灰色,应用对比度并使用阈值来获得以下结果: enter image description here

import cv2
import numpy as np

img = cv2.imread("digits.jpg", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)[...,0]
# edges = cv2.Canny(gray, 10,30)    
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(32,32))
contrast = clahe.apply(blurred)
ret, thresh = cv2.threshold(contrast, 20, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)


while True:
  cv2.imshow("result", thresh)
  k = cv2.waitKey(30) & 0xff
  if k == 27:
      break