8.直方图与傅里叶变换
import cv2
import numpy as np
from matplotlib import pyplot as plt
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()
showGeneralHist()
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()
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 = np.zeros(img.shape[:2],np.uint8)
mask[50:150,50:150] = 255
maskImg = cv2.bitwise_and(img,img,mask=mask)
maskHistImg = cv2.calcHist([img],[0],mask,[256],[0,256])
fullHistImg = 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(maskImg,'gray')
plt.subplot(224),plt.plot(maskHistImg),plt.plot(fullHistImg)
plt.xlim([0,256])
plt.show()
maskHist()
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()
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()
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.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()