歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Python OpenCV 實現Reinhard顏色遷移算法

Reinhard顏色遷移算法的過程很簡單,流程如下,細節部分見原文,題目為color transfer between images:

  1. 將參考圖片和目標圖片轉換到LAB空間下
  2. 得到參考圖片和目標圖片的均值和標准差
  3. 對目標圖片的每一個像素值,減去目標圖像均值然後乘上參考圖片和目標圖片標准差的比值,再加上參考圖像均值
  4. 將目標圖片轉換到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)

結果如下:

OpenCV官方教程中文版(For Python) PDF  http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu 14.04安裝OpenCV2.4.9  http://www.linuxidc.com/Linux/2016-07/132884.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 16.04上用CMake圖形界面交叉編譯樹莓派的OpenCV3.0 http://www.linuxidc.com/Linux/2016-10/135914.htm

Ubuntu 16.04中安裝OpenCV 2.4.11  http://www.linuxidc.com/Linux/2016-07/132882.htm

Ubuntu 16.04下Matlab2014a+Anaconda2+OpenCV3.1+Caffe安裝  http://www.linuxidc.com/Linux/2016-07/132860.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2014-12/110045.htm

Ubuntu 安裝 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2016-12/138293.htm

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved