SHARE
TWEET

Untitled

a guest Feb 18th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def reinhard(target, img):
  2.  
  3.     #converts image and target from BGR colorspace to l alpha beta
  4.     lAB_img = cv2.cvtColor(img, cv2.COLOR_BGR2Lab)
  5.     lAB_tar = cv2.cvtColor(target, cv2.COLOR_BGR2Lab)
  6.  
  7.     #finds mean and standard deviation for each color channel across the entire image
  8.     (mean, std) = cv2.meanStdDev(lAB_img)
  9.     (mean_tar, std_tar) = cv2.meanStdDev(lAB_tar)
  10.  
  11.     #iterates over image implementing formula to map color normalized pixels to target image
  12.     for y in range(512):
  13.         for x in range(512):
  14.             lAB_tar[x, y, 0] = (lAB_img[x, y, 0] - mean[0]) / std[0] * std_tar[0] + mean_tar[0]
  15.             lAB_tar[x, y, 1] = (lAB_img[x, y, 1] - mean[1]) / std[1] * std_tar[1] + mean_tar[1]
  16.             lAB_tar[x, y, 2] = (lAB_img[x, y, 2] - mean[2]) / std[2] * std_tar[2] + mean_tar[2]
  17.     mapped = cv2.cvtColor(lAB_tar, cv2.COLOR_Lab2BGR)
  18.     return mapped
  19.    
  20. lAB_tar[:,:,0] = (lAB_img[:,:,0] - mean[0])/std[0] * std_tar[0] + mean_tar[0]
  21. lAB_tar[:,:,1] = (lAB_img[:,:,1] - mean[1])/std[1] * std_tar[1] + mean_tar[1]
  22. lAB_tar[:,:,2] = (lAB_img[:,:,2] - mean[2])/std[2] * std_tar[2] + mean_tar[2]
  23.    
  24. # implementing the formula
  25. #(Io - mo)/so*st + mt  = Io * (st/so) + mt - mo*(st/so)
  26. ratio = (std_tar/std_ori).reshape(-1)
  27. offset = (mean_tar - mean_ori*std_tar/std_ori).reshape(-1)
  28. lab_tar = cv2.convertScaleAbs(lab_ori*ratio + offset)
  29.    
  30. # 2019/02/19 by knight-金
  31. # https://stackoverflow.com/a/54757659/3547485
  32.  
  33. import numpy as np
  34. import cv2
  35.  
  36. def reinhard(target, original):
  37.     # cvtColor: COLOR_BGR2Lab
  38.     lab_tar = cv2.cvtColor(target, cv2.COLOR_BGR2Lab)
  39.     lab_ori = cv2.cvtColor(original, cv2.COLOR_BGR2Lab)
  40.  
  41.     # meanStdDev: calculate mean and stadard deviation
  42.     mean_tar, std_tar = cv2.meanStdDev(lab_tar)
  43.     mean_ori, std_ori = cv2.meanStdDev(lab_ori)
  44.  
  45.     # implementing the formula
  46.     #(Io - mo)/so*st + mt  = Io * (st/so) + mt - mo*(st/so)
  47.     ratio = (std_tar/std_ori).reshape(-1)
  48.     offset = (mean_tar - mean_ori*std_tar/std_ori).reshape(-1)
  49.     lab_tar = cv2.convertScaleAbs(lab_ori*ratio + offset)
  50.  
  51.     # convert back
  52.     mapped = cv2.cvtColor(lab_tar, cv2.COLOR_Lab2BGR)
  53.     return mapped
  54.  
  55. if __name__ == "__main__":
  56.     ori = cv2.imread("ori.png")
  57.     tar = cv2.imread("tar.png")
  58.  
  59.     mapped = reinhard(tar, ori)
  60.     cv2.imwrite("mapped.png", mapped)
  61.  
  62.     mapped_inv = reinhard(ori, tar)
  63.     cv2.imwrite("mapped_inv.png", mapped)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top