以下是查找64 * 64图像的DFT的代码。这是一个3通道图像,所以我从中提取绿色通道并进行处理。我认为该程序没有提供所需的输出。请检查一下,让我知道我做对还是错?
这是我正在使用的图像
#include "stdafx.h"
#include "opencv\highgui.h"
#include "opencv\cv.h"
using namespace cv;
#define _USE_MATH_DEFINES
#include<math.h>
#include<iostream>
#include<complex>
int main()
{
Mat im = imread("joker.jpg");
Mat im1(64, 64, CV_8UC1);
Mat im2(64, 64, CV_64FC1);
int from_to[] = {1,0};
mixChannels(&im, 1, &im1, 1, from_to, 1);
for (int u = 0; u < 64; u++)
{
for (int v = 0; v <64; v++)
{
double sum1 = 0, sum2 = 0;
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 64; y++)
{
auto c = std::complex<double>(cos(2 * M_PI*(u*x / 64.0 + v*y / 64.0)), -sin(2 * M_PI*(u*x / 64.0 + v*y / 64.0)));
double temp2 = std::real(c);
double temp3 = std::imag(c);
sum1 = sum1 + im1.at<uchar>(x,y) * temp2;
sum2 = sum2 + im1.at<uchar>(x,y) * temp3;
}
}
/* Finding the Frquency spectrum */
double t = round(sqrt(pow(sum1, 2) + pow(sum2, 2)));
im2.at<double>(u, v) = t;
}
}
double min, max;
minMaxIdx(im2, &min, &max);
/*Scaling to a range between 0 - 255 */
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
im2.at<double>(i, j) = 255 / (max - min)*(im2.at<double>(i, j)-min);
}
}
namedWindow("output2");
imshow("output2", im2);
waitkey(0);
}
答案 0 :(得分:1)
用(总不科学的)替换你的最终转换代码:
Mat im3;
im2.convertTo(im3, CV_8U, .04);
imshow("output2", im3);
给出:
另外,因为它是灰度img,你可以简化输入:
Mat im1 = imread("joker.jpg", 0); // read as 1chan grayscale
并跳过mixChannels部分
为了它的乐趣,让我们试试opencv的内置dct;)
Mat im = imread("joker.jpg", 0);
im.convertTo(im1, CV_64F, 1.0/255); // [0..1]
Mat im2;
dct(im1,im2);
Mat im3;
im2.convertTo(im3, CV_8U, 255); //[0..255]
imshow("output2", im3);