数字图像处理-傅里叶变换系列编程

news/2024/12/16 1:08:18/

在这里插入图片描述
在这里插入图片描述

问题

问题1

通过计算一维傅里叶变换实现图像二维快速傅里叶变换
在这里插入图片描述
代码实现

def dft2D(f):h,w = f.shapeF = np.zeros(f.shape, dtype=complex)for i in range(h):F[i,:] = np.fft.fft(f[i,:])for i in range(w):F[:,i] = np.fft.fft(F[:,i])return F 

问题2

图像二维快速傅里叶逆变换
在这里插入图片描述
代码实现

def idft2D(F):h,w = F.shapeF1 = np.conj(F)f = np.zeros(F1.shape,dtype=complex)for i in range(h):f[i,:] = np.fft.fft(F1[i,:])for i in range(w):f[:,i] = np.fft.fft(f[:,i])f = f/(h*w)f = np.conj(f)f = np.abs(f)return f

问题3

测试图像二维快速傅里叶变换与逆变换
代码

import cv2
import numpy as np
#归一化
def normalization(data):_range = np.max(data) - np.min(data)return (data - np.min(data)) / _rangedef dft2D(f):h,w = f.shapeF = np.zeros(f.shape, dtype=complex)for i in range(h):F[i,:] = np.fft.fft(f[i,:])for i in range(w):F[:,i] = np.fft.fft(F[:,i])return Fdef idft2D(F):h,w = F.shapeF1 = np.conj(F)f = np.zeros(F1.shape,dtype=complex)for i in range(h):f[i,:] = np.fft.fft(F1[i,:])for i in range(w):f[:,i] = np.fft.fft(f[:,i])f = f/(h*w)f = np.conj(f)f = np.abs(f)return fif __name__ == '__main__':f1 = cv2.imread("rose512.tif",cv2.IMREAD_GRAYSCALE)f = normalization(f1)F = dft2D(f)g = idft2D(F)d = np.abs(f-g)cv2.imshow('original', f1)cv2.imshow('g', g)cv2.imshow('d', d)cv2.waitKey(0)cv2.destroyAllWindows()        

结果展示
在这里插入图片描述

问题4

计算图像的中心化二维快速傅里叶变换与谱图像

问题分析

首先合成矩形图像,测试结果如下图所示,其长为 60,宽为 10。
在这里插入图片描述

代码

import numpy as np
import cv2
import Q3if __name__ == '__main__':img = np.zeros((512,512))img[512//2-30:512//2+30,512//2-5:512//2+5]=1cv2.imshow('img', img)F1 = Q3.dft2D(img)F1 = Q3.normalization(np.abs(F1))img_c = np.zeros(img.shape)cv2.imshow('F1',  F1)for i in range (img.shape[0]):for j in range (img.shape[1]):img_c[i,j] = img[i,j]*((-1)**(i+j))F2 = Q3.dft2D(img_c)cv2.imshow('F2', Q3.normalization(np.abs(F2)))F3 = np.log(1+np.abs(F2))cv2.imshow('F3',  Q3.normalization(F3))cv2.waitKey(0)cv2.destroyAllWindows()

结果展示
在这里插入图片描述

选做题

计算其他 5 幅 图像的二维快速傅里叶变换 : house.tif, house02.tif, lena_gray_512.tif, lunar_surface.tif, characters_test_pattern.tif。注意,有些图像的尺寸不是 2 的整数次幂,需要进行相应的像素填补处理。如果图像有多个通道可以选择其中的一个通道进行计算。

问题分析
在这里插入图片描述
代码实现

import numpy as np
import cv2
import Q3def isPower(n):if n < 1:return Falsei = 1while i <= n:if i == n:return Truei <<= 1return Falsedef pad(img):if isPower(img.shape[0]) and isPower(img.shape[1]):return imgh = w = 0i = 1while True:if 2**i>=img.shape[0] and h==0:h = 2**iif 2**i>=img.shape[1] and w==0:w = 2**iif h!=0 and w!=0:breaki=i+1img2 = np.zeros((h,w))img2[(h-img.shape[0])//2:h-(h-img.shape[0])//2,(w-img.shape[1])//2:w-(w-img.shape[1])//2] = imgreturn img2def center(f):img_c = np.zeros(f.shape)for i in range(f.shape[0]):for j in range(f.shape[1]):img_c[i, j] = f[i, j] * ((-1) ** (i + j))return img_cif __name__ == '__main__':f1 = cv2.imread("house.tif", cv2.IMREAD_GRAYSCALE)f2 = cv2.imread("house02.tif", cv2.IMREAD_GRAYSCALE)f3 = cv2.imread("lena_gray_512.tif",cv2.IMREAD_GRAYSCALE)f4 = cv2.imread("lunar_surface.tif", cv2.IMREAD_GRAYSCALE)f5 = cv2.imread("Characters_test_pattern.tif", cv2.IMREAD_GRAYSCALE)f11 = pad(Q3.normalization(f1))F11 = Q3.dft2D(f11)F11 = Q3.normalization(np.abs(F11))f12 = center(f11)F12 = Q3.dft2D(f12)F13 = np.log(1 + np.abs(F12))cv2.imshow('orign_house', cv2.resize(Q3.normalization(f11), (f11.shape[0]//2,f11.shape[1]//2))	)cv2.imshow('F1_house',cv2.resize(Q3.normalization(F11) , (F11.shape[0]//2,F11.shape[1]//2)))cv2.imshow('F2_house', cv2.resize(Q3.normalization(np.abs(F12)), (F12.shape[0]//2,F12.shape[1]//2)))cv2.imshow('F3_house',cv2.resize( Q3.normalization(F13), (F13.shape[0]//2,F13.shape[1]//2)))f21 = pad(Q3.normalization(f2))F21 = Q3.dft2D(f21)F21 = Q3.normalization(np.abs(F21))f22 = center(f21)F22 = Q3.dft2D(f22)F23 = np.log(1 + np.abs(F22))cv2.imshow('orign_house02', cv2.resize(Q3.normalization(f21), (f21.shape[0]//2,f21.shape[1]//2))	)cv2.imshow('F1_house02',cv2.resize(Q3.normalization(F21) , (F21.shape[0]//2,F21.shape[1]//2)))cv2.imshow('F2_house02',cv2.resize(Q3.normalization(np.abs(F22)), (F22.shape[0]//2,F22.shape[1]//2)))cv2.imshow('F3_house02',cv2.resize( Q3.normalization(F23), (F23.shape[0]//2,F23.shape[1]//2)))f31 = pad(Q3.normalization(f3))F31 = Q3.dft2D(f31)F31 = Q3.normalization(np.abs(F31))f32 = center(f31)F32 = Q3.dft2D(f32)F33 = np.log(1 + np.abs(F32))cv2.imshow('orign_lena_gray_512',cv2.resize(Q3.normalization(f31), (f31.shape[0]//2,f31.shape[1]//2))	)cv2.imshow('F1_lena_gray_512',  cv2.resize(Q3.normalization(F31) , (F31.shape[0]//2,F31.shape[1]//2)))cv2.imshow('F2_lena_gray_512',  cv2.resize(Q3.normalization(np.abs(F32)), (F32.shape[0]//2,F32.shape[1]//2)))cv2.imshow('F3_lena_gray_512', cv2.resize( Q3.normalization(F33), (F33.shape[0]//2,F33.shape[1]//2)))f41 = pad(Q3.normalization(f4))F41 = Q3.dft2D(f41)F41 = Q3.normalization(np.abs(F41))f42 = center(f41)F42 = Q3.dft2D(f42)F43 = np.log(1 + np.abs(F42))cv2.imshow('orign_lunar_surface', cv2.resize(Q3.normalization(f41), (f41.shape[1]//2,f41.shape[0]//2))	)cv2.imshow('F1_lunar_surface', cv2.resize(Q3.normalization(F41) , (F41.shape[1]//2,F41.shape[0]//2)))cv2.imshow('F2_lunar_surface', cv2.resize(Q3.normalization(np.abs(F42)), (F42.shape[1]//2,F42.shape[0]//2)))cv2.imshow('F3_lunar_surface',cv2.resize( Q3.normalization(F43), (F43.shape[1]//2,F43.shape[0]//2)))f51 = pad(Q3.normalization(f5))F51 = Q3.dft2D(f51)F51 = Q3.normalization(np.abs(F51))f52 = center(f51)F52 = Q3.dft2D(f52)F53 = np.log(1 + np.abs(F52))cv2.imshow('orign_Characters_test_pattern', cv2.resize(Q3.normalization(f51), (f51.shape[1]//2,f51.shape[0]//2))	)cv2.imshow('F1_Characters_test_pattern', cv2.resize(Q3.normalization(F51) , (F51.shape[1]//2,F51.shape[0]//2)))cv2.imshow('F2_Characters_test_pattern', cv2.resize(Q3.normalization(np.abs(F52)), (F52.shape[1]//2,F52.shape[0]//2)))cv2.imshow('F3_Characters_test_pattern',cv2.resize( Q3.normalization(F53), (F53.shape[1]//2,F53.shape[0]//2)))cv2.waitKey(0)cv2.destroyAllWindows()

结果展示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/191787.html

相关文章

RISCV的浮点计算单元FPU

1.机器特征寄存器CSR_MISA 通过该寄存器可以查询当前处理器是否支持单精度浮点计算&#xff0c;是否支持双精度浮点计算&#xff0c;只要支持其中之一&#xff0c;就表示当前处理器支持浮点计算。 csrr t0, CSR_MISAandi t0, t0, &#xff08;COMPAT_HWCAP_ISA_F | COMPAT_HWCA…

CF713div3 F

Problem - F - Codeforces 题意&#xff1a; 小明想买一个价值为c的东西&#xff0c;他开始打工&#xff0c;他一开始在第i个职务&#xff0c;每天可以挣a[i]&#xff0c;他也可以用一天的时间并且花费b[i]升到i1职务&#xff0c;第i1职务每天可以挣a[i1] 一共有n个职务&#x…

BAM注意力机制——pytorch实现

论文传送门&#xff1a;BAM: Bottleneck Attention Module BAM的目的&#xff1a; 为网络添加注意力机制。 BAM的结构&#xff1a; ①通道注意力机制(Channel attention branch)&#xff1a;与SEblock相似&#xff1b; ②空间注意力机制(Spatial attention branch)&#xf…

主板EC程序烧写异常致无法点亮修复经验

主板型号&#xff1a;Gigabyte AB350M-Gaming3 官网上明确写着支持R5 5500&#xff0c;但按照如下步骤实践下来实际是不支持的 升级biosF31到F40版本的注意事项&#xff1a; 步骤&#xff1a; 1 使用Q-Flash先将bios升级到f31版本&#xff1b;2 然后下载提示中的ECFW Update To…

基于多向梯度局部对比度的红外弱小目标检测算法(MDWCM)解析及代码(2021年IEEE GRSL 论文)

该论文是< IEEE GEOSCIENCE AND REMOTE SENSING LETTERS >期刊2020年录取的一篇较新的一篇论文《Robust Infrared Small Target Detection via Multidirectional Derivative-Based Weighted Contrast Measure》&#xff0c;目前网上下载地址为&#xff1a;https://www.re…

分享个SM2259XT Intel N18混贴3CH开卡经验

收了条Intel的512G不认盘的ssd&#xff0c;拆出来两颗29F02T2AMCQH1&#xff0c;这个应该是正品&#xff0c;ID也没问题。然后&#xff0c;还有个山寨的256G SATA&#xff0c;主控2259XT&#xff0c;两个颗粒丝印29F1TB2ALCTH2&#xff0c;但是&#xff0c;ID与CQH1一样&#x…

数据可视化 | Python绘制多维柱状图:一图展示西部各省人口变迁【附本文数据和代码】

查看原文&#xff1a;【数据seminar】https://mp.weixin.qq.com/s/pvx2ZzLbrBL-6cyOwVATOA Part1前言 柱状图是利用柱子的高度来反映数据差异的统计图&#xff0c;与一维的柱状图相比&#xff0c;二维柱状图可以展示y轴上的变量含有两个指标的情况&#xff0c;如下图&#xff0…

Arduino ESP8266 浮点数精度问题:保留指定位数、精度丢失

最近币市熊的一塌糊涂&#xff0c;闲来无事&#xff0c;正好在玩Arduino ESP8266相关硬件&#xff0c;成功晋身“电子垃圾佬”。那么就想用 ESP8266一个LCD屏幕 自己做一个能够显示币行情的小玩意儿&#xff0c;解解闷。结果发现一个尴尬的问题&#xff0c;SHIB&#xff08;屎币…