python 低通滤波

低通滤波:

import cv2
import numpy as np

def function(img):
  h,w=img.shape
  newimg=np.zeros((h,w),np.uint8)

  img2=np.fft.fft2(img)
  fshift = np.fft.fftshift(img2)
  st=fshift.copy()
  h,w=fshift.shape
  sh=h/2
  sw=w/2
  r=40
  for i in range(h):
      for j in range(w):
          if ((sh - i) * (sh - i) + (sw - j) * (sw - j)) <= r * r:
              newimg[i, j] = 255
              tmp = 1
          else:
              tmp = 0
          st[i, j] = tmp * fshift[i, j]


  sl=np.fft.ifftshift(st)
  x2=np.fft.ifft2(sl)
  x3=np.uint8(np.real(x2))
  return newimg,x3

img=cv2.imread('e:\\lena.bmp',cv2.IMREAD_GRAYSCALE)
img1,img2=function(img)
cv2.imshow("image",img)
cv2.imshow("1",img1)
cv2.imshow("ideal",img2)
cv2.waitKey(0)

python 低通滤波_第1张图片
可以看到有明显的晕染

你可能感兴趣的:(python+图像处理)