Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color levels in OpenCV

Tags:

opencv

I want to do something similar to the levels function in Photoshop, but can't find the right openCV functions.

Basically I want to stretch the greys in an image to go from almost white to practically black instead of from almost white to slightly greyer, while leaving white as white and black as black (I am using greyscale images).

like image 452
leinaD_natipaC Avatar asked Dec 19 '22 09:12

leinaD_natipaC


1 Answers

The following python code fully implements Photoshop Adjustments -> Levels dialog.

Change the values for each channel to the desired ones.

img is input rgb image of np.uint8 type.

inBlack  = np.array([0, 0, 0], dtype=np.float32)
inWhite  = np.array([255, 255, 255], dtype=np.float32)
inGamma  = np.array([1.0, 1.0, 1.0], dtype=np.float32)
outBlack = np.array([0, 0, 0], dtype=np.float32)
outWhite = np.array([255, 255, 255], dtype=np.float32)

img = np.clip( (img - inBlack) / (inWhite - inBlack), 0, 255 )                            
img = ( img ** (1/inGamma) ) *  (outWhite - outBlack) + outBlack
img = np.clip( img, 0, 255).astype(np.uint8)
like image 111
iperov Avatar answered Jan 16 '23 00:01

iperov