import cv2 as cv
import numpy as np
import matplotlib.pyplot as pyplot
def darkchannel():
for i in range(0,rows-1):
for j in range(0,cols-1):
min_rgb = img_arr[i][j][0]
if min_rgb > img_arr[i][j][1]:
min_rgb = img_arr[i][j][1]
elif min_rgb > img_arr[i][j][2]:
min_rgb = img_arr[i][j][2]
for c in range(channels):
data[i][j][c] = min_rgb
def min_filter():
for i in range(0, rows):
for j in range(0, cols):
for c in range(0, channels):
if i == 0 or j == 0 or i == rows - 1 or j == cols - 1:
min_data[i][j][c] = data[i][j][c]
elif j == 0:
min_data[i][j][c] = data[i][j][c]
else:
min = 255
for m in range(i - 2, i + 2):
for n in range(j - 2, j + 2):
if min > data[m][n][c]:
min = data[m][n][c]
min_data[i][j][c] = min
def guided_filter():
#i:導向圖 p:輸入圖
# q = a * i + b
i = img_arr
mean_i = cv.blur(i, (r, r)) # i的均值平滑
mean_p = cv.blur(data, (r, r)) # p的均值平滑
mean_ii = cv.blur(i*i, (r, r)) # i*i的均值平滑
mean_ip = cv.blur(i*data, (r, r)) # i*p的均值平滑
var_i = mean_ii - mean_i * mean_i # 方差
cov_ip = mean_ip - mean_i * mean_p # 協方差
a = cov_ip / (var_i +eps)
b = mean_p - a *mean_i
mean_a = cv.blur(a, (r, r)) # 對a、b進行均值平滑
mean_b = cv.blur(b, (r, r))
q = mean_a*img_arr + mean_b
return q
def select_bright(data,v):
order = [0 for i in range(size)]
m = 0
for t in range(0,rows):
for j in range(0,cols):
order[m] = data[t][j][0]
m = m+1
order.sort(reverse=true)
index =int(size * 0.001) #從暗通道中選取亮度最大的前0.1%
img = img/255.0 # 歸一化
img_arr = np.array(img)
rows, cols, channels = img.shape
data = np.empty([rows,cols,channels],dtype=float)
min_data = np.empty([rows, cols, channels],dtype=float)
darkchannel()
min_filter()
v = guided_filter()
size = rows * cols
t, a = select_bright(data, v)
pyplot.imshow(min_data)
pyplot.axis("off")
pyplot.show()
def repair():
j = np.zeros(img_arr.shape)
for i in range(0,rows):
for j in range(0,cols):
for c in range(0,channels):
t[i][j][c] = t[i][j][c]-0.25 # 不知道為什麼這裡減掉0.25效果才比較好
j[i][j][c] = (img_arr[i][j][c]-a/255.0)/t[i][j][c]+a/255.0
return j
j = repair()
pyplot.imshow(j)
pyplot.axis("off")
pyplot.show()
cv.waitkey(0)
cv.destroyallwindows()
執行結果:
(1)原圖:
(2)暗通道圖:
(3)去霧圖:
至於引數為什麼要減掉0.25效果才好,已經不想思考…
瞎搞系列 暗通道去霧
基於暗通道先驗的去霧演算法實際上是一種統計演算法,發現了無霧影象中區域性存在一些畫素,這些畫素至少有乙個顏色通道的亮度值非常非常低 但是當影象區域白色的時候,這種演算法則存在缺陷 在計算機圖形學中,存在乙個比較重要的模型,這種模型的廣泛應用於霧圖中 其中i x 表示觀測到的亮度,j x 表示恢復後的...
暗通道先驗原理 DCP去霧演算法
一 霧圖形成模型 變形 c為r g b三通道。二 dark channel prior 統計規律 對於乙個無霧影象,每個區域性區域很可能至少乙個顏色通道會有很低的值,或黑色東西。非天空區域 每個區域性區域都總有一些很暗的東西。dark object subtraction 利用全圖最暗點來去除全域性...
關於暗通道先驗去霧問題的小結
前段日子導師甩給我一篇何博士的 叫做 single image haze removal using dark channel prior 利用統計得來的暗通道先驗實現對有霧影象的去霧問題,現大概說說對 中的幾個點。其中,i為haze image,即輸入影象,j haze free image,即目...