OpenCV8直方图与傅里叶变换

8.直方图与傅里叶变换

import cv2
import numpy as np
from matplotlib import pyplot as plt


#
# 读取图像直方图数据
#cv2.calcHist([img],[0],None,[256],[0,256])
#cv2.calcHist(image,channels,Mask,histSize,range)
# @image 图像数据 图像数据用[]包裹
# @channels  灰度图用[0]   彩色图用[0][1][2]代表BGR
# @mask 掩模图像  如果统计整幅图的直方图就用None表示  如果想统计图像一部分的直方图就制作一个掩模图像来使用它
# @histSize BIN的数目 也用[]包裹
# @ranges 像素范围常为[256]
 

#matplotlib显示直方图数据
# plt.hist(hist.ravel(),256)  #显示直方图数据 
# plt.show()  #matplotlib显示直方图

#展示普通直方图数据
def showGeneralHist():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg",cv2.IMREAD_GRAYSCALE)  #读取灰度图
    hist = cv2.calcHist([img],[0],None,[256],[0,256])   #统计一幅图像的直方图
    plt.hist(hist.ravel(),256)  #显示直方图数据 
    plt.show()  #matplotlib显示直方图

showGeneralHist()


#展示BGR图像直方图数据  BGR图像 [0][1][2]代表BGR

#打印多条数据到matplotlib
# plt.plot(histStr,color=colorItem)
# plt.xlim([0,256])

def showBGRHist():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg")
    color= ['b','g','r']
    for i,colorItem in enumerate(color):
        histStr = cv2.calcHist([img],[i],None,[256],[0,256])
        plt.plot(histStr,color=colorItem)
        plt.xlim([0,256])

showBGRHist()

#创建直方图掩码Mask
# mask = np.zeros(像素,np.uint8) 将整个像素图象置黑
# mask[x起始像素点:x轴截至像素点,y轴起始像素点:y轴截至像素点] = 255 将自定义需要统计直方图的区域置白

#创建掩码
def createHistMask():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg")
    print(img.shape)
    mask = np.zeros(img.shape[:2],np.uint8)
    mask[50:200,50:200] = 255
    cv2.imshow(r'lenna',mask)
    cv2.waitKey()
    cv2.destroyAllWindows()

#将图片与掩模&操作实现只展示置白的那部分图像
def bitImgAndMask():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg")
    print(img.shape)
    mask = np.zeros(img.shape[:2],np.uint8)
    mask[50:200,50:200] = 255
    imgDst = cv2.bitwise_and(img,img,mask=mask)
    cv2.imshow(r'lenna',imgDst)
    cv2.waitKey()
    cv2.destroyAllWindows()

#实现掩模图像直方图效果
def maskHist():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg",cv2.IMREAD_GRAYSCALE)
    #创建mask
    mask = np.zeros(img.shape[:2],np.uint8)
    mask[50:150,50:150] = 255
    #图像与mask&操作
    maskImg = cv2.bitwise_and(img,img,mask=mask)
    #mask直方图
    maskHistImg = cv2.calcHist([img],[0],mask,[256],[0,256]) 
    fullHistImg = cv2.calcHist([img],[0],mask,[256],[0,256]) 

    # matplotlib绘图
    plt.subplot(221),plt.imshow(img,'gray')
    plt.subplot(222),plt.imshow(mask,'gray')
    plt.subplot(223),plt.imshow(maskImg,'gray')
    plt.subplot(224),plt.plot(maskHistImg),plt.plot(fullHistImg)
    plt.xlim([0,256])
    plt.show()
#
# createHistMask()#创建掩码
# bitImgAndMask()#将图片与掩模&操作
maskHist()


#直方图均衡化
# equalHistDst = cv2.equalizeHist(imgData)

def histEqual():
    img = cv2.imread(r"C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg",cv2.IMREAD_GRAYSCALE)
    equalHistDst = cv2.equalizeHist(img)

    plt.subplot(221),plt.imshow(img,'gray')
    plt.subplot(222),plt.imshow(equalHistDst,'gray')
    plt.subplot(223),plt.suptitle(r"未均衡化"),plt.hist(img.ravel(),256)
    plt.subplot(224),plt.suptitle(r"均衡化后"),plt.hist(equalHistDst.ravel(),256)
    plt.xlim([0,256])
    plt.show()

histEqual()

#自适应直方图均衡化  
# 1.clahe = cv2.createCLAHE(clipLimit=0.2,tileGridSize=(8,8))  
# @clipLimit   
# @tileGridSize 划分为多少个格子

# 2.imgDst = clahe.apply(img)  生成自适应直方图数据

def claheDst():
    img = cv2.imread(r'C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg',cv2.IMREAD_GRAYSCALE)
    clahe = cv2.createCLAHE(clipLimit=0.2,tileGridSize=(8,8))  #生成自适应直方图对象
    imgDst = clahe.apply(img) #将自适应直方图对象应用到图像中来
    plt.subplot(221),plt.imshow(img,'gray')
    plt.subplot(222),plt.imshow(imgDst,'gray')
    plt.subplot(223),plt.hist(img.ravel(),256)
    plt.subplot(224),plt.hist(imgDst.ravel(),256)

    plt.xlim([0,256])
    plt.show()

claheDst()





# 傅里叶变化
# @高频  高频的图像显示的实部,显示的是轮廓
# @低频  低频的图像显示的是虚部,显示的轮廓的内部

#滤波
#低频滤波器  只保留低频,会使图像模糊
#高频滤波器  只保留高频,会使图像细节增强

# opencv中主要使用cv2.dft()和cv2.idft(),输入图像需要先转换为np.float32()格式
# 得到的结果中频滤为0的部分会在左上角,通常要转换到中心位置,可以通过shift变换来实现
# cv2.dft()返回的结果是双通道的(实部,虚部),通常还需要转换成图像格式才能展示(0,255)

def useDFT():
    img = cv2.imread(r'C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg',cv2.IMREAD_GRAYSCALE)
    img_float32 = np.float32(img)
    dftImg = cv2.dft(img_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
    dftImg = np.fft.fftshift(dftImg)
    #得到灰度图能表示的形式
    dst = 20* np.log(cv2.magnitude(dftImg[:,:,0],dftImg[:,:,1]))
    plt.subplot(121),plt.imshow(img,cmap='gray')
    plt.title('inputImage'),plt.xticks([]),plt.yticks([])
    plt.subplot(122),plt.imshow(dst,cmap='gray')
    plt.title('MagineImage'),plt.xticks([]),plt.yticks([])
    plt.show()
useDFT()

#低频滤波器
def lowDFT():
    img = cv2.imread(r'C:\Users\Administrator\Pictures\Camera Roll\lenna.jpg',cv2.IMREAD_GRAYSCALE)
    img_float32 = np.float32(img)
    dft = cv2.dft(img_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
    dft = np.fft.fftshift(dft)

    rows,cols = img.shape
    row = int(rows/2)
    col = int(cols/2)

    #制作低通滤波器
    # mask = np.zeros((rows,cols,2),np.uint8)
    # mask[row-30:row+30,col-30:col+30] = 255
    #制作高通滤波器
    mask = np.ones((rows,cols,2),np.uint8)
    mask[row-30:row+30,col-30:col+30] = 0

    dft*=mask
    idft = np.fft.ifftshift(dft)
    idft = cv2.idft(idft)
    mImg = cv2.magnitude(idft[:,:,0],idft[:,:,1])

    plt.subplot(121),plt.imshow(img,cmap='gray')
    plt.title('input'),plt.xticks([]),plt.yticks([])
    plt.subplot(122),plt.imshow(mImg,cmap='gray')
    plt.title('maginude'),plt.xticks([]),plt.yticks([])
    plt.show()
lowDFT()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值