-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
58 lines (48 loc) · 1.49 KB
/
main.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
// main.cpp : Defines the entry point for the application.
//
#include "main.h"
using namespace std;
int main()
{
// Create random generator
random_device rd;
mt19937 gen(rd());
// Load image
cout << "image: ";
string imagename;
cin >> imagename;
Image source = Image(imagename);
source.display();
u32 width = source.getPixels()[0].size();
u32 height = source.getPixels().size();
// Create markov chain from image
MarkovChain chain = MarkovChain(source.getPixels());
Image result = Image(width, height);
// Count number of same pixels to prevent infinite loops
u64 samePixelsCount = 0;
// Set up creation loop
cv::Vec3b previous = NULL;
cv::Vec3b start = source.getPixels()[0][0];
cout << "Starting pixel: " << start << endl;
for (u64 i = 0; i < height; i++) {
for (u64 j = 0; j < width; j++) {
result.setPixel(j, i, start[0], start[1], start[2]);
// Uses \n so the application doesnt have to wait for the console to catch up
cout << "Set pixel at: " << j << ", " << i << " to: " << start << "\n";
previous = start;
start = chain.getNextPixel(start);
if (previous == start) {
++samePixelsCount;
}
// If pixels are the same in a row 10 times pick a random pixel from the source
if (samePixelsCount > 10) {
start = source.getPixels()[gen() % height][gen() % width];
samePixelsCount = 0;
cout << "Loop detected, using " << start << " instead" << "\n";
}
cout << "Got next pixel: " << start << "\n";
}
}
result.display();
return 0;
}