detect foreground and background colors from bounding rectangle
Hello, I am pretty new to OpenCV and python. I have few screenshots of which I want to identify the text fore color and back color. Using pytesseract I was able to draw the bounding rectangle to the texts. Now Is there any ways to detect the text foreground and background colors? Below is my code so far
imgOriginal = cv2.imread('2.png')
image = imgOriginal.copy()
image_1 = imgOriginal.copy()
gray = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Removing the horizantal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255, 255, 255), 2)
cv2.imshow("HContours",image)
# Removing the vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 7))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255, 255, 255), 2)
cv2.imshow("VContours",image)
gray_no_lines = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
otsu = cv2.threshold(gray_no_lines, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
### Detecting words
boxes = pytesseract.image_to_data(otsu, config='--psm 6') # list
xs = []
ys = []
ws = []
hs = []
words = []
for i, b in enumerate(boxes.splitlines()):
if i != 0: # no need to extract the first row since it is the header
b = b.split()
if len(b) == 12: # 12th item is the word
if b[11] != -1:
x, y, w, h = int(b[6]), int(b[7]), int(b[8]), int(b[9])
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 1)
xs.append(x)
ys.append(y)
ws.append(w)
hs.append(h)
words.append(b[11])
cv2.imshow('Words',image)
The overall idea is to identify the text color contrast and correct the contrast as per color accessibility standards. Thanks in advance.