当前位置:网站首页>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
边栏推荐
- Leetcode 994, rotten orange
- 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
- Learn to C language fourth day
- The iswow64process function determines the number of program bits
- 2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
- Leetcode-279-complete square number
- GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
- Resolve the error - error identifier 'attr_ id‘ is not in camel case camelcase
- Is qiniu school useful and is the recommended securities account safe
- go slice
猜你喜欢

Deno 1.13.2 发布

Common commands of MySQL in Linux

Unity animation creates sequence frame code and generates animationclip

Deep analysis of C language function

CUDA, NVIDIA driver, cudnn download address and version correspondence

Write table of MySQL Foundation (create table)

小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌

Unity Odin ProgressBar add value column

MySQL基础合集

wait、waitpid
随机推荐
go array
Fastdfs mind map
How to configure SSH public key in code cloud
Syntaxerror: unexpected token r in JSON at position 0
Valueerror: invalid literal for int() with base 10 conversion error related to data type
Introduction to standardization, regularization and normalization
MySQL基础之写表(创建表)
LeetCode-279-完全平方数
go interface
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
Lunch on the 23rd day at home
Awk example skills
GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
Elastic box model
MySQL basic collection
Express③(使用Express编写接口、跨域有关问题)
Leetcode 232, queue with stack
41. 缺失的第一个正数
Deno 1.13.2 发布
matplotlib. Pyplot partition drawing