-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
213 lines (182 loc) · 5.52 KB
/
Copy pathmain.cpp
File metadata and controls
213 lines (182 loc) · 5.52 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <math.h>
#include <queue>
#include "jpgd.h"
#include "jpge.h"
void binarizeImage(unsigned char *image, unsigned char *binaryImage, int nPixels, unsigned char threshold)
{
for (int i = 0; i < nPixels; i++)
{
if (image[i] > threshold)
binaryImage[i] = 255;
else
binaryImage[i] = 0;
}
}
//This function "fixes" some places in the maze where the wall is too thin.
void dilateImage(unsigned char *image, int w, int h)
{
unsigned char curPix, nextPix, belowPix, acrossPix;
for (int x = 0; x < w - 1; x++)
{
for (int y = 0; y < h - 1; y++)
{
curPix = image[x + y * w];
nextPix = image[x + 1 + y * w];
belowPix = image[x + (y + 1) * w];
acrossPix = image[x + 1 + (y + 1) * w];
if ((curPix == 0 && nextPix == 255 && belowPix == 255 && acrossPix == 0) ||
(curPix == 255 && nextPix == 0 && belowPix == 0 && acrossPix == 255))
{
image[x + y * w] = 0;
image[x + 1 + y * w] = 0;
image[x + (y + 1) * w] = 0;
image[x + 1 + (y + 1) * w] = 0;
}
}
}
}
void getPathThroughMaze(unsigned char *outputImage, unsigned char *binaryImage, int w, int h, int startX, int startY, int endX, int endY, bool drawCostMap)
{
unsigned char *visited = (unsigned char*)calloc(w * h, 1);
unsigned int *pathValue = (unsigned int*)calloc(w * h, sizeof(unsigned int));
std::queue<std::pair<int, int> > currentPoints;
currentPoints.push(std::pair<int, int>(startX, startY));
pathValue[startX + startY * w] = 1;
visited[startX + startY * w] = 1;
printf("Running Dijkstra's algorithm\n");
bool endReached = false;
unsigned int maxPathValue = 0;
while (currentPoints.size() > 0)
{
std::pair<int, int> currPair = currentPoints.front();
currentPoints.pop();
int currX = currPair.first;
int currY = currPair.second;
unsigned int currPathValue = pathValue[currX + currY * w];
if (currPathValue > maxPathValue)
maxPathValue = currPathValue;
if (currX == endX && currY == endY)
{
endReached = true;
break;
}
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (currX + j < 0 || currY + i < 0 || currX + j >= w || currY + i >= h)
continue;
if (binaryImage[(currX + j) + (currY + i) * w] && !visited[(currX + j) + (currY + i) * w])
{
pathValue[(currX + j) + (currY + i) * w] = currPathValue + 1;
visited[(currX + j) + (currY + i) * w] = 1;
currentPoints.push(std::pair<int, int>(currX + j, currY + i));
}
}
}
}
if (drawCostMap)
{
double multiplier = 1.0 / maxPathValue;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
for (int i = 0; i < 3; i++)
outputImage[3 * (x + y * w) + i] = static_cast<unsigned char>(255 * multiplier * pathValue[x + y * w]);
}
}
return;
}
if (!endReached)
{
printf("Solution to the maze not found\n");
return;
}
//Let's color the maze walls in a black, with gray background so that the path is more visible.
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
for (int i = 0; i < 3; i++)
{
if (binaryImage[x + y * w] == 255)
outputImage[3 * (x + y * w) + i] = 128;
}
}
}
printf("Drawing path\n");
unsigned int currPathValue = pathValue[endX + endY * w];
int currX = endX;
int currY = endY;
while (currPathValue != 1)
{
outputImage[3 * (currX + currY * w) + 0] = 0;
outputImage[3 * (currX + currY * w) + 1] = 255;
outputImage[3 * (currX + currY * w) + 2] = 0;
int bestMoveX = 0;
int bestMoveY = 0;
unsigned int bestMovePathVal = maxPathValue;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (i == 0 && j == 0)
continue;
if (currX + j < 0 || currY + i < 0 || currX + j >= w || currY + i >= h)
continue;
if (pathValue[(currX + j) + (currY + i) * w] == 0)
continue;
if (pathValue[(currX + j) + (currY + i) * w] < bestMovePathVal)
{
bestMovePathVal = pathValue[(currX + j) + (currY + i) * w];
bestMoveX = currX + j;
bestMoveY = currY + i;
}
}
}
currX = bestMoveX;
currY = bestMoveY;
currPathValue = pathValue[currX + currY * w];
}
}
int main(int argc, char *argv[])
{
unsigned char *inputImage, *outputImage, *binaryImage;
int w, h;
int startX, startY, endX, endY;
bool saveCostMap = false;
if (argc < 6)
{
printf("Not enough input argmuents. The proper way to run the program is shown below.\n");
printf("If the program completes successully the path through the maze will be saved in output.jpg\n");
printf("\n");
printf("MazeSolver.exe IMAGE_FILE_NAME.jpg START_X START_Y END_X END_Y [SAVE_COST_MAP]\n");
printf("\n");
printf("Where:\n");
printf("\tSTART_X/Y are the pixel coordinates of the maze entrance.\n");
printf("\tEND_X/Y are the pixel coordinates of the maze exit.\n");
printf("\t[SAVE_COST_MAP] is an additional paramter, if added the program saves the maze cost map instead of the path through the maze\n");
return -1;
}
if (argc == 7)
saveCostMap = true;
startX = atoi(argv[2]);
startY = atoi(argv[3]);
endX = atoi(argv[4]);
endY = atoi(argv[5]);
int actual_comps;
inputImage = jpgd::decompress_jpeg_image_from_file(argv[1], &w, &h, &actual_comps, 1);
outputImage = new unsigned char[w * h * 3]();
binaryImage = new unsigned char[w * h]();
binarizeImage(inputImage, binaryImage, w * h, 200);
dilateImage(binaryImage, w, h);
getPathThroughMaze(outputImage, binaryImage, w, h, startX, startY, endX, endY, saveCostMap);
jpge::params params;
params.m_quality = 100;
jpge::compress_image_to_jpeg_file("output.jpg", w, h, 3, outputImage, params);
delete []inputImage;
delete []outputImage;
return 0;
}