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 | #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
class Color {
public:
unsigned char r, g, b;
Color(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0) : r(r), g(g), b(b) {}
};
class Image {
private:
std::vector<std::vector<Color>> pixels;
int width, height;
public:
Image(int w, int h) : width(w), height(h), pixels(h, std::vector<Color>(w)) {}
void setPixel(int x, int y, const Color& c) {
if (x >= 0 && x < width && y >= 0 && y < height) {
pixels[y][x] = c;
}
}
Color getPixel(int x, int y) const {
if (x >= 0 && x < width && y >= 0 && y < height) {
return pixels[y][x];
}
return Color();
}
int getWidth() const { return width; }
int getHeight() const { return height; }
};
class QuadTree {
private:
struct Node {
Color color;
Node* children[4];
int x, y, size;
Node(int x, int y, int size) : x(x), y(y), size(size) {
for (int i = 0; i < 4; ++i) children[i] = nullptr;
}
~Node() {
for (int i = 0; i < 4; ++i) delete children[i];
}
};
Node* root;
int threshold;
bool isLeaf(Node* node) const {
return node->children[0] == nullptr;
}
Color averageColor(const Image& image, int x, int y, int size) const {
long long r = 0, g = 0, b = 0;
int count = 0;
for (int i = y; i < y + size; ++i) {
for (int j = x; j < x + size; ++j) {
Color c = image.getPixel(j, i);
r += c.r; g += c.g; b += c.b;
++count;
}
}
return Color(r / count, g / count, b / count);
}
int colorDifference(const Color& c1, const Color& c2) const {
return std::abs(c1.r - c2.r) + std::abs(c1.g - c2.g) + std::abs(c1.b - c2.b);
}
void buildTree(Node* node, const Image& image) {
Color avgColor = averageColor(image, node->x, node->y, node->size);
node->color = avgColor;
if (node->size == 1) return;
bool shouldSplit = false;
for (int y = node->y; y < node->y + node->size; ++y) {
for (int x = node->x; x < node->x + node->size; ++x) {
if (colorDifference(image.getPixel(x, y), avgColor) > threshold) {
shouldSplit = true;
break;
}
}
if (shouldSplit) break;
}
if (shouldSplit) {
int newSize = node->size / 2;
node->children[0] = new Node(node->x, node->y, newSize);
node->children[1] = new Node(node->x + newSize, node->y, newSize);
node->children[2] = new Node(node->x, node->y + newSize, newSize);
node->children[3] = new Node(node->x + newSize, node->y + newSize, newSize);
for (int i = 0; i < 4; ++i) {
buildTree(node->children[i], image);
}
}
}
void reconstruct(Node* node, Image& image) const {
if (isLeaf(node)) {
for (int y = node->y; y < node->y + node->size; ++y) {
for (int x = node->x; x < node->x + node->size; ++x) {
image.setPixel(x, y, node->color);
}
}
} else {
for (int i = 0; i < 4; ++i) {
reconstruct(node->children[i], image);
}
}
}
public:
QuadTree(const Image& image, int threshold) : threshold(threshold) {
root = new Node(0, 0, std::max(image.getWidth(), image.getHeight()));
buildTree(root, image);
}
~QuadTree() { delete root; }
Image reconstruct() const {
Image result(root->size, root->size);
reconstruct(root, result);
return result;
}
};
int main() {
// Create a sample 8x8 image
Image originalImage(8, 8);
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
if (x < 4 && y < 4) {
originalImage.setPixel(x, y, Color(255, 0, 0)); // Red quadrant
} else if (x >= 4 && y < 4) {
originalImage.setPixel(x, y, Color(0, 255, 0)); // Green quadrant
} else if (x < 4 && y >= 4) {
originalImage.setPixel(x, y, Color(0, 0, 255)); // Blue quadrant
} else {
originalImage.setPixel(x, y, Color(255, 255, 0)); // Yellow quadrant
}
}
}
// Create QuadTree with a threshold of 50
QuadTree qt(originalImage, 50);
// Reconstruct the image from the QuadTree
Image reconstructedImage = qt.reconstruct();
// Print original and reconstructed images
std::cout << "Original Image:" << std::endl;
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
Color c = originalImage.getPixel(x, y);
std::cout << "(" << (int)c.r << "," << (int)c.g << "," << (int)c.b << ") ";
}
std::cout << std::endl;
}
std::cout << "\nReconstructed Image:" << std::endl;
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
Color c = reconstructedImage.getPixel(x, y);
std::cout << "(" << (int)c.r << "," << (int)c.g << "," << (int)c.b << ") ";
}
std::cout << std::endl;
}
return 0;
}
|