IHS图像融合 python完整代码
时间: 2023-07-23 12:26:41 浏览: 341
图像融合附python代码.zip
以下是一个简单的基于Python的IHS图像融合的完整代码:
```python
import numpy as np
from skimage import io
import matplotlib.pyplot as plt
def rgb2hsi(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
hsi = np.zeros_like(rgb)
num = ((r-g)+(r-b))/2
den = np.sqrt((r-g)**2+(r-b)*(g-b))
theta = np.arccos(num/den)
h = np.zeros_like(r)
h[b>=g] = theta[b>=g]
h[b<g] = 2*np.pi - theta[b<g]
h[den==0] = 0
s = 1 - 3*np.minimum(np.minimum(r,g),b)/(r+g+b)
s[r+g+b==0] = 0
i = (r+g+b)/3
hsi[:,:,0], hsi[:,:,1], hsi[:,:,2] = h, s, i
return hsi
def hsi2rgb(hsi):
h, s, i = hsi[:,:,0], hsi[:,:,1], hsi[:,:,2]
r, g, b = np.zeros_like(h), np.zeros_like(h), np.zeros_like(h)
idx = (0<=h) & (h<2*np.pi/3)
b[idx] = i[idx]*(1-s[idx])
r[idx] = i[idx]*(1+s[idx]*np.cos(h[idx])/np.cos(np.pi/3-h[idx]))
g[idx] = 3*i[idx] - (r[idx]+b[idx])
idx = (2*np.pi/3<=h) & (h<4*np.pi/3)
h[idx] = h[idx] - 2*np.pi/3
r[idx] = i[idx]*(1-s[idx])
g[idx] = i[idx]*(1+s[idx]*np.cos(h[idx])/np.cos(np.pi/3-h[idx]))
b[idx] = 3*i[idx] - (r[idx]+g[idx])
idx = (4*np.pi/3<=h) & (h<=2*np.pi)
h[idx] = h[idx] - 4*np.pi/3
g[idx] = i[idx]*(1-s[idx])
b[idx] = i[idx]*(1+s[idx]*np.cos(h[idx])/np.cos(np.pi/3-h[idx]))
r[idx] = 3*i[idx] - (g[idx]+b[idx])
rgb = np.zeros_like(hsi)
rgb[:,:,0], rgb[:,:,1], rgb[:,:,2] = r, g, b
return rgb
def ihs_fusion(img1, img2, alpha):
hsi1 = rgb2hsi(img1)
hsi2 = rgb2hsi(img2)
hsi_fusion = np.zeros_like(hsi1)
hsi_fusion[:,:,0] = alpha*hsi1[:,:,0] + (1-alpha)*hsi2[:,:,0]
hsi_fusion[:,:,1] = alpha*hsi1[:,:,1] + (1-alpha)*hsi2[:,:,1]
hsi_fusion[:,:,2] = alpha*hsi1[:,:,2] + (1-alpha)*hsi2[:,:,2]
rgb_fusion = hsi2rgb(hsi_fusion)
return rgb_fusion
# 使用示例
img1 = io.imread('image1.jpg')
img2 = io.imread('image2.jpg')
fusion = ihs_fusion(img1, img2, 0.5)
plt.imshow(fusion)
plt.show()
```
其中,`rgb2hsi`函数将RGB图像转换为HSI图像,`hsi2rgb`函数将HSI图像转换为RGB图像,`ihs_fusion`函数实现了IHS图像融合算法,其中`img1`和`img2`分别为待融合的两幅RGB图像,`alpha`为融合权重。在使用示例中,我们将两幅图像的融合权重设为0.5。
阅读全文