import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False
def gasuss_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
image:原始图像
mean : 均值
var : 方差,越大,噪声越大
'''
image = np.array(image/255, dtype=float)#将原始图像的像素值进行归一化,除以255使得像素值在0-1之间
noise = np.random.normal(mean, var ** 0.5, image.shape)#创建一个均值为mean,方差为var呈高斯分布的图像矩阵
out = image + noise#将噪声和原始图像进行相加得到加噪后的图像
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)#clip函数将元素的大小限制在了low_clip和1之间了,小于的用low_clip代替,大于1的用1代替
out = np.uint8(out*255)#解除归一化,乘以255将加噪后的图像的像素值恢复
#cv.imshow("gasuss", out)
noise = noise*255
return [noise,out]
def BGR2HSI(bgr_img):
'''
BGR转换HSI颜色模型
:param bgr_img:
:return:
'''
bgr=bgr_img.copy()
B,G,R=cv2.split(bgr/255.0)
hsi_img=bgr.copy()/255.0
H,S,I=cv2.split(hsi_img)
h,w=B.shape
for i in range(h):
for j in range(w):
bgr_min=min(B[i,j],G[i,j],R[i,j])
bgr_sum=B[i,j]+G[i,j]+R[i,j]
I[i,j]=bgr_sum/3
S[i,j]=1-3*bgr_min/bgr_sum
cov=(R[i,j]-G[i,j])+(R[i,j]-B[i,j])
var=2*np.sqrt((R[i,j]-G[i,j])**2+(R[i,j]-B[i,j])*(G[i,j]-B[i,j])**2)
theta=np.arccos(cov/var)
if G[i,j]>=B[i,j]:
H[i,j]=theta/(2*np.pi)
else:
H[i,j]=(2*np.pi-theta)/(2*np.pi)
hsi_img[:,:,0]=H
hsi_img[:,:,1]=S
hsi_img[:,:,2]=I
return hsi_img
def HSI2BGR(hsi_img):
'''
HSI转换BGR颜色模型
:param hsi_img:
:return:
'''
hsi=hsi_img.copy()
H,S,I=cv2.split(hsi)
bgr_img=hsi_img.copy()
B,G,R=cv2.split(bgr_img)
h,w=B.shape
for i in range(h):
for j in range(w):
if S[i,j]<1e-6:
R[i,j]=I[i,j]
G[i,j]=I[i,j]
B[i,j]=I[i,j]
else:
H[i,j]*=360
if H[i,j]>0 and H[i,j]<=120:
B[i,j]=(1-S[i,j])*I[i,j]
sigma=(H[i,j]-60)*np.pi/180
temp=np.tan(sigma)/np.sqrt(3)
G[i,j]=(1.5+1.5*temp)*I[i,j]-(0.5+1.5*temp)*B[i,j]
R[i,j]=3*I[i,j]-G[i,j]-B[i,j]
elif H[i,j]>120 and H[i,j]<=240:
R[i,j]=(1-S[i,j])*I[i,j]
sigma=(H[i,j]-180)*np.pi/180
temp=np.tan(sigma)/np.sqrt(3)
B[i,j]=(1.5+1.5*temp)*I[i,j]-(0.5+1.5*temp)*R[i,j]
G[i,j]=3*I[i,j]-R[i,j]-B[i,j]
elif H[i,j]>240 and H[i,j]<=360:
G[i,j]=(1-S[i,j])*I[i,j]
sigma=(H[i,j]-300)*np.pi/180
temp=np.tan(sigma)/np.sqrt(3)
R[i,j]=(1.5+1.5*temp)*I[i,j]-(0.5+1.5*temp)*G[i,j]
B[i,j]=3*I[i,j]-G[i,j]-R[i,j]
bgr_img[:,:,0]=B
bgr_img[:,:,1]=G
bgr_img[:,:,2]=R
return bgr_img
if __name__ == '__main__':
#读取图片,添加高斯噪声,转换HSI
src=cv2.imread('./cat/cat1.png')
[noise,img]=gasuss_noise(src)
hsi_img=BGR2HSI(img)
#拆分通道
B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]
H = hsi_img[:, :, 0]
S = hsi_img[:, :, 1]
I = hsi_img[:, :, 2]
plt.figure(1)
plt.subplot(231), plt.imshow(B), plt.title('B')
plt.subplot(232), plt.imshow(G), plt.title('G')
plt.subplot(233), plt.imshow(R), plt.title('R')
plt.subplot(234), plt.imshow(H), plt.title('H')
plt.subplot(235), plt.imshow(S), plt.title('S')
plt.subplot(236), plt.imshow(I), plt.title('I')
B_blur = cv2.blur(B, (3, 3))
G_blur = cv2.blur(G, (3, 3))
R_blur = cv2.blur(R, (3, 3))
img_new = cv2.merge([B_blur, G_blur, R_blur]) # 通道合并
I_blur = cv2.blur(I, (3, 3))
hsi_new = cv2.merge([H, S, I_blur])# 通道合并后转换位BGR显示
hsi_new_img = HSI2BGR(hsi_new)
cf = img_new - hsi_new_img
plt.figure(2)
plt.subplot(231), plt.imshow(img_new), plt.title('RGB模型下均值滤波'), plt.ylabel('k=3')
plt.subplot(232), plt.imshow(hsi_new_img), plt.title('HSI模型下均值滤波')
plt.subplot(233), plt.imshow(cf), plt.title('作差')
# 均值滤波核变大后的效果
B_blur = cv2.blur(B, (12, 12))
G_blur = cv2.blur(G, (12, 12))
R_blur = cv2.blur(R, (12, 12))
img_new = cv2.merge([B_blur, G_blur, R_blur]) # 通道合并
I_blur = cv2.blur(I, (12, 12))
hsi_new = cv2.merge([H, S, I_blur]) # 通道合并后转换位BGR显示
hsi_new_img = HSI2BGR(hsi_new)
cf = img_new - hsi_new_img
plt.subplot(234), plt.imshow(img_new), plt.ylabel('k=12')
plt.subplot(235), plt.imshow(hsi_new_img)
plt.subplot(236), plt.imshow(cf)
plt.show()
数字图像处理——彩色图像平滑HSI均值滤波、RGB图像进行平滑处理
最新推荐文章于 2024-06-30 11:04:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)