日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

第4章 Python 数字图像处理(DIP) - 频率域滤波5 - 二变量函数的傅里叶变换、图像中的混叠、二维离散傅里叶变换及其反变换

發(fā)布時(shí)間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第4章 Python 数字图像处理(DIP) - 频率域滤波5 - 二变量函数的傅里叶变换、图像中的混叠、二维离散傅里叶变换及其反变换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

    • 二變量函數(shù)的傅里葉變換
      • 二維沖激及其取樣性質(zhì)
      • 二維連續(xù)傅里葉變換對
      • 二維取樣和二維取樣定理
      • 圖像中的混疊
      • 二維離散傅里葉變換及其反變換

二變量函數(shù)的傅里葉變換

二維沖激及其取樣性質(zhì)

兩個(gè)連續(xù)變量的沖激函數(shù)定義為:
δ(t,z)={1,t=z=00,others(4.52)\delta(t, z) = \begin{cases} 1, & t=z=0 \\ 0, & \text{others} \end{cases} \tag{4.52}δ(t,z)={1,0,?t=z=0others?(4.52)
∫?∞∞∫?∞∞δ(t,z)dtdz(4.53)\int_{-\infty}^{\infty} \int_{-\infty}^{\infty} \delta(t, z) \textozvdkddzhkzdt \textozvdkddzhkzdz\tag{4.53}????δ(t,z)dtdz(4.53)

二維沖激在積分下展現(xiàn)了取樣性質(zhì)
∫?∞∞∫?∞∞f(t,z)δ(t,z)dtdz=f(0,0)(4.54)\int_{-\infty}^{\infty} \int_{-\infty}^{\infty} f(t,z) \delta(t, z) \textozvdkddzhkzdt \textozvdkddzhkzdz\ = f(0, 0) \tag{4.54}????f(t,z)δ(t,z)dtdz?=f(0,0)(4.54)
一般的情況
∫?∞∞∫?∞∞f(t,z)δ(t?t0,z?z0)dtdz=f(t0,z0)(4.55)\int_{-\infty}^{\infty} \int_{-\infty}^{\infty} f(t,z) \delta(t - t_0, z - z_0) \textozvdkddzhkzdt \textozvdkddzhkzdz\ = f(t_0, z_0) \tag{4.55}????f(t,z)δ(t?t0?,z?z0?)dtdz?=f(t0?,z0?)(4.55)

二維離散單位沖激定義為
δ(x,y)={1,x=y=00,others(4.56)\delta(x, y) = \begin{cases} 1, & x=y=0 \\ 0, & \text{others} \end{cases} \tag{4.56}δ(x,y)={1,0,?x=y=0others?(4.56)
取樣性質(zhì)為
∑?∞∞∑?∞∞f(x,y)δ(x?x0,y?y0)dxdy=f(x0,y0)(4.58)\sum_{-\infty}^{\infty} \sum_{-\infty}^{\infty} f(x,y) \delta(x - x_0, y - y_0) \textozvdkddzhkzdx \textozvdkddzhkzdy\ = f(x_0, y_0) \tag{4.58}????f(x,y)δ(x?x0?,y?y0?)dxdy?=f(x0?,y0?)(4.58)

二維連續(xù)傅里葉變換對

F(μ,v)=∫?∞∞∫?∞∞f(t,z)e?j2π(μt+vz)dtdz(4.59)F(\mu, v) = \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} f(t, z) e^{-j2\pi(\mu t + vz)} \textozvdkddzhkzdt \textozvdkddzhkzdz\tag{4.59}F(μ,v)=????f(t,z)e?j2π(μt+vz)dtdz(4.59)

f(t,z)=∫?∞∞∫?∞∞F(μ,v)ej2π(μt+vz)dμdv(4.60)f(t, z) = \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} F(\mu, v) e^{j2\pi(\mu t + vz)} \textozvdkddzhkzd\mu \textozvdkddzhkzdv\tag{4.60}f(t,z)=????F(μ,v)ej2π(μt+vz)dμdv(4.60)

μ\muμvvv是頻率變量,涉及圖像時(shí),tttzzz解釋為連續(xù)空間變量。變量μ\muμvvv的域定義了連續(xù)頻率域

# 二維盒式函數(shù)的傅里葉變換 height, width = 128, 128 m = int((height - 1) / 2) n = int((width - 1) / 2) f = np.zeros([height, width]) # T 控制方格的大小 T = 5 f[m-T:m+T, n-T:n+T] = 1fft = np.fft.fft2(f) shift_fft = np.fft.fftshift(fft) amp = np.log(1 + np.abs(shift_fft))plt.figure(figsize=(16, 16)) plt.subplot(2, 2, 1), plt.imshow(f, 'gray'), plt.title('Box filter'), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 2), plt.imshow(amp, 'gray'), plt.title('FFT Spectrum'), plt.xticks([]), plt.yticks([])# 不同的盒式函數(shù)對應(yīng)的傅里葉變換 height, width = 128, 128 m = int((height - 1) / 2) n = int((width - 1) / 2) f = np.zeros([height, width]) T = 20 f[m-T:m+T, n-T:n+T] = 1fft = np.fft.fft2(f) shift_fft = np.fft.fftshift(fft) amp = np.abs(shift_fft) plt.subplot(2, 2, 3), plt.imshow(f, 'gray'), plt.title('Box filter'), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 4), plt.imshow(amp, 'gray'), plt.title('FFT Spectrum'), plt.xticks([]), plt.yticks([]) plt.tight_layout() plt.show()

二維取樣和二維取樣定理

sΔTΔZ(t,z)=∑m=?∞∞∑n=?∞∞δ(t?mΔT,z?nΔZ)(4.61)s_{\Delta T \Delta Z}(t, z) = \sum_{m=-\infty}^{\infty} \sum_{n=-\infty}^{\infty} \delta(t - m \Delta T, z - n \Delta Z) \tag{4.61}sΔTΔZ?(t,z)=m=??n=??δ(t?mΔT,z?nΔZ)(4.61)

在區(qū)間[?μmax,μmax][-\mu_{max}, \mu_{max}][?μmax?,μmax?][?vmax,vmax][-v_{max}, v_{max}][?vmax?,vmax?]建立的頻率域矩形之外,函數(shù)f(t,z)f(t,z)f(t,z)的傅里葉變換為零,即時(shí),
F(μ,v)=0,∣μ∣≥μmax且∣v∣≥vmax(4.62)F(\mu, v) = 0, \quad |\mu| \ge \mu_{max} 且|v| \ge v_{max} \tag{4.62}F(μ,v)=0,μμmax?vvmax?(4.62)
稱該函數(shù)為帶限函數(shù)

二維取樣定理:
ΔT<12μmax(4.63)\Delta T < \frac{1}{2\mu_{max}} \tag{4.63}ΔT<2μmax?1?(4.63)

ΔZ<12vmax(4.64)\Delta Z < \frac{1}{2 v_{max}} \tag{4.64}ΔZ<2vmax?1?(4.64)

或者是:
1ΔT>2μmax(4.65)\frac{1} {\Delta T} > {2\mu_{max}} \tag{4.65}ΔT1?>2μmax?(4.65)

1ΔZ>2vmax(4.66)\frac{1} {\Delta Z} > {2 v_{max}} \tag{4.66}ΔZ1?>2vmax?(4.66)

則連續(xù)帶限函數(shù)f(t,z)f(t,z)f(t,z)可由基一組樣本無誤地復(fù)原。

圖像中的混疊

def get_check(height, width, check_size=(5, 5), lower=130, upper=255):"""create check pattern imageheight: input, height of the image you wantwidth: input, width of the image you wantcheck_size: the check size you want, default is 5x5lower: dark color of the check, default is 130, which is dark gray, 0 is black, 255 is whiteupper: light color of the check, default is 255, which is white, 0 is blackreturn uint8[0, 255] grayscale check pattern image"""m, n = check_sizeblack = np.zeros((m, n), np.uint8)white = np.zeros((m, n), np.uint8)black[:] = lower # darkwhite[:] = upper # whiteblack_white = np.concatenate([black, white], axis=1)white_black = np.concatenate([white, black], axis=1)black_white_black_white = np.vstack((black_white, white_black))tile_times_h = int(np.ceil(height / m / 2))tile_times_w = int(np.ceil(width / n / 2))img_temp = np.tile(black_white_black_white, (tile_times_h, tile_times_w))img_dst = np.zeros([height, width])img_dst = img_temp[:height, :width]return img_dst # 混疊 img_16 = get_check(512, 800, check_size=(16, 16), lower=10, upper=255) img_16_show = img_16[:48, :96] img_6 = get_check(512, 800, check_size=(6, 6), lower=10, upper=255) img_6_show = img_6[:48, :96]# 16 * 0.95 = 15.2 img_095 = img_16[::15, ::15] img_095 = np.concatenate((img_095, img_095[:, 3:]), axis=1) img_095 = np.concatenate((img_095, img_095[:, 3:]), axis=1) img_095 = np.concatenate((img_095, img_095[:, 3:]), axis=1) img_095 = np.concatenate((img_095, img_095[3:, :]), axis=0) img_095 = np.concatenate((img_095, img_095[3:, :]), axis=0) img_095 = img_095[2:50, 2:98]# 16 * 0.48 = 7.68 img_05 = img_16[::2, ::2] # 為了顯示這里的步長選了2 img_05 = img_05[:48, :96]fig = plt.figure(figsize=(15, 8)) plt.subplot(2, 2, 1), plt.imshow(img_16_show, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 2), plt.imshow(img_6_show, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 3), plt.imshow(img_095, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 4), plt.imshow(img_05, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.tight_layout() plt.show()

圖像重取樣和剛插

在圖像被取樣之前,必須在前端進(jìn)行搞混疊濾波,但對于違反取樣定理導(dǎo)致的混疊效應(yīng),事實(shí)上不存在事后能降低它的抗混疊濾波的軟件。多數(shù)抗混疊的功能主要是模糊數(shù)字圖像,進(jìn)行降低由重取樣導(dǎo)致的其他混疊偽影,而不能降低原取樣圖像中的混疊。

對數(shù)字圖像降低混疊,在重取樣之前 需要使用低通濾波器來平滑,以衰減數(shù)字圖像的高頻分量。但實(shí)際上只是平滑圖像,而減少了那些令人討厭的混疊現(xiàn)象。

def nearest_neighbor_interpolation(img, new_h, new_w):"""get nearest_neighbor_interpolation for image, can up or down scale image into any ratioparam: img: input image, grady image, 1 channel, shape like [512, 512]param: new_h: new image height param: new_w: new image widthreturn a nearest_neighbor_interpolation up or down scale image"""new_img = np.zeros([new_h, new_w])src_height, src_width = img.shape[:2]r = new_h / src_heightl = new_w / src_widthfor i in range(new_h):for j in range(new_w):x0 = int(i / r)y0 = int(j / l)new_img[i, j] = img[x0, y0]return new_img def box_filter(image, kernel):""":param image: input image:param kernel: input kernel:return: image after convolution"""img_h = image.shape[0]img_w = image.shape[1]m = kernel.shape[0]n = kernel.shape[1]# paddingpadding_h = int((m -1)/2)padding_w = int((n -1)/2)image_pad = np.zeros((image.shape[0]+padding_h*2, image.shape[1]+padding_w*2), np.uint8)image_pad[padding_h:padding_h+img_h, padding_w:padding_w+img_w] = imageimage_convol = image.copy()for i in range(padding_h, img_h + padding_h):for j in range(padding_w, img_w + padding_w):temp = np.sum(image_pad[i-padding_h:i+padding_h+1, j-padding_w:j+padding_w+1] * kernel)image_convol[i - padding_h][j - padding_w] = temp # 1/(m * n) * tempreturn image_convol # 抗混疊 img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH04/Fig0417(a)(barbara).tif', 0)# 先縮小圖像,再放大圖像 img_down = nearest_neighbor_interpolation(img_ori, int(img_ori.shape[0]*0.33), int(img_ori.shape[1]*0.33)) img_up = nearest_neighbor_interpolation(img_down, img_ori.shape[0], img_ori.shape[1])# 先對原圖像進(jìn)行5x5的平均濾波,再縮小圖像,再放大圖像 kernel = np.ones([5, 5]) kernel = kernel / kernel.size img_box_filter = box_filter(img_ori, kernel=kernel) img_down_1 = nearest_neighbor_interpolation(img_box_filter, int(img_ori.shape[0]*0.33), int(img_ori.shape[1]*0.33)) img_up_1 = nearest_neighbor_interpolation(img_down_1, img_ori.shape[0], img_ori.shape[1])fig = plt.figure(figsize=(15, 10)) plt.subplot(1, 3, 1), plt.imshow(img_ori, 'gray'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 3, 2), plt.imshow(img_up, 'gray'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 3, 3), plt.imshow(img_up_1, 'gray'), plt.xticks([]), plt.yticks([])plt.tight_layout() plt.show()

混疊和莫爾模式

莫爾模式是由近似等間隔的兩個(gè)光柵疊加所產(chǎn)生的一種視覺現(xiàn)象。

def rotate_image(img, angle=45):height, width = img.shape[:2]if img.ndim == 3:channel = 3else:channel = Noneif int(angle / 90) % 2 == 0:reshape_angle = angle % 90else:reshape_angle = 90 - (angle % 90)reshape_radian = np.radians(reshape_angle) # 角度轉(zhuǎn)弧度# 三角函數(shù)計(jì)算出來的結(jié)果會(huì)有小數(shù),所以做了向上取整的操作。new_height = int(np.ceil(height * np.cos(reshape_radian) + width * np.sin(reshape_radian)))new_width = int(np.ceil(width * np.cos(reshape_radian) + height * np.sin(reshape_radian)))if channel:new_img = np.zeros((new_height, new_width, channel), dtype=np.uint8)else:new_img = np.zeros((new_height, new_width), dtype=np.uint8)radian = np.radians(angle)cos_radian = np.cos(radian)sin_radian = np.sin(radian)# dx = 0.5 * new_width + 0.5 * height * sin_radian - 0.5 * width * cos_radian# dy = 0.5 * new_height - 0.5 * width * sin_radian - 0.5 * height * cos_radian# ---------------前向映射--------------------# for y0 in range(height):# for x0 in range(width):# x = x0 * cos_radian - y0 * sin_radian + dx# y = x0 * sin_radian + y0 * cos_radian + dy# new_img[int(y) - 1, int(x) - 1] = img[int(y0), int(x0)] # 因?yàn)檎w映射的結(jié)果會(huì)比偏移一個(gè)單位,所以這里x,y做減一操作。# ---------------后向映射--------------------dx_back = 0.5 * width - 0.5 * new_width * cos_radian - 0.5 * new_height * sin_radiandy_back = 0.5 * height + 0.5 * new_width * sin_radian - 0.5 * new_height * cos_radianfor y in range(new_height):for x in range(new_width):x0 = x * cos_radian + y * sin_radian + dx_backy0 = y * cos_radian - x * sin_radian + dy_backif 0 < int(x0) <= width and 0 < int(y0) <= height: # 計(jì)算結(jié)果是這一范圍內(nèi)的x0,y0才是原始圖像的坐標(biāo)。new_img[int(y), int(x)] = img[int(y0) - 1, int(x0) - 1] # 因?yàn)橛?jì)算的結(jié)果會(huì)有偏移,所以這里做減一操作。# # ---------------雙線性插值-------------------- # if channel: # fill_height = np.zeros((height, 2, channel), dtype=np.uint8) # fill_width = np.zeros((2, width + 2, channel), dtype=np.uint8) # else: # fill_height = np.zeros((height, 2), dtype=np.uint8) # fill_width = np.zeros((2, width + 2), dtype=np.uint8) # img_copy = img.copy() # # 因?yàn)殡p線性插值需要得到x+1,y+1位置的像素,映射的結(jié)果如果在最邊緣的話會(huì)發(fā)生溢出,所以給圖像的右邊和下面再填充像素。 # img_copy = np.concatenate((img_copy, fill_height), axis=1) # img_copy = np.concatenate((img_copy, fill_width), axis=0) # for y in range(new_height): # for x in range(new_width): # x0 = x * cos_radian + y * sin_radian + dx_back # y0 = y * cos_radian - x * sin_radian + dy_back # x_low, y_low = int(x0), int(y0) # x_up, y_up = x_low + 1, y_low + 1 # u, v = np.modf(x0)[0], np.modf(y0)[0] # 求x0和y0的小數(shù)部分 # x1, y1 = x_low, y_low # x2, y2 = x_up, y_low # x3, y3 = x_low, y_up # x4, y4 = x_up, y_up # if 0 < int(x0) <= width and 0 < int(y0) <= height: # pixel = (1 - u) * (1 - v) * img_copy[y1, x1] + (1 - u) * v * img_copy[y2, x2] + u * (1 - v) * img_copy[y3, x3] + u * v * img_copy[y4, x4] # 雙線性插值法,求像素值。 # new_img[int(y), int(x)] = pixelreturn new_img # 混疊和莫爾模式 # 豎線原圖 img_lines = np.ones([129, 129]) * 255 img_lines[:, ::3] = 0# 旋轉(zhuǎn)時(shí)使用剛內(nèi)插,產(chǎn)生了混疊 rotate_matrix = cv2.getRotationMatrix2D((int(img_lines.shape[0]*0.5), int(img_lines.shape[1]*0.5)), -5, 1) img_lines_r = cv2.warpAffine(img_lines, rotate_matrix, dsize=(img_lines.shape[0], img_lines.shape[1]), flags=cv2.INTER_CUBIC, borderValue=255)# 相加后,產(chǎn)生的混疊更明顯 img_add = img_lines + img_lines_r img_add = np.uint8(normalize(img_add) * 255)fig = plt.figure(figsize=(15, 10)) plt.subplot(2, 3, 1), plt.imshow(img_lines, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 3, 2), plt.imshow(img_lines_r, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 3, 3), plt.imshow(img_add, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([])# 格子原圖 img_check = np.ones([129, 129]) * 255 img_check[::2, ::2] = 0# 旋轉(zhuǎn)時(shí)使用剛內(nèi)插,產(chǎn)生了混疊 rotate_matrix = cv2.getRotationMatrix2D((int(img_check.shape[0]*0.5), int(img_check.shape[1]*0.5)), -5, 1) img_check_r = cv2.warpAffine(img_check, rotate_matrix, dsize=(img_check.shape[0], img_check.shape[1]), flags=cv2.INTER_CUBIC, borderValue=255)# 相加后,產(chǎn)生的混疊更明顯 img_add = img_check + img_check_r img_add = np.uint8(normalize(img_add) * 255)plt.subplot(2, 3, 4), plt.imshow(img_check, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 3, 5), plt.imshow(img_check_r, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([]) plt.subplot(2, 3, 6), plt.imshow(img_add, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([])plt.tight_layout() plt.show()

# 印刷采用欠取樣時(shí)產(chǎn)生莫爾模式效應(yīng) img_ori = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH04/Fig0421(car_newsprint_sampled_at_75DPI).tif', 0)fig = plt.figure(figsize=(15, 10)) plt.subplot(1, 3, 1), plt.imshow(img_ori, 'gray'), plt.xticks([]), plt.yticks([])plt.tight_layout() plt.show()

二維離散傅里葉變換及其反變換

二維DFT
Fu,v=∑x=0M?1∑y=0N?1f(x,y)e?j2π(ux/M+vy/N)(4.67)F_{u, v} = \sum_{x = 0}^{M - 1} \sum_{y = 0}^{N - 1} f(x, y) e^{-j2\pi(u x/M + v y /N)} \tag{4.67}Fu,v?=x=0M?1?y=0N?1?f(x,y)e?j2π(ux/M+vy/N)(4.67)

二維IDFT
f(x,y)=1MN∑u=0M?1∑v=0N?1F(u,v)ej2π(ux/M+vy/N)(4.68)f(x, y) = \frac{1}{MN}\sum_{u = 0}^{M - 1} \sum_{v = 0}^{N - 1} F(u, v) e^{j2\pi(u x /M + vy /N)} \tag{4.68}f(x,y)=MN1?u=0M?1?v=0N?1?F(u,v)ej2π(ux/M+vy/N)(4.68)

上式,u=0,1,2,?,M?1u = 0, 1, 2, \cdots, M-1u=0,1,2,?,M?1v=0,1,2,?,N?1v = 0, 1, 2, \cdots, N-1v=0,1,2,?,N?1x=0,1,2,?,M?1x = 0, 1, 2, \cdots, M-1x=0,1,2,?,M?1y=0,1,2,?,N?1y = 0, 1, 2, \cdots, N-1y=0,1,2,?,N?1

總結(jié)

以上是生活随笔為你收集整理的第4章 Python 数字图像处理(DIP) - 频率域滤波5 - 二变量函数的傅里叶变换、图像中的混叠、二维离散傅里叶变换及其反变换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。