0

In Photoshop, it is possible to adjust the color curves of L,a,and b in the CIE Lab* color space. I am attempting to perform a similar process for image enhancement using gamma correction in python.

However, this only adjusts the curve in one direction, either towards the top left or bottom right. Is there any transformation I can perform in python to adjust the curves?

I have applied gamma correction techniques but that skews the color too much one way.

I have tried the below code for gamma correction

def adjust_gamma(image, gamma=1.0):
  m,n,c = image.shape
  res = np.zeros((m,n,c))
  # build a lookup table mapping the pixel values [0, 255] to
  # their adjusted gamma values
  invGamma = 1.0 / gamma
  table = np.array([((i / 255.0) ** invGamma) * 255
    for i in np.arange(0, 256)]).astype("uint8")

  # apply gamma correction using the lookup table
  return cv2.LUT(image, table,res)

def toLAB(image, input_type = 'BGR'):
  conversion = cv2.COLOR_BGR2LAB if input_type == 'BGR' else cv2.COLOR_RGB2LAB
  image_LAB = cv2.cvtColor(image, conversion)

  y,x,z = image_LAB.shape
  LAB_flat = np.reshape(image_LAB, [y*x,z])

  colors = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if input_type == 'BGR' else image
  colors = np.reshape(colors, [y*x,z])/255.

  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')
  ax.scatter(xs=LAB_flat[:,2], ys=LAB_flat[:,1], zs=LAB_flat[:,0], s=10,  c=colors, lw=0)
  ax.set_xlabel('A')
  ax.set_ylabel('B')
  ax.set_zlabel('L')

  plt.show()

  return image_LAB 

lab_image = toLAB(image_BGR)


L,a,b = cv2.split(lab_image)

l = np.zeros((500,500,3))
l[:,:,0] = b
l[:,:,1] = b
l[:,:,2] = b

l = np.uint8(l)


adjusted = adjust_gamma(l, gamma=1.2)

cv2_imshow(np.hstack([l, adjusted]))

gamma_lab = cv2.merge([L,a,adjusted[:,:,0]])



# final_image = cv2.cvtColor(merged_channels, cv2.COLOR_LAB2BGR)
new = cv2.cvtColor(gamma_lab, cv2.COLOR_LAB2BGR)```
Deepti Hegde
  • 51
  • 1
  • 2
  • Not sure I understand what you are doing, but I would have expected to only apply a gamma to the `L` channel, not the the `a` and `b` channels of a `Lab` image. – Mark Setchell Jul 19 '19 at 16:27
  • @MarkSetchell I thought of gamma correction because I wanted to perform an operation on the output curves. Now I am aware it won't work on ```a``` and ```b``` since they don't have luminence information. My question is, what operation can I perform on the curves to adjust the colors as is possible in Photoshop? i.e. make it an S-shaped curve – Deepti Hegde Jul 20 '19 at 05:22
  • @DeeptiHegde:You just need a function which transform values from 0 to 1 (adjust for MAX) into new values from 0 to 1. Basic function (which do nothing) is *f(x)=x*. You may use s shaped curve (logistic) or spline (so you can divide in more places). This question is not about python, but about the meaning of curves. You will find online explanation, but you may also test it. Try with a gradient, and apply filters (curves). Try to understand how such curves works. then it is just your choice of a function '[0,1] -> [0,1]'. Everybody uses different curves. Your eyes/style define it. – Giacomo Catenazzi Jul 23 '19 at 14:53

0 Answers0