轉載請註明出處:https://blog.csdn.net/weixin_38285131/article/details/88097771
最近在做街景圖象色彩校正方面的工作,對於過暗,過曝光,以及背光等現象,用過一些gamma校正以及其他的方法,最終選擇基於Retinex原理的方法對這幾種現象都有一定的增強效果。
Retinex理論基於一下假設:
1.真實世界是無顏色的,我們所感知的顏色是光與物質的相互作用的結果。我們見到的水是無色的,但是水膜—肥皂膜卻是顯現五彩繽紛,那是薄膜表面光干涉的結果。
2.每一顏色區域由給定波長的紅、綠、藍三原色構成的;
3.三原色決定了每個單位區域的顏色。
Retinex理論的基礎理論是物體的顏色是由物體對長波(紅色)、中波(綠色)、短波(藍色)光線的反射能力來決定的,而不是由反射光強度的絕對值來決定的,物體的色彩不受光照非均勻性的影響,具有一致性,即retinex是以色感一致性(顏色恆常性)爲基礎的。不同於傳統的線性、非線性的只能增強圖像某一類特徵的方法,Retinex可以在動態範圍壓縮、邊緣增強和顏色恆常三個方面達到平衡,因此可以對各種不同類型的圖像進行自適應的增強。
40多年來,研究人員模仿人類視覺系統發展了Retinex算法,從單尺度Retinex算法,MSR改進成多尺度加權平均的MSR算法,再發展成彩色恢復多尺度MSRCR算法和色彩增益加權的AutoMSRCR算法。
主要算法公式介紹可以參考如下博客:
https://blog.csdn.net/ajianyingxiaoqinghan/article/details/71435098
我再對這幾種方法稍稍總結一下:
一丶單尺度的Retinex——SSR
可以理解爲圖像分解,一幅圖像S(x,y)可以分爲他的光照圖象 I(x,y)和反射圖像R(x,y),反射圖象是根據物體本身的反射特性,所以基本不會發生變化,光照圖像是根據環境明暗來決定的。
只看公式的話感覺一下就看懵逼了,我感覺就三個步驟:
1)將圖像進行log變換
2)然後將log圖像進行高斯模糊
3)利用原圖和模糊之後的log圖像做差分
二丶多尺度的Retinex——MSR
通俗解釋:就是再多個單尺度Retinex做平均,區別是在第二步高斯模糊是選擇的sigma是不同的
原始圖像進行三次SSR
高斯模糊選擇15,80,200作爲高斯模糊sigma參數
對三次的SSR結果做平均即爲MSR圖像
三丶彩色恢復多尺度Retinex——MSRCR,MSRCP等
對多尺度MSR結果做了色彩平衡,歸一化,增益和偏差線性加權
四丶參數說明
{
“sigma_list”: [15, 80, 200],多尺度高斯模糊sigma值
“G” : 5.0,增益
“b” : 25.0,偏差
“alpha” : 125.0,
“beta” : 46.0,
“low_clip” : 0.01,
“high_clip” : 0.99
}
五丶圖像增強結果:
六丶代碼
import numpy as np
import cv2
def singleScaleRetinex(img, sigma):
retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
return retinex
def multiScaleRetinex(img, sigma_list):
retinex = np.zeros_like(img)
for sigma in sigma_list:
retinex += singleScaleRetinex(img, sigma)
retinex = retinex / len(sigma_list)
return retinex
def colorRestoration(img, alpha, beta):
img_sum = np.sum(img, axis=2, keepdims=True)
color_restoration = beta * (np.log10(alpha * img) - np.log10(img_sum))
return color_restoration
def simplestColorBalance(img, low_clip, high_clip):
total = img.shape[0] * img.shape[1]
for i in range(img.shape[2]):
unique, counts = np.unique(img[:, :, i], return_counts=True)
current = 0
for u, c in zip(unique, counts):
if float(current) / total < low_clip:
low_val = u
if float(current) / total < high_clip:
high_val = u
current += c
img[:, :, i] = np.maximum(np.minimum(img[:, :, i], high_val), low_val)
return img
def MSRCR(img, sigma_list, G, b, alpha, beta, low_clip, high_clip):
img = np.float64(img) + 1.0
img_retinex = multiScaleRetinex(img, sigma_list)
img_color = colorRestoration(img, alpha, beta)
img_msrcr = G * (img_retinex * img_color + b)
for i in range(img_msrcr.shape[2]):
img_msrcr[:, :, i] = (img_msrcr[:, :, i] - np.min(img_msrcr[:, :, i])) / \
(np.max(img_msrcr[:, :, i]) - np.min(img_msrcr[:, :, i])) * \
255
img_msrcr = np.uint8(np.minimum(np.maximum(img_msrcr, 0), 255))
img_msrcr = simplestColorBalance(img_msrcr, low_clip, high_clip)
return img_msrcr
def automatedMSRCR(img, sigma_list):
img = np.float64(img) + 1.0
img_retinex = multiScaleRetinex(img, sigma_list)
for i in range(img_retinex.shape[2]):
unique, count = np.unique(np.int32(img_retinex[:, :, i] * 100), return_counts=True)
for u, c in zip(unique, count):
if u == 0:
zero_count = c
break
low_val = unique[0] / 100.0
high_val = unique[-1] / 100.0
for u, c in zip(unique, count):
if u < 0 and c < zero_count * 0.1:
low_val = u / 100.0
if u > 0 and c < zero_count * 0.1:
high_val = u / 100.0
break
img_retinex[:, :, i] = np.maximum(np.minimum(img_retinex[:, :, i], high_val), low_val)
img_retinex[:, :, i] = (img_retinex[:, :, i] - np.min(img_retinex[:, :, i])) / \
(np.max(img_retinex[:, :, i]) - np.min(img_retinex[:, :, i])) \
* 255
img_retinex = np.uint8(img_retinex)
return img_retinex
def MSRCP(img, sigma_list, low_clip, high_clip):
img = np.float64(img) + 1.0
intensity = np.sum(img, axis=2) / img.shape[2]
retinex = multiScaleRetinex(intensity, sigma_list)
intensity = np.expand_dims(intensity, 2)
retinex = np.expand_dims(retinex, 2)
intensity1 = simplestColorBalance(retinex, low_clip, high_clip)
intensity1 = (intensity1 - np.min(intensity1)) / \
(np.max(intensity1) - np.min(intensity1)) * \
255.0 + 1.0
img_msrcp = np.zeros_like(img)
for y in range(img_msrcp.shape[0]):
for x in range(img_msrcp.shape[1]):
B = np.max(img[y, x])
A = np.minimum(256.0 / B, intensity1[y, x, 0] / intensity[y, x, 0])
img_msrcp[y, x, 0] = A * img[y, x, 0]
img_msrcp[y, x, 1] = A * img[y, x, 1]
img_msrcp[y, x, 2] = A * img[y, x, 2]
img_msrcp = np.uint8(img_msrcp - 1.0)
return img_msrcp
import sys
import os
import cv2
import json
import retinex
data_path = 'data'
img_list = os.listdir(data_path)
if len(img_list) == 0:
print( 'Data directory is empty.')
exit()
with open('config.json', 'r') as f:
config = json.load(f)
for img_name in img_list:
if img_name == '.gitkeep':
continue
img = cv2.imread(os.path.join(data_path, img_name))
print('msrcr processing......')
img_msrcr = retinex.MSRCR(
img,
config['sigma_list'],
config['G'],
config['b'],
config['alpha'],
config['beta'],
config['low_clip'],
config['high_clip']
)
cv2.imshow('MSRCR retinex', img_msrcr)
cv2.imwrite("MSRCR_retinex.tif",img_msrcr);
print('amsrcr processing......')
img_amsrcr = retinex.automatedMSRCR(
img,
config['sigma_list']
)
cv2.imshow('autoMSRCR retinex', img_amsrcr)
cv2.imwrite('AutomatedMSRCR_retinex.tif', img_amsrcr)
print('msrcp processing......')
img_msrcp = retinex.MSRCP(
img,
config['sigma_list'],
config['low_clip'],
config['high_clip']
)
shape = img.shape
cv2.imshow('Image', img)
cv2.imshow('MSRCP', img_msrcp)
cv2.imwrite('MSRCP.tif', img_msrcp)
cv2.waitKey()