GGG
GGG

Reputation: 141

How to convert a range of gray pixels to black with opencv?

I am working with OpenCV and Tesseract. I was trying to convert a range of grey pixels to black. Example: Grey color

As you can see there is a "bright" multiple grey color, I need to convert that to full black for OCR to be able to read it better.

Does anyone know how to do so?

Upvotes: 1

Views: 1207

Answers (1)

Operator77
Operator77

Reputation: 424

import cv2                                                                                                                                    
import numpy as np                                                                                                                            I = cv2.imread('imgPath')
## If you are interested in thresholding based on brightness,
## using grey channel or brightness channel from HSV is always a good idea :)  
# Convert input RGB to HSV
HSV = cv2.cvtColor(I, cv2.COLOR_BGR2HSV)
# Get brightness channel
V = HSV[:, :, 2]
# Threshold all the very bright pixels
bw = 255*np.uint8(V < 200)  
# save the image you want
cv2.imwrite("bw.png", bw)  

enter image description here

Upvotes: 3

Related Questions