我想模仿廉價相機鏡頭(如

我選擇了 5 的最大模糊(這是高斯的 sigma,以像素為單位,而不是內核的足跡),blur_map這里用中間的 0 和角落的 1 之間的因子縮放這個 sigma圖片。
另一個有趣的效果如下,隨著影像中間每個圓的切向模糊增加,徑向模糊很少:
angle_map = dip.CreatePhiCoordinate(img.Sizes())
img_blur = dip.AdaptiveGauss(img, [angle_map, blur_map], sigmas=[8,1])

uj5u.com熱心網友回復:
這是在 Python/OpenCV 中應用(均勻、不變)鏡頭散焦模糊的一種方法,方法是將影像和濾波器都轉換到傅立葉(頻率)域。
- 讀取輸入
- 將輸入的 dft 轉換為傅立葉域
- 在黑色背景上繪制一個白色實心圓圈,輸入大小作為掩碼(過濾器內核)。這是空間域中的散焦核,即圓形矩形函式。
- 稍微模糊圓圈以消除邊緣的鋸齒
- 滾動蒙版,使中心位于原點(左上角)并標準化,使值的總和 = 1
- 將掩碼的 dft 轉換為傅立葉域,其中其幅度分布是一個 jinx 函式。
- 將兩個 dft 影像相乘以應用模糊
- 取乘積的 idft 變換回空間域
- 獲取乘積的實部和虛部的大小,裁剪并轉換為uint8作為結果
- 保存結果
輸入:

import numpy as np
import cv2
# read input and convert to grayscale
img = cv2.imread('lena_512_gray.png', cv2.IMREAD_GRAYSCALE)
# do dft saving as complex output
dft_img = np.fft.fft2(img, axes=(0,1))
# create circle mask
radius = 32
mask = np.zeros_like(img)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, 255, -1)[0]
# blur the mask slightly to antialias
mask = cv2.GaussianBlur(mask, (3,3), 0)
# roll the mask so that center is at origin and normalize to sum=1
mask_roll = np.roll(mask, (256,256), axis=(0,1))
mask_norm = mask_roll / mask_roll.sum()
# take dft of mask
dft_mask_norm = np.fft.fft2(mask_norm, axes=(0,1))
# apply dft_mask to dft_img
dft_shift_product = np.multiply(dft_img, dft_mask_norm)
# do idft saving as complex output
img_filtered = np.fft.ifft2(dft_shift_product, axes=(0,1))
# combine complex real and imaginary components to form (the magnitude for) the original image again
img_filtered = np.abs(img_filtered).clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("MASK", mask)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_512_gray_mask.png", mask)
cv2.imwrite("lena_dft_numpy_lowpass_filtered_rad32.jpg", img_filtered)
掩碼 - 空間域中的過濾器內核:

圓半徑 = 4 的結果:

圓半徑 = 8 的結果:

圓半徑 = 16 的結果:

圓半徑=32 的結果
:
添加
將 OpenCV 用于 dft 等而不是 Numpy,以上變為:
import numpy as np
import cv2
# read input and convert to grayscale
img = cv2.imread('lena_512_gray.png', cv2.IMREAD_GRAYSCALE)
# do dft saving as complex output
dft_img = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
# create circle mask
radius = 32
mask = np.zeros_like(img)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, 255, -1)[0]
# blur the mask slightly to antialias
mask = cv2.GaussianBlur(mask, (3,3), 0)
# roll the mask so that center is at origin and normalize to sum=1
mask_roll = np.roll(mask, (256,256), axis=(0,1))
mask_norm = mask_roll / mask_roll.sum()
# take dft of mask
dft_mask_norm = cv2.dft(np.float32(mask_norm), flags = cv2.DFT_COMPLEX_OUTPUT)
# apply dft_mask to dft_img
dft_product = cv2.mulSpectrums(dft_img, dft_mask_norm, 0)
# do idft saving as complex output, then clip and convert to uint8
img_filtered = cv2.idft(dft_product, flags=cv2.DFT_SCALE cv2.DFT_REAL_OUTPUT)
img_filtered = img_filtered.clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("MASK", mask)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_512_gray_mask.png", mask)
cv2.imwrite("lena_dft_opencv_defocus_rad32.jpg", img_filtered)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/495778.html
上一篇:裁剪影像中的所有字符
