【笔记】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();
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值