Mat fft2( Mat src)
{
Mat Fourier = new Mat();
int mat_type = (int)src.Depth;
if (mat_type>15)
{
Debug.Print("报错");
}
if (mat_type < 7)
{
VectorOfMat planes =new VectorOfMat( new Mat[]{ src, new Mat(src.Size, DepthType.Cv64F,1) });
CvInvoke.Merge(planes,Fourier);
CvInvoke.Dft(Fourier, Fourier, DxtType.Forward, Fourier.Rows);
}
else // 7<mat_type<15
{
Mat tmp=new Mat();
CvInvoke.Dft(src, tmp, DxtType.Forward, Fourier.Rows);
VectorOfMat planes=new VectorOfMat();
CvInvoke.Split(tmp, planes);
Image<Gray, Single> Ita = new Image<Gray, Single>(planes[0].Size);
Image<Gray, Single> ItC = new Image<Gray, Single>(planes[0].Size);
CvInvoke.CartToPolar(planes[0], planes[1], ItC, Ita,true); //将复数转化为幅值
Fourier = planes[0];
}
return Fourier;
}
Matrix<double> fftshift(Mat src)
{
Size sz = src.Size;
Point pt=new Point(0, 0);
pt.X = (int)Math.Floor(sz.Width / 2.0);
pt.Y = (int)Math.Floor(sz.Height / 2.0);
Mat mat= circshift(src, pt);
Matrix<double> xx = new Matrix<double>(mat.Size);
mat.CopyTo(xx);
mat.Dispose();
return xx;
}
Mat circshift(Mat src, Point delta)
{
Size sz = src.Size;
// 此种情况不需要移动
if ((sz.Height == 1 && sz.Width == 1) || (delta.X == 0 && delta.Y == 0))
return src;
// 需要移动参数的变换,这样就能输入各种整数了
int x = delta.X;
int y = delta.Y;
if (x > 0) x = x % sz.Width;
if (y > 0) y = y % sz.Height;
if (x < 0) x = x % sz.Width + sz.Width;
if (y < 0) y = y % sz.Height + sz.Height;
// 多维的Mat也能移动
VectorOfMat planes=new VectorOfMat();
CvInvoke.Split(src, planes);
for (int i = 0; i < planes.Size; i++)
{
// 竖直方向移动
Mat tmp0=new Mat(), tmp1 = new Mat(), tmp2 = new Mat(), tmp3 = new Mat();
Mat q0 = new Mat(planes[i], new Rectangle(0, 0, sz.Width, sz.Height - y));
Mat q1 = new Mat(planes[i], new Rectangle(0, sz.Height - y, sz.Width, y));
q0.CopyTo(tmp0);
q1.CopyTo(tmp1);
tmp0.CopyTo(new Mat( planes[i],new Rectangle(0, y, sz.Width, sz.Height - y)));
tmp1.CopyTo(new Mat(planes[i],new Rectangle(0, 0, sz.Width, y)));
// 水平方向移动
Mat q2 = new Mat(planes[i], new Rectangle(0, 0, sz.Width - x, sz.Height));
Mat q3 = new Mat(planes[i], new Rectangle(sz.Width - x, 0, x, sz.Height));
q2.CopyTo(tmp2);
q3.CopyTo(tmp3);
tmp2.CopyTo(new Mat( planes[i],new Rectangle(x, 0, sz.Width - x, sz.Height)));
tmp3.CopyTo(new Mat( planes[i],new Rectangle(0, 0, x, sz.Height)));
}
CvInvoke.Merge(planes, src);
return src;
}
最后
以上就是犹豫啤酒最近收集整理的关于C#实现Matlab中的fft2函数和fftshift函数的全部内容,更多相关C#实现Matlab中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复