opencv shift
时间: 2023-07-07 18:46:30 浏览: 43
在OpenCV中,`shift`函数用于将频谱图像进行中心化,以便进行傅里叶逆变换。该函数的定义如下:
```c++
void shift(cv::InputArray src, cv::OutputArray dst, const cv::Point& delta, int borderType = cv::BORDER_CONSTANT, const cv::Scalar& borderValue = cv::Scalar());
```
其中,`src`表示输入的频谱图像,`dst`表示输出的中心化后的频谱图像,`delta`表示中心化的偏移量,`borderType`表示边界处理方式,`borderValue`表示边界的颜色值。
具体来说,中心化的偏移量`delta`可以通过以下公式计算:
```c++
delta = Point(src.cols/2, src.rows/2)
```
其中,`src.cols`和`src.rows`分别表示频谱图像的列数和行数。这个公式的意义是将频谱图像的中心移动到图像的左上角。
下面是一个示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("input.jpg");
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
Mat f;
dft(Mat_<float>(gray), f);
shift(f, f, cv::Point(-f.cols/2, -f.rows/2));
shift(f, f, cv::Point(f.cols/2, f.rows/2));
Mat ift;
idft(f, ift);
Mat output;
normalize(ift, output, 0, 255, NORM_MINMAX, CV_8U);
imshow("input", img);
imshow("output", output);
waitKey(0);
return 0;
}
```
在这个示例代码中,我们首先读取了一张名为"input.jpg"的图像,并将其转换为灰度图像。然后,使用`dft`函数进行傅里叶变换,并使用`shift`函数将变换结果进行中心化。接着,使用`shift`函数将中心化后的频谱图像移回到图像的中心位置。然后,使用`idft`函数进行傅里叶逆变换,得到原始图像,并使用`normalize`函数将图像像素值归一化到0~255之间。最后使用`imshow`函数将原始图像和傅里叶逆变换后的图像显示出来。