-4

i have to correct in Python the color blue level like in Photoshop ( CTRL + L ). I found the code in this topic: Color levels in OpenCV , but this is for RGB and I need it for the Blue only. Please Help. TKS.

  • 2
    Select the blue channel with slicing: image[:,:,0] (assuming the color format is the default of opencv - BGR) – frab Feb 17 '21 at 10:27
  • Thanks for the reply, that was my first try but I get an error:img = np.clip( (img[:,:,0] - inBlack) / (inWhite - inBlack), 0, 255 ) ValueError: operands could not be broadcast together with shapes (1024,1024) (3,) – Michele Feb 17 '21 at 10:44
  • that's because in that thread inBlack and inWhite are defined as 3-dimensional vectors, if you select just the blue channel inBlack and inWhite should be scalars – frab Feb 17 '21 at 10:47

2 Answers2

0

Use split function to seperate the channels

b,g,r = cv2.split(img)

or slicing

b = img[:,:,0]
Ashish M J
  • 642
  • 1
  • 5
  • 11
  • I have done 2 tries: 1st (frab reply) I have done this:inBlack = np.array([0,0,0], dtype=np.float32) inWhite = np.array([255], dtype=np.float32) inGamma = np.array([1.0], dtype=np.float32) outBlack = np.array([218], dtype=np.float32) outWhite = np.array([255], dtype=np.float32) but the result was a light gray img. The second (Ahsish reply) : I have splitted the colors in b,g,r ; used th b color in my code, but when i try to merge them at final i have an error :type error: merge() takes at most 2 arguments. – Michele Feb 17 '21 at 11:59
0

Ok thanks to all. I've solved. This is my code:

    img = cv2.imread("normal.png",1)
    b,g,r = cv2.split(img)
    
    
    inBlack  = np.array([0], dtype=np.float32)
    inWhite  = np.array([255], dtype=np.float32)
    inGamma  = np.array([1.0], dtype=np.float32)
    outBlack = np.array([218], dtype=np.float32)
    outWhite = np.array([255], dtype=np.float32)

    b = np.clip( (b - inBlack) / (inWhite - inBlack), 0, 255 )                            
    b = ( b ** (1/inGamma) ) *  (outWhite - outBlack) + outBlack
    b = np.clip( b, 0, 255).astype(np.uint8)
    
    img = cv2.merge((b,g,r))