本文主要是介绍妙用FFT之逆变换生成脸谱,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原创:项道德(daode3056,daode1212)
快速傅立叶变换(FFT)有正向变换与反向变换,在极坐标系中,当对下图(左)正向变换之后,取不完全的比例进行逆变换,可生成以下右边的图案:
以下各图略去了原始曲线,直接出脸谱了:
//不完全FFT逆变换_花脸曲线(C#编程语言源代码):
private void button3_Click(object sender, EventArgs e)
{
/*
public static void FFT(double[] real, double[] imag, Accord.Math.FourierTransform.Direction direction)
Accord.Math.Transforms.FourierTransform2 的成员
*/
//画布,作图工具:
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
int xOrg = bmp.Width / 2; int yOrg = bmp.Height / 2;
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Black);
//画刷,画笔:
Brush bh = new SolidBrush(Color.FromArgb(220, 220, 220));
Brush bh2 = new SolidBrush(Color.FromArgb(220, 220, 0));
Brush bh3 = new SolidBrush(Color.FromArgb(250, 0, 250));
Pen pen = new Pen(Color.FromArgb(255, 255, 0), 4);
Pen pen2 = new Pen(Color.FromArgb(255, 0, 255), 1);
//样本数据:
double[] real = new double[501];// { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
double[] imag = new double[501];// { 1, 1.5, 3, 4, 5, 4.5, 5, 4, 3, 1.5, 1 };
Random RD = new Random();
double u1 = 0.5 * RD.NextDouble() + 0.5;
double u2 = 0.4 * RD.NextDouble() + 0.6;
double u3 = 0.3 * RD.NextDouble() + 0.7;
double u4 = 0.2 * RD.NextDouble() + 0.8;
for (int i = 0; i <= 500; i++)
{
float m = (float)((i - 250) / 25f);
real[i] = m;
//imag[i] = 40 * (8 * Math.Cos(m / u4 / 6) - 2 * Math.Cos(m / u3) + Math.Cos(2*m / u2 )) - 100;//OK
imag[i] = 40 * (8 * Math.Cos(m / u2 / 8) - 2 * Math.Cos(m / u3) + Math.Cos(2 * m / u4)) - 100; //OK
float q = (float)(PI * m / 10);
g.FillEllipse(bh3, (int)(imag[i]*Math.Sin(q) + xOrg*0.5), (int)(imag[i] * Math.Cos(q) + yOrg), 4, 4);//加圆点
}
//FFT正向变换,结果存入real1[],imag1[]:
double[] real1 = real;
double[] imag1 = imag;
Accord.Math.Transforms.FourierTransform2.FFT(real1, imag1, Accord.Math.FourierTransform.Direction.Forward);
//FFT逆向变换,结果存入real2[],imag2[]:
int NUM = (int)(real1.Length * (u1));//不完全截取,进行逆变换. 最大是: real1.Length;//
double[] real2 = new double[NUM];
double[] imag2 = new double[NUM];
for (int i = 0; i < NUM; i++)
{
real2[i] = real1[i];
imag2[i] = imag1[i];
}
Accord.Math.Transforms.FourierTransform2.FFT(real2, imag2, Accord.Math.FourierTransform.Direction.Backward);
//数据:
List<Point> LP = new List<Point>();
//绘制线段集与点集:
for (int i = 0; i < NUM; i++)
{
double x = real2[i];
double y = imag2[i];
g.FillEllipse(bh2, (int)(x - 2 + xOrg*1.5), (int)(y - 2 + yOrg-100), 4, 4);//加圆点
LP.Add(new Point((int)(x + xOrg*1.5), (int)(y + yOrg-100)));//+ yOrg
}
//显示数据:
g.FillPolygon(bh3, LP.ToArray());//填充多边形
g.DrawPolygon(pen, LP.ToArray());//封闭多边形
//string txt = string.Format("N3算法参数:{0:X}{1:X}{2:X}{3:X}", (int)(u1 * 0xFF), (int)(u2 * 0xFF), (int)(u3 * 0xFF), (int)(u4 * 0xFF));
//myGraphics.DrawString(txt, new Font("", 12), darkBlueBrush, boxWidth / 2 - 90, boxHeight - 25);
//------------------
//生成文本与保存图片: ===============================
string txt = sender.ToString().Split(':')[1] + string.Format("_{0:X}`{1:X}`{2:X}`{3:X}", (int)(100 * u1), (int)(100 * u2), (int)(100 * u3), (int)(100 * u4));
g.DrawString(txt, new Font("", 12), bh, boxWidth / 2 - 150, boxHeight - 25);
pictureBox1.Image = bmp;
bmp.Save(txt + ".png"); this.Text = txt + ".png ---文件已经保存";
this.pictureBox1.Refresh();
}
作者:daode3056[毕业于杭州师范大学数学系]
这篇关于妙用FFT之逆变换生成脸谱的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!