当前位置:网站首页>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
边栏推荐
- go struct
- Common problems in deploying projects with laravel and composer for PHP
- Fastdfs mind map
- On the three paradigms of database design
- Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
- Learn to C language fourth day
- C knowledge
- Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
- Sequential state
- 2021-06-29 C escape character cancellation and use
猜你喜欢
3-5通过XSS获取cookie以及XSS后台管理系统的使用
100天拿下11K,转岗测试的超全学习指南
GSI-ECM工程建设管理数字化平台
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
DeNO 1.13.2 release
go defer
Addition, deletion, modification and query of advanced MySQL data (DML)
Unity Odin ProgressBar add value column
Use 3080ti to run tensorflow GPU = 1 X version of the source code
Keywords static, extern + global and local variables
随机推荐
Express ③ (use express to write interface and cross domain related issues)
DeNO 1.13.2 release
Some thoughts on super in pytorch, combined with code
opencv应用——以图拼图
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
又一款数据分析神器:Polars 真的很强大
go array
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Cmake project under vs2019: calculating binocular parallax using elas method
MySQL advanced common functions
如何发挥测试策略的指导性作用
ROS学习笔记-----ROS的使用教程
Selenium displays webdriverwait
MySQL数据库常识之储存引擎
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
管道和xargs
PHP的Laravel与Composer部署项目时常见问题
Introduce structured concurrency and release swift 5.5!
An error occurs when the addressable assets system project is packaged. Runtimedata is null
阿里云回应用户注册信息泄露事件