#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include<fstream>
using namespace std;
using namespace cv;
int main()
{
    vector<float> src;
    string path = "E:\\实习入门\\原跳动\\1.txt";
    ifstream infile;
    infile.open(path, ios::in);
    if (!infile)
    {
        cout << "读取文件失败" << endl;
    }
    float i = 0;

    while (!infile.eof())
    {

        infile >> i;
        
        src.push_back(i);
        if (infile.fail())
            break;
    }
    infile.close();
    int c = src.size();
    vector<float>padde(src);
    int r = getOptimalDFTSize(src.size());
    int w = r - c;
    for (int i = 0; i < w; i++)
    {
        padde.push_back(0);
    }
    //src信号的实部、虚部
    Mat_<float> src_real = Mat_<float>(padde);
    Mat_<float> src_img = Mat::zeros(r, 1, CV_32F);
    vector<Mat_<float>> data;
    data.push_back(src_img);
    data.push_back(src_real);
    Mat complexImg;
    merge(data, complexImg); //可以理解为组合成2通道(实部+虚部)图像
    dft(complexImg, complexImg); //DFT变换后的数据复制到原处,没有另外开辟内存, complexImg是个复数矩阵
    int cy = complexImg.rows / 2;
    Mat m1(complexImg, cv::Rect(0, 0, 1, cy)); //左上部分
    Mat m2(complexImg, cv::Rect(0,cy, 1, cy)); //右上部分
    Mat temp;
    m1.copyTo(temp);
    m2.copyTo(m1);
    temp.copyTo(m2);

    split(complexImg, data);
    Mat src_dft_nor;
    magnitude(data[0], data[1], src_dft_nor);
    normalize(src_dft_nor, src_dft_nor, 1, 0, CV_MINMAX);
    //除去高频;
    //int data_temp_loat;
    //Mat src_nor_t=src_dft_nor.t();//转置
    //for (int i = 0; i < src_nor_t.cols; i++)
    //{
    //    float *data_ = src_nor_t.ptr<float>(0);
    //    if (data_[i]==1 )
    //    {
    //        data_temp_loat =i;
    //    }
    //}
    //Mat src_low=src_nor_t.t();
    /*Mat temp;
    m1.copyTo(temp);
    m2.copyTo(m1);
    temp.copyTo(m2);*/
     int y= complexImg.rows ;

    Mat partFrequencyImg_t = (complexImg.clone()).t();

    
    int colNumber = partFrequencyImg_t.cols*partFrequencyImg_t.channels();  //列数 x 通道数=每一行元素的个数
    int data_locate= partFrequencyImg_t.cols;
    //双重循环,遍历所有的像素值
    float * partFrequencyImg_t_data = partFrequencyImg_t.ptr<float>(0);  //获取第i行的首地址
    for (int i = 0; i < colNumber; i++)
    {
        if (i == (data_locate-3) || i == data_locate +3)
            ;
        else
        {
            partFrequencyImg_t_data[i] = 0;
        }
    }
    Mat partFrequencyImg= partFrequencyImg_t.t();
    //partFrequencyImg.colRange(0, 1).setTo(Scalar::all(0));
    /*partFrequencyImg.rowRange(0, y/2).setTo(Scalar::all(0));
    partFrequencyImg.rowRange(y/2+1,y).setTo(Scalar::all(0));*/
    Mat iPartDft[] = { Mat::zeros(complexImg.size(),CV_32F),Mat::zeros(complexImg.size(),CV_32F) };
    idft(partFrequencyImg, partFrequencyImg);

    split(partFrequencyImg, iPartDft);
    Mat data_temp;
    magnitude(iPartDft[0], iPartDft[1], data_temp);
    Mat data_img;
    cv::normalize(data_temp, data_img, 1, 0, CV_MINMAX);
    ofstream outfile;
    string path_out = "E:\\C++\\other_test\\file_out.csv";
    outfile.open(path_out, ios::out);
    for (int i = 0; i < data_temp.rows; i++)
    {
        outfile<<data_temp.at<float>(i)<<endl;
    }
    outfile.close();

    
    
    
}