import numpy as np
def create_gaussian_kernel(k, sigma):
center = k // 2
kernel = np.zeros((k, k), dtype=np.float32)
for i in range(k):
for j in range(k):
kernel[i, j] = (1 / (2 * np.pi * sigma**2)) * np.exp(
-((i - center) ** 2 + (j - center) ** 2) / (2 * sigma**2)
)
kernel /= np.sum(kernel)
return kernel
def conv_2d(img, k, sigma=None):
assert k % 2 != 0, "卷积核应为奇数"
pad = k // 2
if sigma is None:
kernel = np.ones((k, k), dtype=np.float32) / (k * k)
else:
kernel = create_gaussian_kernel(k, sigma)
if len(img.shape) == 2:
H, W = img.shape
img_padded = np.zeros((img.shape[0] + pad * 2, img.shape[1] + pad * 2))
img_padded[pad:-pad, pad:-pad] = img
out = np.zeros_like(img, dtype=np.float32)
for h in range(H):
for w in range(W):
out[h, w] = (img_padded[h : h + k, w : w + k] * kernel).sum()
else:
H, W, C = img.shape
img_padded = np.zeros((img.shape[0] + pad * 2, img.shape[1] + pad * 2, C))
img_padded[pad:-pad, pad:-pad] = img
out = np.zeros_like(img, dtype=np.float32)
for h in range(H):
for w in range(W):
for c in range(C):
out[h, w, c] = (img_padded[h : h + k, w : w + k, c] * kernel).sum()
out = np.clip(out, 0, 255).astype(np.uint8)
return out
def normalize_image(img):
min_val = np.min(img)
max_val = np.max(img)
img_normalized = (img - min_val) * (255 / (max_val - min_val))
img_normalized = np.clip(img_normalized, 0, 255)
img_normalized = img_normalized.astype(np.uint8)
return img_normalized
def my_filters(img, D=30, tag=None):
assert tag=='low-pass' or tag=='high-pass', "please choose a tag in 'low-pass' or 'high-pass'"
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
H, W = img.shape
crow, ccol = int(H/2), int(W/2)
if tag=='low-pass':
mask = np.zeros((H, W), np.uint8)
mask[crow-D:crow+D, ccol-D:ccol+D] = 1
md = fshift * mask
epsilon = 1e-10
magnitude_spectrum_md = 20 * np.log(np.abs(md) + epsilon)
ishift_low = np.fft.ifftshift(md)
iimg_low = np.fft.ifft2(ishift_low)
iimg_low = np.abs(iimg_low)
iimg_low = normalize_image(iimg_low)
return iimg_low, magnitude_spectrum_md
else:
fshift[crow-D:crow+D, ccol-D:ccol+D] = 0
ishift_high = np.fft.ifftshift(fshift)
iimg_high = np.fft.ifft2(ishift_high)
iimg_high = np.abs(iimg_high)
iimg_high = normalize_image(iimg_high)
magnitude_spectrum_fshift = 20 * np.log(np.abs(fshift)+1)
return iimg_high, magnitude_spectrum_fshift
def blend_images(image1, image2, d1=30, d2=10, d3=20):
assert image1.shape == image2.shape, "image1 shape should equal to image2 shape"
fA = np.fft.fft2(image1)
fshiftA = np.fft.fftshift(fA)
fB = np.fft.fft2(image2)
fshiftB = np.fft.fftshift(fB)
rows, cols = image1.shape
crow, ccol = int(rows/2), int(cols/2)
maskA = np.zeros((rows, cols), np.uint8)
maskA[crow-d1:crow+d1, ccol-d1:ccol+d1] = 1
md = fshiftA * maskA
fshiftB[crow-d2:crow+d2, ccol-d2:ccol+d2] = 0
fshiftB[crow-d3:crow+d3, ccol-d3:ccol+d3] = md[crow-d3:crow+d3, ccol-d3:ccol+d3]
ishiftC = np.fft.ifftshift(fshiftB)
iimgC = np.fft.ifft2(ishiftC)
iimgC = np.abs(iimgC)
iimgC = normalize_image(iimgC)
return iimgC
def gaussian_pyramid(image, levels=6):
pyramid_images = [image]
for _ in range(levels-1):
image = image[::2, ::2]
pyramid_images.append(image)
return pyramid_images
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具