-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstant_artwork.cpp
More file actions
114 lines (91 loc) · 2.49 KB
/
instant_artwork.cpp
File metadata and controls
114 lines (91 loc) · 2.49 KB
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
#include <iostream>
// opencv stuff
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
/**
* Turns a picture into grayscale.
*/
Mat grayscale(Mat image) {
// new gray matrix
Mat gray;
// convert RGB image to gray
cvtColor(image, gray, CV_BGR2GRAY);
return gray;
}
/**
* Creates the selective sampling style
**/
Mat selective_sampling(Mat image, int row_div, int col_div) {
int SPLIT = 2;
Mat images[row_div][col_div];
// find pixel ratio
int x = image.rows / row_div;
int y = image.cols / col_div;
// just selects a random sample of pixels
for(int i = 0; i < col_div; i++) {
for(int j = 0; j < row_div; j++) {
images[j][i] = image(Range(0 + (x * j), x + (x * j)),
Range(0 + (y * i), y + (y * i)));
}
}
Mat result;
// Splits and puts together the resulting image
for(int i = 0; i < col_div; i += SPLIT) {
Mat interm_result;
// put together the column of images
for(int j = 0; j < row_div; j += SPLIT) {
interm_result.push_back(images[j][i]);
}
// concat the newly formed column
if(result.empty()) {
result = interm_result;
} else {
hconcat(result, interm_result, result);
}
}
return result;
}
/**
* Save the image to a file.
**/
void writeToFile(Mat image, string output_path) {
// compression parameters
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
// Write to file
try {
imwrite(output_path, image, compression_params);
}
catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
throw ex;
}
}
/**
* Entry point, reads the image and sends it to
* whichever function user picks.
*/
int main(int argc, char const *argv[])
{
Mat image;
// Error checking
if(argc < 2) {
cout << "Enter a filename." << std::endl;
return -1;
}
// Read image
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if(!image.data)
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
Mat result = selective_sampling(image, 60, 30);
// write the reesult to file
writeToFile(result, "result.png");
return 0;
}