/
ShellViewModel.cs
294 lines (253 loc) · 10.8 KB
/
ShellViewModel.cs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace CrazyBlindDateReverser
{
[Export(typeof(IShell))]
public class ShellViewModel : IShell
{
public ObservableCollection<WriteableBitmap> ImagePieces { get; set; }
public ShellViewModel()
{
ImagePieces = new ObservableCollection<WriteableBitmap>();
foreach (var file in Directory.GetFiles(@"c:\users\kevin\desktop\", "*.jpg"))
SolveImage(file);
//SolveImage(@"c:\users\kevin\desktop\85b3f898933780a9ee9f.jpg");
}
private void SolveImage(string baseFileName)
{
var imagePieces = new List<WriteableBitmap>();
var bmpImg = new BitmapImage(new Uri(baseFileName, UriKind.Absolute));
var baseImage = BitmapFactory.ConvertToPbgra32Format(bmpImg);
int pieceHeight = (int)(baseImage.Height / 4);
int pieceWidth = (int)(baseImage.Width / 4);
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
var bmp = BitmapFactory.New(pieceHeight, pieceWidth);
bmp.Blit(new Rect(0, 0, pieceWidth, pieceHeight), baseImage, new Rect(x * pieceWidth, y * pieceHeight, pieceWidth, pieceHeight));
imagePieces.Add(bmp);
}
}
var leftRightScores = new Dictionary<Tuple<int, int>, double>();
var upDownScores = new Dictionary<Tuple<int, int>, double>();
foreach (var a in Enumerable.Range(0, imagePieces.Count))
{
foreach (var b in Enumerable.Range(0, imagePieces.Count))
{
var aImg = imagePieces[a];
var bImg = imagePieces[b];
double leftRightScore = Enumerable.Range(0, pieceHeight)
.Average(i => ColorDiff(aImg.GetPixel(pieceWidth - 1, i), bImg.GetPixel(0, i)));
//if (leftRightScore < 0.00001)
// leftRightScore = 1;
leftRightScores[new Tuple<int, int>(a, b)] = leftRightScore;
double upDownScore = Enumerable.Range(0, pieceHeight)
.Average(i => ColorDiff(aImg.GetPixel(i, pieceHeight - 1), bImg.GetPixel(i, 0)));
//if (upDownScore < 0.00001)
// upDownScore = 1;
upDownScores[new Tuple<int, int>(a, b)] = upDownScore;
}
}
//var fullMap = new WriteableBitmap[9,9];
//var remaining = imagePieces.ToList();
//fullMap[4, 4] = remaining.First();
//remaining.Remove(fullMap[4, 4]);
//int ctr = 0;
//while (remaining.Any())
//{
// if (ctr++ == 2)
// break;
// var best = new
// {
// Bmp = (WriteableBitmap)null,
// X = -1,
// Y = -1,
// Score = Double.MaxValue
// };
// foreach (var item in remaining)
// {
// for (int x = 0; x < 9; x++)
// {
// for (int y = 0; y < 9; y++)
// {
// if (fullMap[x, y] != null)
// continue;
// double score = 0;
// // check below
// if (y < 8 && fullMap[x, y + 1] != null)
// {
// score += upDownScores[new Tuple<WriteableBitmap, WriteableBitmap>(item, fullMap[x, y + 1])];
// }
// else
// {
// score += 10;
// }
// // check above
// if (y > 0 && fullMap[x, y - 1] != null)
// {
// score += upDownScores[new Tuple<WriteableBitmap, WriteableBitmap>(fullMap[x, y - 1], item)];
// }
// else
// {
// score += 10;
// }
// // check right
// if (x < 8 && fullMap[x + 1, y] != null)
// {
// score += leftRightScores[new Tuple<WriteableBitmap, WriteableBitmap>(item, fullMap[x + 1, y])];
// }
// else
// {
// score += 10;
// }
// // check left
// if (x > 0 && fullMap[x - 1, y] != null)
// {
// score += leftRightScores[new Tuple<WriteableBitmap, WriteableBitmap>(fullMap[x - 1, y], item)];
// }
// else
// {
// score += 10;
// }
// if (score < best.Score)
// {
// best = new
// {
// Bmp = item,
// X = x,
// Y = y,
// Score = score
// };
// }
// }
// }
// }
// if (best.Bmp == null)
// break;
// fullMap[best.X, best.Y] = best.Bmp;
// remaining.Remove(best.Bmp);
//}
int[,] fullMap;
double beta = 0.000001;
do
{
fullMap = new int[4,4]
{
{
-1, -1, -1, -1
},
{
-1, -1, -1, -1
},
{
-1, -1, -1, -1
},
{
-1, -1, -1, -1
},
};
RecursiveSolver(leftRightScores, upDownScores, Enumerable.Range(0, 16).ToList(), fullMap, 0, 0, beta);
beta *= 1.05;
}
while (!FullyDone(fullMap));
var finalBmp = BitmapFactory.New(fullMap.GetLength(0) * pieceHeight, fullMap.GetLength(1) * pieceWidth);
for (int x = 0; x < fullMap.GetLength(0); x++)
{
for (int y = 0; y < fullMap.GetLength(1); y++)
{
if (fullMap[x, y] != -1)
{
finalBmp.Blit(new Rect(x * pieceWidth, y * pieceWidth, pieceWidth, pieceHeight), imagePieces[fullMap[x, y]], new Rect(0, 0, pieceWidth, pieceHeight));
}
}
}
ImagePieces.Add(baseImage);
ImagePieces.Add(finalBmp);
}
private bool FullyDone(int[,] fullMap)
{
for (int x = 0; x < fullMap.GetLength(0); x++)
{
for (int y = 0; y < fullMap.GetLength(1); y++)
{
if (fullMap[x, y] == -1)
return false;
}
}
return true;
}
private double RecursiveSolver(
Dictionary<Tuple<int, int>, double> leftRightScores,
Dictionary<Tuple<int, int>, double> upDownScores,
IList<int> remaining,
int[,] fullMap,
int x = 0,
int y = 0,
double beta = double.MaxValue)
{
var score = Evaluate(leftRightScores, upDownScores, fullMap);
if (score > beta)
return score;
if (!remaining.Any())
return score;
double alpha = double.MaxValue;
int[,] best = null;
foreach (var next in remaining)
{
var mapCopy = (int[,])fullMap.Clone();
mapCopy[x, y] = next;
//int nextX = x == 0 && y < 3 ? y + 1 : y == 3 ? 3 : x - 1;
//int nextY = x == 0 && y < 3 ? 0 : y == 3 ? x : y + 1;
int nextX = x == 3 ? 0 : x + 1;
int nextY = x == 3 ? y + 1 : y;
double s = RecursiveSolver(leftRightScores, upDownScores, remaining.Where(i => i != next).ToList(), mapCopy, nextX, nextY, beta);
if (s < alpha)
{
alpha = s;
best = mapCopy;
}
}
for (int xx=0; xx<4; xx++)
for (int yy=0; yy<4; yy++)
fullMap[xx, yy] = best[xx, yy];
return alpha;
}
private static double Evaluate(Dictionary<Tuple<int, int>, double> leftRightScores, Dictionary<Tuple<int, int>, double> upDownScores, int[,] fullMap)
{
double score = 0;
int ct = 0;
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (xx < 3 && fullMap[xx, yy] != -1 && fullMap[xx + 1, yy] != -1)
{
score = Math.Max(score, leftRightScores[new Tuple<int, int>(fullMap[xx, yy], fullMap[xx + 1, yy])]);
ct++;
}
if (yy < 3 && fullMap[xx, yy] != -1 && fullMap[xx, yy + 1] != -1)
{
score = Math.Max(score, upDownScores[new Tuple<int, int>(fullMap[xx, yy], fullMap[xx, yy + 1])]);
ct++;
}
}
}
return ct > 0 ? score / ct : 0;
}
private double ColorDiff(Color a, Color b)
{
const double factor = 2;
return Math.Pow(Math.Abs(a.R - b.R) / 256.0, factor) +
Math.Pow(Math.Abs(a.G - b.G) / 256.0, factor) +
Math.Pow(Math.Abs(a.B - b.B) / 256.0, factor);
}
}
}