-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
185 lines (144 loc) · 5.01 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//////////////////////////////////////////////////////
//
// Grayscale square grid texture generator
//
// Author: Nick Stones-Havas
// Date: 29 Oct 2015
//
#include <cstdlib>
#include <vector>
#include <iostream>
#include <cstdint>
#include <opencv2/opencv.hpp>
#include "config.h"
using namespace std;
using namespace cv;
const int PAGE_WIDTH = (BORDER_SIZE * 2) + int(NUM_SQUARES_X * SQUARE_SIZE) + ((NUM_SQUARES_X + 1) * GAP_SIZE);
const int PAGE_HEIGHT = (BORDER_SIZE * 2) + int(NUM_SQUARES_Y * SQUARE_SIZE) + ((NUM_SQUARES_Y + 1) * GAP_SIZE);
#define NUM_INTENSITIES 5 // The total number of different intensities
// The 8-bit value of each intensity
const uint8_t intensities[] = {
8,
16,
32,
64,
128
};
// The probability intervals for each intensity (Spcified number is the upper bound of the interval. The lower bound is the previous upper-bound)
const float prob_intervals[] = {
0.05f,
0.1f,
0.3f,
0.7f,
1.0f
};
#define FILTER_KERNEL_WIDTH 5 // Width/height of the filter
float KERNEL[FILTER_KERNEL_WIDTH][FILTER_KERNEL_WIDTH] = { // The arbitrary blur kernel
{0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
{0.0f, 0.0f, 0.0f, 2.0f, 0.0f},
{0.0f, 0.0f, 8.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f, 0.0f}
};
// Find the sum of values in the kernel matrix
inline float sumMatrix(float matrix[FILTER_KERNEL_WIDTH][FILTER_KERNEL_WIDTH], int width, int height) {
float sum = 0;
for(int row = 0; row < width; row++) {
for(int col = 0; col < height; col++) {
sum += matrix[row][col];
}
}
return sum;
}
// Draw a sqaure to the page
void fillSquare(Mat& image, int x, int y, int size, uint8_t shade) {
for(int row = y; row < y+size; row++) {
for(int col = x; col < x+size; col++) {
image.ptr<uint8_t>(row)[col] = shade;
}
}
}
// Main routine
int run(int argc, char** argv) {
Mat page(Size(PAGE_WIDTH, PAGE_HEIGHT), CV_8UC1);
page = Scalar(255); // Set page to be initially white
srand(time(NULL));
// Generate squares
for(int x = 0; x < NUM_SQUARES_X; x++) {
for(int y = 0; y < NUM_SQUARES_Y; y++) {
int squarex = BORDER_SIZE + x * (GAP_SIZE + SQUARE_SIZE) + GAP_SIZE;
int squarey = BORDER_SIZE + y * (GAP_SIZE + SQUARE_SIZE) + GAP_SIZE;
float random = float(rand() % 101) / 100.0f;
int i;
for(i = 0; i < NUM_INTENSITIES; i++)
if(random <= prob_intervals[i]) break;
fillSquare(page, squarex, squarey, SQUARE_SIZE, intensities[i]);
}
}
// Apply noise
#ifdef APPLY_NOISE
int noiseRange = NOISE_START_RANGE;
float noiseProb = NOISE_START_PROB;
for(int i = 0; i < NOISE_ROUNDS; i++) {
for(int row = BORDER_SIZE; row < PAGE_HEIGHT - BORDER_SIZE; row++) {
for(int col = BORDER_SIZE; col < PAGE_WIDTH - BORDER_SIZE; col++) {
int shade = page.ptr<uint8_t>(row)[col];
float random = float(rand() % 101) / 100.0f;
if(random <= noiseProb) {
int noise = (rand() % noiseRange) - noiseRange/2;
if(noise == 0) noise = noiseRange/2;
shade += noise;
// Clamp shade if required
if(shade < 0 || shade > 255) shade -= 2 * noise;
page.ptr<uint8_t>(row)[col] = (char)shade;
}
}
}
noiseRange *= 2;
noiseProb /= 2.0f;
}
#endif
// Filter the page
#ifdef APPLY_FILTER
// Initialize kernel
float kernelSum = sumMatrix(KERNEL, FILTER_KERNEL_WIDTH, FILTER_KERNEL_WIDTH);
for(int i = 0; i < FILTER_KERNEL_WIDTH; i++)
for(int j = 0; j < FILTER_KERNEL_WIDTH; j++)
KERNEL[i][j] /= kernelSum;
// Apply the filter
Mat filteredPage;
page.copyTo(filteredPage);
for(int row = BORDER_SIZE; row < PAGE_HEIGHT - BORDER_SIZE; row++) {
for(int col = BORDER_SIZE; col < PAGE_WIDTH - BORDER_SIZE; col++) {
float shade = 0.0f;
for(int krow = 0; krow < FILTER_KERNEL_WIDTH; krow++) {
for(int kcol = 0; kcol < FILTER_KERNEL_WIDTH; kcol++) {
int x = col - int(FILTER_KERNEL_WIDTH/2) + kcol;
int y = row - int(FILTER_KERNEL_WIDTH/2) + krow;
#ifdef FILTER_INPLACE
shade += KERNEL[krow][kcol] * (float)filteredPage.ptr<uint8_t>(y)[x];
#else
shade += KERNEL[krow][kcol] * (float)page.ptr<uint8_t>(y)[x];
#endif
}
}
filteredPage.ptr<uint8_t>(row)[col] = shade;
}
}
#endif
// Write the images to the current folder
imwrite("texture.png", page);
#ifdef APPLY_FILTER
imwrite("filtered_texture.png", filteredPage);
#endif
return EXIT_SUCCESS;
}
// Program entry point
int main(int argc, char** argv) {
try {
return run(argc, argv);
} catch(exception e) {
cerr << "ERROR: " << e.what();
}
return EXIT_FAILURE;
}