使用DFT进行卷积
convolution using DFT
使用以下代码用指定的内核计算图像的卷积(在我的情况下,高斯)。每次我获得不同的结果,结果图像甚至都不接近我通过空间域中卷积获得的结果。首先,我认为问题是图像的数据类型。我将它们更改为32和64,但结果仍然相同。谁能告诉我可能出了什么问题?
http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#dft以上此功能给了我黑色图像。我在灰度中有输入。
void convol_fft(const Mat& A,const vector<vector<float>>& kernel2d,Mat& result)
{
Mat B = Mat(3,3,CV_64F);
for (int row = 0; row < kernel2d.size(); row++)
for (int col = 0; col < kernel2d[row].size(); col++){
B.at<uchar>(row,col) = (uchar)kernel2d[row][col];
}
int dft_M = getOptimalDFTSize( A.rows+B.rows-1 );
int dft_N = getOptimalDFTSize( A.cols+B.cols-1 );
Mat dft_A = Mat::zeros(dft_M, dft_N, CV_64F);
Mat dft_B = Mat::zeros(dft_M, dft_N, CV_64F);
Mat dft_A_part = dft_A(Rect(0, 0, A.cols,A.rows));
A.convertTo(dft_A_part, dft_A_part.type(), 1, -mean(A)[0]);
Mat dft_B_part = dft_B(Rect(0, 0, B.cols,B.rows));
B.convertTo(dft_B_part, dft_B_part.type(), 1, -mean(B)[0]);
dft(dft_A, dft_A, 0, A.rows);
dft(dft_B, dft_B, 0, B.rows);
// set the last parameter to false to compute convolution instead of correlation
mulSpectrums( dft_A, dft_B, dft_A, 0, false );
idft(dft_A, dft_A, DFT_SCALE, A.rows + B.rows - 1 );
result = dft_A(Rect(0, 0, A.cols + B.cols - 1, A.rows + B.rows - 1));
normalize(result, result, 0, 1, NORM_MINMAX, result.type());
pow(result, 3., result);
// B ^= Scalar::all(255);
}
基于OPENCV的phaseCorrelateRes()
的以下代码将在2个维度上进行相关性。
static void fftShift(InputOutputArray _out)
{
Mat out = _out.getMat();
if(out.rows == 1 && out.cols == 1)
{
// trivially shifted.
return;
}
vector<Mat> planes;
split(out, planes);
int xMid = out.cols >> 1;
int yMid = out.rows >> 1;
bool is_1d = xMid == 0 || yMid == 0;
if(is_1d)
{
xMid = xMid + yMid;
for(size_t i = 0; i < planes.size(); i++)
{
Mat tmp;
Mat half0(planes[i], Rect(0, 0, xMid, 1));
Mat half1(planes[i], Rect(xMid, 0, xMid, 1));
half0.copyTo(tmp);
half1.copyTo(half0);
tmp.copyTo(half1);
}
}
else
{
for(size_t i = 0; i < planes.size(); i++)
{
// perform quadrant swaps...
Mat tmp;
Mat q0(planes[i], Rect(0, 0, xMid, yMid));
Mat q1(planes[i], Rect(xMid, 0, xMid, yMid));
Mat q2(planes[i], Rect(0, yMid, xMid, yMid));
Mat q3(planes[i], Rect(xMid, yMid, xMid, yMid));
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
}
merge(planes, out);
}
void Correlate2d(
const cv::Mat& src1,
const cv::Mat& src2,
cv::Mat& dst,
double* response)
{
CV_Assert( src1.type() == src2.type());
CV_Assert( src1.type() == CV_32FC1 || src1.type() == CV_64FC1 );
CV_Assert( src1.size == src2.size);
int M = getOptimalDFTSize(src1.rows);
int N = getOptimalDFTSize(src1.cols);
Mat padded1, padded2, paddedWin;
if(M != src1.rows || N != src1.cols)
{
copyMakeBorder(src1, padded1, 0, M - src1.rows, 0, N - src1.cols, BORDER_CONSTANT, Scalar::all(0));
copyMakeBorder(src2, padded2, 0, M - src2.rows, 0, N - src2.cols, BORDER_CONSTANT, Scalar::all(0));
}
else
{
padded1 = src1;
padded2 = src2;
}
Mat FFT1, FFT2, P, Pm, C;
// correlation equation
// Reference: http://en.wikipedia.org/wiki/Phase_correlation
dft(padded1, FFT1, DFT_REAL_OUTPUT);
dft(padded2, FFT2, DFT_REAL_OUTPUT);
mulSpectrums(FFT1, FFT2, dst, 0, true);
idft(dst, dst, DFT_SCALE); // gives us the correlation result...
fftShift(dst); // shift the energy to the center of the frame.
// locate the highest peak
Point peakLoc;
minMaxLoc(dst, NULL, NULL, NULL, &peakLoc);
// max response is scaled
if( response )
*response = dst.at<float>(peakLoc);
}
您可以在 opencv sources 模块 imgproc src src phasecorr.cpp
中找到代码为了将代码更改为卷积,只需更改以下行:
mulSpectrums(FFT1, FFT2, dst, 0, true);
to
mulSpectrums(FFT1, FFT2, dst, 0, false);
这等同于在MATLAB中进行:
dst = fftshift(ifft2(fft2(src1).*conj(fft2(src2))))
我不确定opencv ...但这看起来可疑。
for (int row = 0; row < kernel2d.size(); row++)
for (int col = 0; col < kernel2d[row].size(); col++){
B.at<uchar>(row,col) = (uchar)kernel2d[row][col];
}
如果您要填充B内核,则该行应为kernel2d [col] .size()。看来您超越了B内核。kernel2d.size()的值是什么?
为什么不直接加载值?保存所有功能调用。
对于高斯内核,它应该看起来像{1,2,1,2,3,2,2,1,2,1}。
相关文章:
- 具有可分离内核的 2D 模糊卷积
- CUDA 使用共享内存平铺 3D 卷积实现
- OpneCV - 保留十进制的卷积
- C++中复向量与实向量的二维卷积
- 如何在张量流上使用 fp16(Eigen::half) 进行卷积
- 如何在卷积程序的 c++ 中优化嵌套循环
- 带有多通道内核的图像卷积
- 在 Caffe 中,如何修改特定卷积层的行为?
- 使用膨胀卷积的语义分割中的上采样
- 使用 OpenCL 进行图像卷积中的参数大小无效
- C++的全卷积网络训练
- 卷积二维与深度卷积二维计算
- 优化的图像卷积算法
- 我没有从使用 FFTW 的重叠添加 FFT 卷积中获得预期结果
- 卷积:卷积中的最后一个元素永远不会正确
- 如何在Caffe中对复发性卷积神经网络进行建模
- 如何在C 中而不是Prototext中启动卷积层
- 使用高斯模糊进行图像卷积,可以加速
- 自定义Caffe Windows CPP中的卷积层
- 使用DFT进行卷积