Reputation: 81
I'm trying to remove the blue background color on below image.
The blue color can be light or deep.
I tried to use cv2.inRange()
function but failed.
How can I do that?
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)
Upvotes: 4
Views: 6128
Reputation: 2269
I removed the background and also did OCR on the image. Here is the result:
And the code I used:
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)
Hope I helped you.
Upvotes: 4
Reputation: 46600
You can try thresholding to obtain a binary image and morphological transformations to smooth the text
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()
Upvotes: 1