-
Notifications
You must be signed in to change notification settings - Fork 3
/
Impressionist.cpp
152 lines (126 loc) · 3.09 KB
/
Impressionist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// impressionist.cpp
//
// The main driver program for the other parts. We have two major components,
// UI and Doc.
// They do have a link to each other as their member such that they can
// communicate.
//
#include <stdio.h>
#include <FL/Fl.h>
#include <FL/Fl_Window.H>
#include "Impressionist.h"
#include "impressionistUI.h"
#include "impressionistDoc.h"
#include <functional>
#include "Bayesian.h"
#include "node.h"
#include <Eigen/Dense>
#include <iostream>
#include "Cluster.h"
#include "PowerIter.h"
using namespace Eigen;
ImpressionistUI *impUI;
ImpressionistDoc *impDoc;
Bayesian* imp_bayesian;
std::stringstream ss;
int main(int argc,
char** argv)
{
impDoc = new ImpressionistDoc();
// Create the UI
impUI = new ImpressionistUI();
imp_bayesian = new Bayesian(impDoc, impUI);
impDoc->m_bayesian = imp_bayesian;
// Set the impDoc which is used as the bridge between UI and brushes
impUI->setDocument(impDoc);
impDoc->setUI(impUI);
Fl::visual(FL_DOUBLE|FL_INDEX);
impUI->show();
// impDoc->loadImage("bayesian/gandalf.bmp");
// imp_bayesian->solve("bayesian/gandalfTrimap.bmp");
// OutputDebugString(ss.str().c_str());
return Fl::run();
}
// global functions
float frand()
{
return (float)rand()/RAND_MAX;
}
int irand(int max)
{
return rand()%max;
}
double degToRad(double deg)
{
return deg * 2 * M_PI / 360.0f;
}
double radToDeg(double rad)
{
return rad / 2.f / M_PI * 360.0f;
}
double randAlter(double original, double maxAbsPercentageDiff)
{
return original + original*maxAbsPercentageDiff*(frand()-0.5)*2;
}
Point CalGradient(const Point source, const Point target, const std::function<GLubyte*(int, int)> getPixel)
{
const int sobel[][3] = {
{ 1, 0, -1 },
{ 2, 0, -2 },
{ 1, 0, -1 }
};
int gx = 0, gy = 0;
// const int img[3][3] = {};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
const int x = source.x - 1 + i;
const int y = source.y - 1 + j;
GLubyte* p = getPixel(x, y);
const int pix = p[0]+p[1]+p[2];
gx += sobel[j][i] * pix;
gy += sobel[i][j] * pix;
}
}
return Point(gx, gy);
}
std::vector<std::vector<float>> getGaussianKernel(float sigma, int size)
{
std::vector<std::vector<float>> k;
for (int i = 0; i < size; i++) {
std::vector<float> row;
for (int j = 0; j < size; j++) {
int x = i - size / 2;
int y = j - size / 2;
float v = (1 / (2 * M_PI*sigma)) * exp( -(x * x + y * y) / (2 * sigma));
row.push_back(v);
}
k.push_back(row);
}
return k;
}
GLubyte* getColor(unsigned char* p, int xx, int yy, int w, int h){
xx = max(0, min(xx, w - 1));
yy = max(0, min(yy, h - 1));
return p + (yy*w + xx) * 3;
};
float colorDist(unsigned char* p0, unsigned char* p1) {
return sqrt(pow(p0[0] - p1[0], 2) + pow(p0[1] - p1[1], 2) + pow(p0[2] - p1[2], 2));
};
void getMeanColor(GLubyte* buf, unsigned char* p, int xx, int yy, int w, int h, float sigma, int size)
{
std::vector<std::vector<float>> kernel= getGaussianKernel(sigma, size);
memset(buf, 0, 3);
for (int a = 0; a<size; a++)
{
for (int b = 0; b<size; b++)
{
for (int d = 0; d<3; d++)
{
buf[d] += kernel[a][b] * getColor(p,xx,yy,w,h)[d];
}
}
}
}