Contrast Detection - Picking the right Font color [closed]

asked 2019-09-03 10:53:24 -0600

holger gravatar image

Hello,

I hope this not off topic. For a data augmentation task (i need to generate a ocr dataset - and yes i already tried some public dataset) i want to generate text on images.

The text must still be readable. Is there some opencv algorithm which would help me deteremine the font color or areas in the picture where i can safly render the fonts?

Doing this manually take a some time for me and i want to automate this. Any ideas(no code needed) are highly welcome.

Greetings, Holger

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by holger
close date 2019-09-13 18:09:29.251760

Comments

Well i guess you just dont need a fancy algorithm to do this.

  1. Make sure your background image does not contain any text
  2. Just create an overlay with a know color and a known contrast color (there are color models out there)

I think ill just go for that cheap solution. I am still interested if i can solve this otherwise.

holger gravatar imageholger ( 2019-09-03 13:45:44 -0600 )edit

Well how about this:

  1. Crop a region
  2. Use Kmeans for each channel so you get 3 cluster centroids for r,g,b
  3. Determine which color is in contrast to the color from step 2)
  4. Use color from 3.) as either font or overlay color

Maybe my question is too special or commercial. I will try this out and let you know and post what i did in the end.

holger gravatar imageholger ( 2019-09-03 15:26:37 -0600 )edit

Are you doing silmilar mirc colour?

supra56 gravatar imagesupra56 ( 2019-09-04 21:32:35 -0600 )edit

I dont quite understand what this means (i did a quick google) - i am trying to generate synthetic text data on random background images so i can build a ocr / text detection dataset.

holger gravatar imageholger ( 2019-09-05 05:58:35 -0600 )edit

Ok so this is what i did in the end :

  1. Use kmeans to get the base color for the area where you want to draw
  2. Get the contrast color for that base color - just inverting the color gave me bad result sometimes. So i just compute the brightness - if it hit a certain treshhold - i will used it (adding it to list and then random picking from the list of candidates - the max brightness color will always be added to the list!)

How to compute brightness (taken from the internet....)

brightness = (299*R + 587*G + 114*B) / 1000

So in the end it was not complicated - it just took me some time to get the right idea.

holger gravatar imageholger ( 2019-09-13 18:07:37 -0600 )edit

Ok - so after doing what i wrote and having some good first results - i noticed that my approach fails sometimes.

So after googling a while - i found something official from the w3c. https://www.w3.org/TR/WCAG20-TECHS/G1...

I implemented this algorithm(computing contrast ratio >= 4.5) and its finally working now. On remark:

(L1 + 0.05) / (L2 + 0.05)

Just dive the bigger value by the lower value! (Math.max(L1, L2) + 0.05) / (Math.min(L1, L2) + 0.05);

Finally!!!

holger gravatar imageholger ( 2019-09-18 07:51:11 -0600 )edit