gpt4 book ai didi

python - Python图像退化函数的实现和应用

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:54 27 4
gpt4 key购买 nike

我一直在尝试遵循“数字图像处理”(冈萨雷斯和伍兹)一书中的示例过程。该过程是通过对图像进行傅里叶变换并乘以 H(u,v),最后进行傅里叶逆变换来降级和模糊图像。

formula

这是我到目前为止所得到的:

from __future__ import division
import cv2
import numpy as np
import scipy as sp
import math
from scipy.signal import convolve2d

img = cv2.imread('book_cover.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

xSize, ySize = gray_img.shape


def applyFilter(img, func):
image = np.copy(img)

# Construct image from blurring function
for u in xrange(0,ySize):
for v in xrange(0,xSize):
image[u,v] = func(u,v)

# Performe the actual blurring of the image. Not working as expected
return image*img

def blurr(y,x):
a = 0.05
b = 0.05
T = 1
C = math.pi*(a*y+b*x)

if(C == 0):
return 1

return (T/C)*math.sin(C)*math.e**(-1j*C)

def toReal(img):
realImg = np.zeros(img.shape)

for i in xrange(0,img.shape[0]):
for j in xrange(0,img.shape[1]):
realImg[i,j] = np.absolute(img[i,j])

return realImg
def normalize(image):
img = image.copy()
img = toReal(img)
img -= img.min()
img *= 255.0/img.max()
return img.astype(np.uint8)

f = np.fft.fft2(gray_img.astype(np.int32))
fft_img = np.fft.fftshift(f)

# Apply the blurring filter
filtered_fft = applyFilter(fft_img, blurr)

f_fft_img = np.fft.ifftshift(filtered_fft)
filtered_img = np.fft.ifft2(f_fft_img)

filtered_img = normalize(filtered_img)


cv2.imwrite('book_cover_blurred.jpg', filtered_img.astype(np.uint8))

左边两张封面是从书中截取的,最左边的是原图,中间的是经过滤镜处理的。最右边的是我用上面的代码得到的结果。 Link to images

我肯定误解了一些东西,但我完全不知所措。

最佳答案

我最后发现了我犯的错误。我犯了一个愚蠢的错误,实现了 H(u,v) 而不是正确的 H(u-M/2,v-N/2)。

关于python - Python图像退化函数的实现和应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com