#include <opencv/cv.hpp>
#include <opencv/highgui.h>
#include <cmath>
#include <complex>
#include <vector>
#include <string>
#include <omp.h>
using namespace std;
using namespace cv;
using myPoint=complex<double>;
class MandelbrotSet {
public:
MandelbrotSet(int _count, double _ex_limit, int _wide, int _height, char *_path) {
WIDE = _wide * 4, HEIGHT = _height * 4;
ORIG_X = WIDE / 2;
ORIG_Y = HEIGHT / 2;
EXLIMIT = _ex_limit;
COUNT = _count;
PATH = _path;
}
double solve() {
double start = omp_get_wtime();
img = Mat(HEIGHT, WIDE, CV_8UC3);
vector<pair<int, int> > g;
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDE; j++)
g.emplace_back(i, j);
#pragma omp parallel for num_threads(4) schedule(dynamic)
for (int k = 0; k < g.size(); k++) {
int &i = g[k].first;
int &j = g[k].second;
if (Iteration(i, j)) {
img.ptr<uchar>(i)[j * 3] = 0;
img.ptr<uchar>(i)[j * 3 + 1] = (double) abs(j - ORIG_X) / WIDE * 255;
img.ptr<uchar>(i)[j * 3 + 2] = (double) abs(i - ORIG_Y) / HEIGHT * 255;
} else {
img.ptr<uchar>(i)[j * 3] = 0;
img.ptr<uchar>(i)[j * 3 + 1] = 255 - (double) abs(j - ORIG_X) / WIDE * 255;
img.ptr<uchar>(i)[j * 3 + 2] = 255 - (double) abs(i - ORIG_Y) / HEIGHT * 255;
}
}
double dur = omp_get_wtime() - start;
if (PATH == NULL) {
cvNamedWindow("分形");
setMouseCallback("分形", &MandelbrotSet::on_mouse, this);
imshow("分形", img);
for (int i = 0; i < 2; i++)
waitKey();
} else if (!imwrite(PATH, img))
puts("Fail to Save the Image!");
return dur;
}
private:
int WIDE, HEIGHT, COUNT, ORIG_X, ORIG_Y;
double EXLIMIT;
char *PATH;
Mat img;
bool Iteration(int y, int x) {
myPoint c = transAxis(this, x, y);
myPoint z = myPoint(0, 0);
for (int i = 0; i <= COUNT; i++) {
z = z * z + c;
if (abs(z) > EXLIMIT)
return true;
}
return false;
}
static inline myPoint transAxis(MandelbrotSet *self, int x, int y) {
return {(x - self->ORIG_X) / (self->WIDE / 4.0), (self->ORIG_Y - y) / (self->HEIGHT / 4.0)}; //[-2,2]*[-2,2]
}
static void on_mouse(int event, int x, int y, int flags, void *userdata) {
auto *self = (MandelbrotSet *) userdata;
if (event == CV_EVENT_MOUSEMOVE) {
Mat t_img = self->img.clone();
Point pt = Point(x, y);
char temp[16];
Vec3b col = Vec3b(255, 255, 255) - t_img.at<Vec3b>(pt);
myPoint np = transAxis(self, x, y);
sprintf(temp, "(%.2lf,%.2lf)", real(np), imag(np));
putText(t_img, temp, Point(x, y), FONT_HERSHEY_DUPLEX, 0.3, col, 1);
imshow("分形", t_img);
}
}
};
int main(int argc, char *argv[]) {
int mp = 400, rs = 800, error_cnt = 0;
char *path = nullptr;
double er = 2.0;
for (int i = 1; i < argc;) {
if (strcmp(argv[i], "-mp") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%d", &mp) == 1) {
if (mp <= 0)
printf("Error %d:parameter of -mp should be positive!\n", ++error_cnt);
else if (mp > 1000)
printf("Error %d:parameter of -mp too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-mp needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-er") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%lf", &er) == 1) {
if (er <= 0)
printf("Error %d:parameter of -er should be positive!\n", ++error_cnt);
else if (er > 10)
printf("Error %d:parameter of -er too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-er needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-rs") == 0) {
if (i + 1 < argc && sscanf(argv[i + 1], "%d", &rs) == 1) {
if (rs <= 0)
printf("Error %d:parameter of -rs should be positive!\n", ++error_cnt);
else if (rs > 10000)
printf("Error %d:parameter of -rs too big!\n", ++error_cnt);
i += 2;
} else {
printf("Error %d:-rs needs a parameter.\n", ++error_cnt);
++i;
}
} else if (strcmp(argv[i], "-p") == 0) {
if (i + 1 < argc) {
path = argv[i + 1];
i += 2;
} else {
printf("Error %d:-er needs a parameter.\n", ++error_cnt);
++i;
}
} else {
printf("Error %d:Unrecognizable parameter name \"%s\"\n", ++error_cnt, argv[i]);
++i;
}
}
if (!error_cnt) {
MandelbrotSet M = MandelbrotSet(mp, er, rs, rs, path);
printf("Finished in %.10lf seconds\n", M.solve());
}
return 0;
}
OpenCV实现生成曼德博集合图像(待更新)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Java基于opencv实现图像数字识别(四)—图像降噪 我们每一步的工作都是基于前一步的,我们先把我们前面的几个...
- 本文作者:小嗷 微信公众号:aoxiaoji 吹比QQ群:736854977 简书链接:https://www.j...
- 1. 自定义滤波 OpenCV中除了之前说的几种常见的滤波方法外,还支持自定义卷积核,用于实现自定义滤波。 这一节...
- 版权声明:本文为博主原创文章,未经博主允许不得转载源码:AnliaLee/BauzMusic首发地址:Anlia_...