OpenCV绘制朱利亚(Julia)集合图形

来源:互联网 发布:孤岛惊魂原始杀戮 优化 编辑:程序博客网 时间:2024/05/07 11:22

朱利亚集合是一个在复平面上形成分形的点的集合。以法国数学家加斯顿·朱利亚(Gaston Julia)的名字命名。

朱利亚集合可以由下式进行反复迭代得到:



对于固定的复数c,取某一z值(如z = z0),可以得到序列 



这一序列可能反散于无穷大或始终处于某一范围之内并收敛于某一值。我们将使其不扩散的z值的集合称为朱利亚集合。


以下使用OpenCV编码绘制Julia集图形:

#include <Windows.h>#include<highgui/highgui.hpp>using namespace cv;const int icount = 200;     //迭代次数const float c = -0.85;       //实部const float d = 0.088;      //虚部double m_real, m_image;     //Mandelbro集class ComplexClass{public:double real;double image;ComplexClass(double r = 0, double i = 0) { real = r, image = i; }};ComplexClass operator+(const ComplexClass& a, const ComplexClass &b){ComplexClass c;c.real = a.real + b.real;c.image = a.image + b.image;return c;}ComplexClass operator*(const ComplexClass& a, const ComplexClass &b){ComplexClass c;c.real = a.real * b.real - a.image * b.image;c.image = a.image * b.real + a.real * b.image;return c;}double Model(ComplexClass a){return sqrtf(a.real * a.real + a.image * a.image);}double Iteration(ComplexClass a, int n){if (n == 0)return Model(a);else{ComplexClass temp = a*a;temp.real += c;temp.image += d;//    temp.real += m_real;  把这两句代替前面的两句就是mandelbrot集了//    temp.image += m_image;return Iteration(temp, n - 1);}}Vec3b dye(double dist){if (dist < 1000000 && dist>-1000000)return Vec3b(255, 0, 0);elsereturn Vec3b(0, 0, 0);   //Julia集之外的区域置为黑色}int main(){Mat image = Mat(Size(500, 500), CV_8UC3, Scalar::all(10));for (int Y = 0; Y < image.rows; Y++){for (int X = 0; X < image.cols; X++){float x = (X - image.cols / 2) / 200.0;float y = (Y - image.rows / 2) / 200.0;m_real = x;m_image = y;ComplexClass a(x, y);float dist = Iteration(a, icount);image.at<Vec3b>(Y, X) = dye(dist);}}//namedWindow("OpenCV For Julia", 0);imshow("OpenCV For Julia | c = -0.85  d = 0.088", image);waitKey();}

改变实部c和虚数b的值可以得到不同的图形,很漂亮。

c=-0.576   d=0.456:



c=-0.8 d=0.156:



c=0.285 d=0.02:



c=-0.85 d=0.088:


0 0
原创粉丝点击