直方图与傅里叶变换

直方图

在这里插入图片描述

cv2.calcHist(images,channels,mask,histSize,ranges)

  • images: 原图像格式为uint8 或 float32,当传入函数是应用中括号【】
  • channels: 同样用中括号来他会告诉统幅图像的直方图,如果如图像是灰度图他的值就是【0】,如果是彩色图像的传入参数可以是【0】【1】【2】 他们分别对应着BGR
  • mask 淹没图像,统幅图像就把它None.但是如果你想同图像某一部分的你就制作一个淹没图像并使用它
  • histSize:BIN的数目。也用应用中括号
  • ranges 像素值范围常为【0256】

Demo

import cv2

img = cv2.imread('../imgs/cat.jpg',0)
# cv2.imshow('None',img)
# cv2.waitKey(0)

hist = cv2.calcHist([img],[0],None,[256],[0,256])
print(hist.shape)
# (256, 1)

在这里插入图片描述

画出统计图像

img = cv2.imread('../imgs/cat.jpg',0)
plt.hist(img.ravel(),256)
plt.show()

在这里插入图片描述

画出每个通道颜色的分布

# 默认读取彩色图片
img = cv2.imread('../imgs/cat.jpg')
color = ('b','g','r')
# 枚举,画出每个通道的颜色分布
for i ,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color=col)
    plt.xlim([0,256])

plt.show()

在这里插入图片描述

# mask操作
img = cv2.imread('../imgs/cat.jpg')
mask = np.zeros(img.shape[:2],np.uint8)
print(mask.shape)
mask[100:300,100:400]=255
# cv_show(mask,'mask')

img = cv2.imread('../imgs/cat.jpg',0)
cv_show(img,'img')

# 与操作
masked_img = cv2.bitwise_and(img,img,mask=mask)
cv_show(masked_img,'masked_img')

hist_full = cv2.calcHist([img],[0],None,[256],[0,256])
hist_mask = cv2.calcHist([img],[0],mask,[256],[0,256])

plt.subplot(221)
plt.imshow(img,'gray')
plt.subplot(222)
plt.imshow(mask,'gray')
plt.subplot(223)
plt.imshow(masked_img,'gray')
plt.subplot(224)
plt.plot(hist_full)
plt.plot(hist_mask)
plt.xlim([0,256])
plt.show()

在这里插入图片描述

直方图均衡化

plt.subplot(121)
img = cv2.imread('../imgs/cat.jpg',0) # 0 表示灰度图
plt.hist(img.ravel(),256)
plt.title('原图像')
# 使用均衡化函数来实现
plt.subplot(122)
equ = cv2.equalizeHist(img)
plt.hist(equ.ravel(),256)
plt.title('均衡化之后的图像')
plt.show()

在这里插入图片描述

展示图片

res = np.hstack((img,equ))
cv_show(res)
cv_end()

在这里插入图片描述

# 自适应均衡化
img = cv2.imread('../imgs/clahe.jpg',0)

plt.hist(img.ravel(),256)
# plt.hist(img.ravel(),256)
plt.show()
equ = cv2.equalizeHist(img)
# res = np.hstack((img,equ))

## 自适应直方图均衡化
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))

res_clahe = clahe.apply(img)
res = np.hstack((img,equ,res_clahe))
cv_show(res,'res')
cv_end()

在这里插入图片描述
在这里插入图片描述

傅里叶变换

傅里叶变换的作用

  • 高频:变换距离的灰度分量,边界
  • 低频:变换缓慢的灰度分量,例如一片大海

滤波

  • 底通滤波器:只保留低频,回事图像模糊

  • 高通滤波器:只保留高频,回事图像细节增强

  • opencv中主要是cv2.dft() 和 cv2.idit() ,输入图像需要先转换成np.,float32格式

  • 得到的结果频率是0的部分会在左上角,通常要转换到中心位置,可以通过shift实现

  • cv2.dft()返回的结果是双通道的(实部,虚部),通常还需要转换成图像格式才能显示(0,255)

img = cv2.imread('../imgs/lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

# 得到灰度图能表示的形式
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('Input Image')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(magnitude_spectrum,cmap='gray')
plt.title('Magnitude Spectrum')
plt.xticks([])
plt.yticks([])
plt.show()

在这里插入图片描述

img = cv2.imread('../imgs/lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

rows,cols = img.shape
crow,ccol = int(rows/2),int(cols/2)  # 中心位置

# 低波通道
mask = np.zeros((rows,cols,2),np.uint8)
mask[crow-30:crow+30,ccol-30:ccol+30]=1

# IDFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('Input Image')
plt.xticks([])
plt.yticks([])

plt.subplot(122)
plt.imshow(img_back,cmap='gray')
plt.title('Result')
plt.xticks([])
plt.yticks([])
plt.show()


在这里插入图片描述

img = cv2.imread('../imgs/lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

rows,cols = img.shape
crow,ccol = int(rows/2),int(cols/2)  # 中心位置

# 高通滤波
mask = np.ones((rows,cols,2),np.uint8)
mask[crow-30:crow+30,ccol-30:ccol+30]=0

# IDFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

plt.subplot(121)
plt.imshow(img,cmap='gray')
plt.title('Input Image')
plt.xticks([])
plt.yticks([])

plt.subplot(122)
plt.imshow(img_back,cmap='gray')
plt.title('Result')
plt.xticks([])
plt.yticks([])
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值