当前位置:网站首页>Opencv application -- jigsaw puzzle
Opencv application -- jigsaw puzzle
2022-04-23 20:59:00 【csuzhucong】
One , Prepare picture
You can divide it manually first , There is some key information in the middle , Some are on top , Mainly these two .
Then use the program to cut out the picture of the square first :
int main()
{
for (int i = 1; i <= 10; i++) {
Mat img = imread("D:/pic2/img (" + to_string(i) + ").jpg",0);
int p = min(img.rows, img.cols);
Mat img2 = img(Rect((img.cols - p) / 2, (img.rows - p) / 2, p, p));
imwrite("D:/pic2/a" + to_string(i) + ".jpg",img2);
}
return 0;
}
Cut the top :
int main()
{
for (int i = 1; i <= 15; i++) {
Mat img = imread("D:/pic2/img (" + to_string(i) + ").jpg",0);
int p = min(img.rows, img.cols);
Mat img2 = img(Rect(0, 0, p, p));
imwrite("D:/pic2/a" + to_string(i) + ".jpg",img2);
}
return 0;
}
Two , Puzzle
Let's first adjust all the small graphs to a uniform size , Then sort by brightness .
For the target image , Block by , The brightness ranking is also counted separately .
Then match according to the sorting results , Just fill the small picture to the corresponding position .
In addition to brightness matching , Before finally putting it on the big picture , Also do brightness adjustment , Adjust the overall brightness to the same as the block brightness .
struct Nodes
{
int t;
int id;
bool operator<(Nodes a) const
{
if (t == a.t)return id < a.id;
return t < a.t;
}
};
int imageSum(Mat img)
{
int s = 0;
for (int i = 0; i < img.rows; i++)for (int j = 0; j < img.cols; j++)
s += img.at<uchar>(i, j);
return s;
}
int main()
{
const int N = 9;
const int NUM = N * N;
const int SIZE = N * 2000;
const int PIX = 100;
const int R1 = SIZE / PIX;
const int R = R1 / N * R1 / N;
Mat imgs[NUM];
for (int i = 1; i <= NUM; i++) {
imgs[i - 1] = imread("D:/pic2/img (" + to_string(i) + ").jpg", 0);
resize(imgs[i - 1], imgs[i - 1], Size(PIX, PIX));
}
Mat img = imread("D:/pic2/img (74).jpg", 0);
resize(img, img, Size(SIZE, SIZE));
Nodes node[NUM * R];
Nodes node2[NUM * R];
for (int i = 0; i < NUM * R; i++) {
node[i].t = imageSum(imgs[i % NUM]);
node[i].id = i % NUM;
node2[i].t = imageSum(img(Rect(i % R1 * PIX, i / R1 * PIX, PIX, PIX)));
node2[i].id = i;
}
sort(node, node + NUM * R);
sort(node2, node2 + NUM * R);
for (int i = 0; i < NUM * R; i++)
{
Mat image;
imgs[node[i].id % NUM].copyTo(image);
image *= 1.0 * node2[i].t / node[i].t;
image.copyTo(img(Rect(node2[i].id % R1 * PIX, node2[i].id / R1 * PIX, PIX, PIX)));
}
//imshow("img", img);
imwrite("D:/pic2/ans.png", img);
cv::waitKey(0);
return 0;
}
Joining together the results :
( Head ) Zoom in :
( eyes ) Zoom in :
3、 ... and , Optimization idea
1, There are few pictures currently used , If there are more pictures , The effect will be better .
2, Because there are few pictures , Each picture is used many times . Currently, each picture is used the same number of times , This can be optimized , Adaptively select the number of times each picture is used , The effect should be better .
3, If you want to make it colorful , The main problem is the matching of images . Grayscale images only need to be sorted according to brightness to match , Changing to a color image is equivalent to changing from one-dimensional to three-dimensional .
版权声明
本文为[csuzhucong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232049172865.html
边栏推荐
- Write table of MySQL Foundation (create table)
- Deep analysis of C language pointer (Part I)
- Graph traversal - BFS, DFS
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- Win 11K in 100 days, super complete learning guide for job transfer test
- Fastdfs mind map
- setInterval、setTimeout、requestAnimationFrame
- 亚马逊和Epic将入驻,微软应用商城向第三方开放
- On the three paradigms of database design
- Norm normalization in tensorflow and pytorch of records
猜你喜欢
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
Addition, deletion, modification and query of MySQL advanced table
Unity solves Z-fighting
Is rust more suitable for less experienced programmers?
On the three paradigms of database design
Win 11K in 100 days, super complete learning guide for job transfer test
Latex formula
Lunch on the 23rd day at home
Mysql database common sense storage engine
Unity Odin ProgressBar add value column
随机推荐
2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
Fastdfs mind map
ubutnu20安装CenterNet
Express③(使用Express编写接口、跨域有关问题)
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
1.整理华子面经--1
Thinkphp5 + data large screen display effect
On the three paradigms of database design
Addition, deletion, modification and query of advanced MySQL data (DML)
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
引入结构化并发,Swift 5.5 发布!
Unity solves Z-fighting
Deep analysis of C language pointer (Part I)
Alibaba cloud responded to the disclosure of user registration information
Queue template code
Google 尝试在 Chrome 中使用 Rust
Unity animation creates sequence frame code and generates animationclip
setInterval、setTimeout、requestAnimationFrame
Question brushing plan -- backtracking method (I)
Norm normalization in tensorflow and pytorch of records