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

朱利亚集合是一个在复平面上形成分形的点的集合。以法国数学家加斯顿·朱利亚(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);
	else
		return 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:


  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值