python opencv 實現Reinhard顏色遷移演算法
阿新 • • 發佈:2020-12-17
技術標籤:影象特徵
https://www.cnblogs.com/likethanlove/p/6003677.html
Reinhard顏色遷移演算法的過程很簡單,流程如下,細節部分見原文,題目為color transfer between images:
- 將參考圖片和目標圖片轉換到LAB空間下
- 得到參考圖片和目標圖片的均值和標準差
- 對目標圖片的每一個畫素值,減去目標影象均值然後乘上參考圖片和目標圖片標準差的比值,再加上參考影象均值
- 將目標圖片轉換到RGB空間
將RGB圖片轉換到LAB空間很重要,因為LAB空間能降低三原色之間的相關性,如果不轉換,結果會有很大的不同
# -*- coding: utf-8 -*- import cv2 import numpy as np image = cv2.imread('des.jpg') image = cv2.cvtColor(image,cv2.COLOR_BGR2LAB) original = cv2.imread('src.jpg') original = cv2.cvtColor(original,cv2.COLOR_BGR2LAB) def getavgstd(image): //得到均值和標準差 avg = [] std = [] image_avg_l = np.mean(image[:,:,0]) image_std_l = np.std(image[:,:,0]) image_avg_a = np.mean(image[:,:,1]) image_std_a = np.std(image[:,:,1]) image_avg_b = np.mean(image[:,:,2]) image_std_b = np.std(image[:,:,2]) avg.append(image_avg_l) avg.append(image_avg_a) avg.append(image_avg_b) std.append(image_std_l) std.append(image_std_a) std.append(image_std_b) return (avg,std) image_avg,image_std = getavgstd(image) original_avg,original_std = getavgstd(original) height,width,channel = image.shape for i in range(0,height): for j in range(0,width): for k in range(0,channel): t = image[i,j,k] t = (t-image_avg[k])*(original_std[k]/image_std[k]) + original_avg[k] t = 0 if t<0 else t t = 255 if t>255 else t image[i,j,k] = t image = cv2.cvtColor(image,cv2.COLOR_LAB2BGR) cv2.imwrite('out.jpg',image)