-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcess.cpp
More file actions
60 lines (53 loc) · 1.68 KB
/
ImageProcess.cpp
File metadata and controls
60 lines (53 loc) · 1.68 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
#include "include.h"
cv::Mat ResizeImage(cv::Mat image, int width, int height)
{
cv::Mat resizedImage;
cv::resize(image, resizedImage, cv::Size(width, height));
return resizedImage;
}
std::vector<Tile> SpliteImage(cv::Mat image)
{
std::vector<Tile> tiles;
for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
cv::Rect positions(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
tiles.push_back({image(positions).clone(), cv::Point(x, y), false});
}
}
return tiles;
}
std::vector<Tile> ShuffleTiles(std::vector<Tile> &tiles)
{
std::vector<Tile> shuffledTiles = tiles;
for (;;)
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(shuffledTiles.begin(), shuffledTiles.end() - 1, g); // 最后1个一开始不显示,所以不用打乱
if (CalculateInversions(PointsToInts(shuffledTiles)) % 2 == 0)
{
// std::cout << "The inverse ordinal number is even, shuffled successfully!" << std::endl;
return shuffledTiles; // 只返回逆序数为偶数的打乱结果
break;
}
// std::cout << "The inverse ordinal number is not even, shuffling again..." << std::endl;
}
}
void DrawTiles(cv::Mat &canvas, const std::vector<Tile> &tiles)
{
int index = 0;
for (int y = 0; y < GRID_SIZE; ++y)
{
for (int x = 0; x < GRID_SIZE; ++x)
{
if (!tiles[index].isEmpty)
{
cv::Rect roi(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
tiles[index].image.copyTo(canvas(roi));
}
index++;
}
}
}