当前位置:网站首页>opencv应用——以图拼图
opencv应用——以图拼图
2022-04-23 20:49:00 【csuzhucong】
一,准备图片
可以先手动分一分,有的是关键信息在中间,有的是在上面,主要就是这两种。
然后用程序先截出正方形的图片:
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;
}
截上面的:
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;
}
二,拼图
我们先把所有小图调整到统一的大小,然后按照亮度排序。
对于目标图像,按照分块,也分别统计亮度排序。
再根据排序结果进行匹配,把小图往对应位置填充就行了。
图片除了要做亮度匹配之外,在最后拼到大图上之前,还要做亮度调整,把整体亮度调到和分块亮度相同。
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;
}

拼接结果:

(头部)放大效果:

(眼睛)放大效果:

三,优化思路
1,当前所用图片较少,如果图片多一点,效果会好一点。
2,因为图片少,每个图片都被用到多次。当前是每个图片被使用的次数相同,这一点可以优化一下,自适应的选择每个图片被使用的次数,效果应该会更好。
3,要想搞成彩色的,主要问题在于图片的匹配问题。灰度图像只需要按照亮度排序即可匹配,改成彩色图像相当于从一维变成三维。
版权声明
本文为[csuzhucong]所创,转载请带上原文链接,感谢
https://blog.csdn.net/nameofcsdn/article/details/124357818
边栏推荐
- 亚马逊和Epic将入驻,微软应用商城向第三方开放
- 6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
- MySQL basic collection
- Gsi-ecm digital platform for engineering construction management
- 小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
- Resolve the error - error identifier 'attr_ id‘ is not in camel case camelcase
- 如何发挥测试策略的指导性作用
- pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
- C# 知识
- laravel 发送邮件
猜你喜欢
随机推荐
6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
打新债中签以后怎么办,网上开户安全吗
Singleton mode
UKFslam
setInterval、setTimeout、requestAnimationFrame
Selenium 显示等待WebDriverWait
3-5 obtaining cookies through XSS and the use of XSS background management system
How to do after winning the new debt? Is it safe to open an account online
Use of node template engine
Latex formula
Summary and effect analysis of methods for calculating binocular parallax
41. 缺失的第一个正数
Solve importerror: cannot import name 'imread' from 'SciPy misc‘
MySQL基础合集
电脑越用越慢怎么办?文件误删除恢复方法
Some grounded words
Addition, deletion, modification and query of MySQL advanced table
Zhongchuang storage | how to choose a useful distributed storage cloud disk
GSI-ECM工程建设管理数字化平台
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?








