opencv image processing⑧

 

Table of contents

histogram

 mask operation

 Histogram equalization

 Adaptive Histogram Equalization

 Fourier transform

 low pass filter

 high pass filter


histogram

 

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

  • images: The image format of the original image is uint8 or float32. When passing in a function, use square brackets [] to enclose it, such as [img]
  • channels: Also enclosed in square brackets, it will tell the function the histogram of our unified image. If the input image is a grayscale image, its value is [0] If it is a color image, the incoming parameters can make [0][1][2] they correspond to BGR respectively
  • mask: mask image. For the histogram of the entire image, set him to None. But if you want to unify the histogram of a certain part of the image you make a mask image and use that.
  • histSize: the number of BINs. It should also be enclosed in square brackets.
  • ranges: The pixel value range is usually [0,256]
img=cv2.imread('cat.jpg',0) #0表示灰度图
hist=cv2.calaHist([img],[0],None,[256],[0,256])
hist.shape
(256,1)

plt.hist(img.ravel(),256);
plt.show()

 

img=cv2.imread('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])

 mask operation

 

#创建mask
mask=np.zeros(img.shape[:2],np.uint8)
mask[100:300,100:400]=255#要保存的是白色,所以置为255
cv_show(mask,'mask')

 

 

img=cv2.imread('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_full=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()

 

 Histogram equalization

 Cumulative probability is used

 

img=cv2.imread('cat.jpg',0)#0表示灰度图
plt.hist(img.ravel(),256);
plt.show()

 

 

equ=cv2.equalizeHist(img)#均衡化函数
plt.hist(equ.ravel(),256)
plt.show()

 

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

Use the clahe image to equalize the image as follows. After  equalizing the overall average , the details of some areas will be lost, and regional equalization can be performed.

 Adaptive Histogram Equalization

chahe=cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8)) #自适应均衡化

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

 

 Fourier transform

 

 

 

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

img=cv2.imread('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,camp='gray')
plt.title('Input Image'),pltxticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,cmap='gray')
plt.title('Magnitude Spectrum'),pltxticks([]),plt.yticks([])
plt.show()

 low pass filter

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

img=cv2.imread('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.magnitude(img_back[:,:,0],img_back[:,:,1])


plt.subplot(121),plt.imshow(img,camp='gray')
plt.title('Input Image'),pltxticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,cmap='gray')
plt.title('Result'),pltxticks([]),plt.yticks([])
plt.show()

 high pass filter

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

img=cv2.imread('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.magnitude(img_back[:,:,0],img_back[:,:,1])


plt.subplot(121),plt.imshow(img,camp='gray')
plt.title('Input Image'),pltxticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,cmap='gray')
plt.title('Result'),pltxticks([]),plt.yticks([])
plt.show()

 

Guess you like

Origin blog.csdn.net/weixin_58176527/article/details/125208853