OpenCV——图像傅里叶变换

本文介绍了OpenCV中与傅里叶变换相关的函数,包括dft()、magnitude()、phase()、getOptimalDFTSize()、copyMakeBorder()和normalize()。详细阐述了OpenCV进行傅里叶变换的步骤,从图像预处理到显示结果,并提供了关键代码片段。建议对傅里叶变换不熟悉的读者参考作者的完整项目代码以加深理解。
摘要由CSDN通过智能技术生成

1、OpenCV傅里叶变换相关函数

首先我要说明的是,在使用OpenCV写代码做图像傅里叶变换的时候,并仅仅是调用dft函数做一个傅里叶变换这么简单的,而是先要对图像进行一些变换之后,才能得到正确的傅里叶变换结果。因此,第一部分我想先列出几个OpenCV提供的与傅里叶变换相关的函数,在了解这些函数功能的基础上,我们再进行具体的图像傅里叶变换的过程。

1.1 dft()

首先,OpenCV提供的傅里叶变换函数dft。其定义如下:

void dft(InputArray src, OutputArray, dst, int flags, int nonzeroRows);
/**
  * 参数解释:
  * src : 输入图像
  * dst : 输出图像,傅里叶变换结果。默认情况下返回值有两个通道,第一个通达是实部,第二个通道是虚部。
  * flags : 转换的标识符,有默认值0,暂时不用理会这个参数
  */
1.2 magnitude()

计算二维矢量的幅值。其定义如下:

void magnitude(InputArray x, InputArray y, OutputArray magnitude);
/**
  * 参数解释:
  * x : 实部
  * y : 虚部
  * magnitude : 幅值结果
  */

其计算公式如下:
d s t ( I ) = x ( I ) 2 + y ( I ) 2 dst(I) = \sqrt{x(I)^2 + y(I)^2} dst(I)=x(I)2+y(I)2

1.3 phase()
void phase(InputArray x, InputArray y, OutputArray dst, bool angleIndegrees=false);
/**
  * 参数解释:
  * x : 实部
  * y : 虚部
  * dst : x和y的反正切值结果
  */

其计算公式如下:
d s t = arctan ⁡ y x dst = \arctan \frac{y}{x} dst=arctanx

以下是实现该功能的 MATLAB 代码: ```matlab % 调入并显示灰度图像 Lenna.jpg gray_img = imread('Lenna.jpg'); imshow(gray_img); % 转换为 RGB 图像 gray_img = mat2gray(gray_img); red_img = gray_img; green_img = gray_img; blue_img = gray_img; % 将灰度图像变换为 RGB 图像 red_img = red_img - sin(2 * pi * red_img); green_img = green_img - cos(2 * pi * green_img); blue_img = blue_img + sin(2 * pi * blue_img); rgb_img = cat(3, red_img, green_img, blue_img); % 显示变换曲线及变换合成的彩色图像 subplot(2, 2, 1); plot(red_img, 'r'); title('Red Component'); subplot(2, 2, 2); plot(green_img, 'g'); title('Green Component'); subplot(2, 2, 3); plot(blue_img, 'b'); title('Blue Component'); subplot(2, 2, 4); imshow(rgb_img); title('RGB Image'); % 将 RGB 的变换公式至少互换一次 % 例如将 R 与 G 互换 temp = red_img; red_img = green_img + cos(2 * pi * red_img); green_img = temp - sin(2 * pi * green_img); rgb_img2 = cat(3, red_img, green_img, blue_img); % 显示变换曲线及变换结果 figure; subplot(2, 2, 1); plot(red_img, 'r'); title('Red Component'); subplot(2, 2, 2); plot(green_img, 'g'); title('Green Component'); subplot(2, 2, 3); plot(blue_img, 'b'); title('Blue Component'); subplot(2, 2, 4); imshow(rgb_img2); title('RGB Image 2'); ``` 运行代码后,会依次显示灰度图像、变换曲线及变换合成的彩色图像,以及互换 R 和 G 后的变换曲线及变换结果。可以观察到变换后的彩色图像与原始灰度图像的色调有很大的变化,而且互换 R 和 G 后的变换结果与第一次变换的结果也有很大的不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值