How can I remove blue background color on image using OpenCV?
我正在尝试删除下面图像上的蓝色背景色。
蓝色可以是浅色或深色。
我尝试使用
我怎样才能做到这一点?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import sys import cv2 import numpy as np image = cv2.imread(sys.argv[1]) hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower_blue = np.array([85, 50, 40]) upper_blue = np.array([135, 255, 255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) image[mask>0]=(255, 255, 255) cv2.imshow('image',image) cv2.waitKey(0) |
我删除了背景,并对图像进行了OCR。 结果如下:
和我使用的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import pytesseract import cv2 pytesseract.pytesseract.tesseract_cmd = 'C:\\\\Program Files (x86)\\\\Tesseract-OCR\\\\tesseract.exe' img = cv2.imread('idText.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) adaptiveThresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 35, 90) config = '-l eng --oem 1 --psm 3' text = pytesseract.image_to_string(adaptiveThresh, config=config) print("Result:" + text) cv2.imshow('original', img) cv2.imshow('adaptiveThresh', adaptiveThresh) cv2.waitKey(0) |
希望我能帮到你。
您可以尝试阈值获取二进制图像,并进行形态转换以平滑文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import cv2 image = cv2.imread('1.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray,105, 255, cv2.THRESH_BINARY_INV)[1] thresh = 255 - thresh kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) result = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1) cv2.imshow('thresh', thresh) cv2.imshow('result', result) cv2.imwrite('result.png', result) cv2.waitKey() |