c语言实现opencv分形图像,opencv实现mandelbrot分形绘制

直接上程序了,本来从来不写博客的,但今天把自己的源代码copy了下来,就是因为知道以后一定会有工大的学弟学妹会看到,可能我要以一种学长的口吻说话了吧

a4c26d1e5885305701be709a3d33442f.png,这段程序里有几十行是从网上copy的,结果被查同系统发现了,3.5学分的课程,10%的成绩,心疼是真的,可是却很庆幸,35分的当头棒喝

英才学院的学弟学妹们,你们一定要做一个正直坦荡的学者啊。

再说多就招人烦了吧

a4c26d1e5885305701be709a3d33442f.png。。最后,此博文亦作自省。。还有啊,大作业有什么不懂的,可以给我发邮件啊

18800429158@163.com

学长还是很闲的  学妹优先啊

a4c26d1e5885305701be709a3d33442f.png

#include

#include

#include

#define IMG_8UB(img,x,y)

((uchar*)(img->imageData + img->widthStep * (y)))[3 *

(x)]

#define IMG_8UG(img,x,y)

((uchar*)(img->imageData + img->widthStep * (y)))[3 * (x) +

1]

#define IMG_8UR(img,x,y)

((uchar*)(img->imageData + img->widthStep * (y)))[3 * (x) +

2]

#define MAX_COLOR 256

IplImage* pImg;

IplImage* fcopy;

int width = 500;

int height = 500;

double XMax = 2.0;

double XMin = -2.0;

double YMax = 2.0;

double YMin = -2.0;

int B[MAX_COLOR];

int G[MAX_COLOR];

int R[MAX_COLOR];

CvPoint pt1 = {-1,-1};

CvPoint pt2 = {-1,-1};

CvPoint pt3 = {-1,-1};

int X = 250;

int Y = 250;

struct Complex

{

double real;

double img;

};

void initColor()

{

for(int i = 0;i <

MAX_COLOR;i++)

{

B[i] = i * 4 % 256;

G[i] = (int)(0.7 * 255.0);

R[i] = (int)(255.0 * (1.0 - i /

255.0 * i / 255.0 / 1.2));

}

}

void mySetTest()

{

double deltaX = (XMax - XMin) /

width;

double deltaY = (YMax - YMin) /

height;

int max_iterations = 200;

double max_size = 4.0;

for(int row = 0;row <

height;row++)

{

for(int col = 0;col <

width;col++)

{

int color = 0;

Complex c,z;

z.real = 0;

z.img = 0;

c.real  = XMin +

col * deltaX;

c.img = YMin + row * deltaY;

while((color < max_iterations)

&& ((z.img * z.img + z.real * z.real) < max_size))

{

double tmp = z.real * z.real - z.img

* z.img + c.real;

z.img = z.img * z.real + z.real *

z.img + c.img;

z.real = tmp;

color++;

}

if(color >= max_iterations)

color = 255;

color %= MAX_COLOR;

IMG_8UB(pImg,col,row) =

B[color];

IMG_8UG(pImg,col,row) =

G[color];

IMG_8UR(pImg,col,row) =

R[color];

}

}

cvCvtColor(pImg,pImg,CV_HSV2BGR);

cvShowImage("mandelbrot",pImg);

cvWaitKey(0);

}

void on_mouse( int event, int x, int

y, int flags, void *param  =

NULL)

{

if( !pImg)

return;

switch(event)

{

case CV_EVENT_LBUTTONDOWN:

{

pt1 = cvPoint(x,y);

break;

}

case CV_EVENT_LBUTTONUP :

{

pt2 = cvPoint(x,y);

int dx = abs(pt2.x - pt1.x);

int dy = abs(pt2.y - pt1.y);

if(pt1.x > 0 && pt1.y

> 0 && pt2.x > 0 && pt2.y > 0 &&

dx > 5 && dy > 5)

{

double DX = XMax - XMin;

double DY = YMax - YMin;

double offX = DX / width;

double offY = DY / height;

if(pt1.x < pt2.x)

{

XMax = offX * pt2.x + XMin;

XMin = offX * pt1.x + XMin;

}

else

{

XMax = offX * pt1.x + XMin;

XMin = offX * pt2.x + XMin;

}

if(pt1.y < pt2.y)

{

YMax = offY * pt2.y + YMin;

YMin = offY * pt1.y + YMin;

}

else

{

YMax = offY * pt1.y + YMin;

YMin = offY * pt2.y + YMin;

}

pt1 = cvPoint(-1,-1);

pt2 = cvPoint(-1,-1);

mySetTest();

}

break;

}

case CV_EVENT_MOUSEMOVE:

{

if((flags&CV_EVENT_FLAG_LBUTTON))

{

pt2 = cvPoint(x,y);

if(pt1.x > 0 && pt1.y

> 0

&& pt2.x > 0 &&

pt2.y > 0

&& abs(pt2.x - pt1.x) >

5

&& abs(pt2.y - pt1.y) >

5)

{

cvCopy(pImg,fcopy);

cvRectangle(fcopy,pt1,pt2,cvScalar(255,255,255));

cvShowImage( "mandelbrot",

fcopy);

}

}

break;

}

case CV_EVENT_RBUTTONDBLCLK:

case CV_EVENT_RBUTTONDOWN :

{

pt3 = cvPoint(x,y);

if(pt3.x > 0 && pt3.y

> 0 && abs(pt3.x - X) > 5 && abs(pt3.y - Y)

> 5)

{

double DX = XMax - XMin;

double DY = YMax - YMin;

double offX = DX / width;

double offY = DY / height;

DX = abs(pt3.x - X)*offX;

DY = abs(pt3.y - Y)*offY;

if(pt3.x > X)

{

XMax -= DX;

XMin -= DX;

}

if(pt3.x < X)

{

XMax += DX;

XMin += DX;

}

if(pt3.y < Y)

{

YMax += DY;

YMin += DY;

}

if(pt3.y > Y)

{

YMax -= DY;

YMin -= DY;

}

double m ,n;

m = offX * (pt3.x - X);

n = - offY * (pt3.y - Y);

X = pt3.x;

Y = pt3.y;

pt3 = cvPoint(-1,-1);

mySetTest();

}

break;

}

default:

break;

}

}

int main()

{

cvNamedWindow("mandelbrot",1);

cvSetMouseCallback( "mandelbrot",

on_mouse, 0 );

pImg =

cvCreateImage(cvSize(width,height),8,3);

fcopy =

cvCreateImage(cvSize(width,height),8,3);

initColor();

mySetTest();

cvReleaseImage(&pImg);

cvReleaseImage(&fcopy);

cvDestroyWindow("mandelbrot");

return 0;

}

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

学长青岛人,所以用当年高三青岛一模语文作文题结尾吧,曼德拉的一句话:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值