-2

I'm trying to process images that have bloc of text in rectangle with colored background.

See below original picture - I would need all text and numbers in black and all background in white to make it easier to read text.

I'm thinking about having a grayscale version and its opposite and find the area of the colored background and use that area from the inverted picture to replace the same area from the grayscale. I can't find the rectangle of interest though (blue and yellow in the picture)

Input image

Any idea to make all text black in white background in the entire image ?

prosoitos
  • 6,679
  • 5
  • 27
  • 41
benJ
  • 11
  • 1
  • 3
    SO is not a coding service. Please read the following documentation, then [edit], and rephrase the question. [Take the Tour](https://stackoverflow.com/tour), [How to ask a good question](https://stackoverflow.com/help/how-to-ask), & [On Topic](https://stackoverflow.com/help/on-topic). Always provide a [mre] with **code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)** & you're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592). – Trenton McKinney Oct 08 '20 at 01:00

2 Answers2

0

In your case,

1. get connected components (connected components, MSER, region growing, ..., etc.)
2. thresh by width.  # half the size of image would do in the example.
      long (background / boxes / lines) : white
      short (letters)                   : black
Joonyoung Park
  • 474
  • 3
  • 6
0

I was able to detect the contour of the colored background (see code below) - What is the best transformation to ensure that the text in those blocks are black and turn the background white ?

thank you!

`

    # Make greyscale version and inverted, thresholded greyscale version
gr = cv2.cvtColor(page_image,cv2.COLOR_BGR2GRAY)





plt.figure(figsize = (30,30))
plt.imshow(gr)
plt.show() 

blur = cv2.GaussianBlur(gr, (7,7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Create rectangular structuring element and dilate
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) #5,5
dilate = cv2.dilate(thresh, kernel, iterations=4)

# Find contours and draw rectangle
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

thr=(gr.shape[1]/2)    
print(thr)

for c in cnts:
    x,y,w,h = cv2.boundingRect(c)

    if w>thr and h>20 :
        cv2.rectangle(gr, (x,y ), (x+w,y+h ), (36,255,12), 2)

`

benJ
  • 11
  • 1