Reputation: 9568
I am trying to refine an image through removing the noise. The image has a red color as dominant so I am trying to remove any other color except the red .. Here's an example of the image
I have found this code but couldn't use it properly. Please if you will put an answer consider me as very newbie and take it step by step as I need to learn not only to solve a problem
import cv2
import numpy as np
# Load image
im = cv2.imread('Output.png')
# Make all perfectly green pixels white
im[np.all(im == (193, 47, 47), axis=-1)] = (0,0,0)
# Save result
cv2.imwrite('result1.png',im)
I need to keep only the red color and the white as a background color.
I would like to refine the image so as to be able to extract numbers from it using such a code
def getCaptcha(img):
pytesseract.pytesseract.tesseract_cmd=r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img=Image.open(img)
text=pytesseract.image_to_string(img, lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
return text
print(getCaptcha('red_numerals_thresh.jpg'))
print(getCaptcha('red_numerals_result.jpg'))
Upvotes: 0
Views: 3167
Reputation: 53174
Here is one way to do that in Python OpenCV using cv2.inRange().
Input:
import cv2
import numpy as np
# Read image
img = cv2.imread('red_numerals.jpg')
# threshold red
lower = np.array([0, 0, 0])
upper = np.array([40, 40, 255])
thresh = cv2.inRange(img, lower, upper)
# Change non-red to white
result = img.copy()
result[thresh != 255] = (255,255,255)
# save results
cv2.imwrite('red_numerals_thresh.jpg', thresh)
cv2.imwrite('red_numerals_result.jpg', result)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Threshold image:
Result:
Upvotes: 1