Reinhard颜色迁移算法
应该是处理速度最快的写法了(cpu)
import numpy as np
import cv2
def get_avgstd_cv2(image1, image2):
avg1, std1 = cv2.meanStdDev(image1)
avg2, std2 = cv2.meanStdDev(image2)
return avg1.flatten().astype(np.float32), std1.flatten().astype(np.float32), avg2.flatten().astype(
np.float32), std2.flatten().astype(np.float32)
def rct(src, dst):
image = cv2.cvtColor(src, cv2.COLOR_BGR2LAB).astype(np.float32)
original = cv2.cvtColor(dst, cv2.COLOR_BGR2LAB)
image_avg, image_std, original_avg, original_std = get_avgstd_cv2(image, original)
np.subtract(image, image_avg, out=image)
np.divide(original_std, image_std, out=original_std)
np.multiply(image, original_std, out=image)
np.add(image, original_avg, out=image)
np.clip(image, 0, 255, out=image)
image = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_LAB2BGR)
return image
if __name__ == '__main__':
p1 = r'test1.jpg'
p2 = r'test2.jpg'
p1 = cv2.imread(p1)
p2 = cv2.imread(p2)
image = rct(p1, p2)
cv2.imwrite(r'out.jpg', image)