retinex算法求亮度分量python代码
时间: 2023-03-27 20:01:54 浏览: 119
以下是 retinex 算法求亮度分量的 Python 代码:
```python
import cv2
import numpy as np
def retinex(img, sigma_list):
retinex_img = np.zeros_like(img)
for sigma in sigma_list:
img_blur = cv2.GaussianBlur(img, (, ), sigma)
img_blur = np.where(img_blur == , .1, img_blur)
retinex = np.log10(img) - np.log10(img_blur)
retinex_img += retinex
retinex_img = retinex_img / len(sigma_list)
return retinex_img
img = cv2.imread('image.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sigma_list = [15, 80, 250]
retinex_img = retinex(img, sigma_list)
cv2.imwrite('retinex.jpg', retinex_img)
```
希望能对您有所帮助。
相关问题
多尺度retinex算法求亮度分量python代码
以下是多尺度retinex算法求亮度分量的Python代码:
```python
import cv2
import numpy as np
def retinex(img, sigma_list):
retinex_img = np.zeros_like(img)
for sigma in sigma_list:
img_blur = cv2.GaussianBlur(img, (, ), sigma)
img_blur = np.where(img_blur == , .1, img_blur)
retinex = np.log10(img) - np.log10(img_blur)
retinex_img += retinex
retinex_img = retinex_img / len(sigma_list)
return retinex_img
img = cv2.imread('image.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_retinex = retinex(img, [15, 80, 250])
img_retinex = np.uint8(np.clip((img_retinex + 3.) * 64, , 255))
cv2.imshow('Retinex', img_retinex)
cv2.waitKey()
```
希望能对你有所帮助!
将rgb转化成为hsi 并利用retinex算法将亮度分量分解成L和R成分python代码
以下是将RGB转换为HSI的Python代码:
```python
import numpy as np
def rgb_to_hsi(rgb_img):
# 将RGB图像的像素值归一化到[0, 1]范围内
r, g, b = rgb_img[:, :, 0] / 255.0, rgb_img[:, :, 1] / 255.0, rgb_img[:, :, 2] / 255.0
# 计算亮度、饱和度、色相
i = (r + g + b) / 3.0
s = 1 - (3.0 / (r + g + b + 1e-10) * np.minimum(np.minimum(r, g), b))
h = np.arccos((0.5 * ((r - g) + (r - b))) / (np.sqrt((r - g) ** 2 + (r - b) * (g - b)) + 1e-10))
h[b > g] = 2 * np.pi - h[b > g]
h /= 2 * np.pi
# 将HSI图像的像素值从[0, 1]范围内转换回[0, 255]
h, s, i = h * 255, s * 255, i * 255
hsi_img = np.zeros_like(rgb_img)
hsi_img[:, :, 0], hsi_img[:, :, 1], hsi_img[:, :, 2] = h, s, i
return hsi_img.astype(np.uint8)
```
以下是利用Retinex算法将亮度分量分解成L和R成分的Python代码:
```python
import cv2
import numpy as np
def retinex_decomposition(img, sigma_list):
img = img.astype(np.float32)
img_sum = np.zeros_like(img)
img_min = np.ones_like(img) * np.inf
for sigma in sigma_list:
img_blur = cv2.GaussianBlur(img, (0, 0), sigma)
img_diff = img - img_blur
img_sum += np.log10(img_diff + 1e-10)
img_min = np.minimum(img_min, img_diff)
R = np.exp(img_sum / len(sigma_list))
L = np.log10(np.maximum(img_min, 1e-10))
return L, R
```
其中,`img`为输入图像,`sigma_list`是高斯核的标准差列表,返回值`L`和`R`分别为亮度分量和反射分量。
阅读全文