欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用c++实现OpenCV图像横向&纵向拼接

程序员文章站 2022-06-27 18:24:21
功能函数// 图像拼接cv::mat imagesplicing(vector images,int type){if (type != 0 && typ...

功能函数

// 图像拼接
cv::mat imagesplicing(vector<cv::mat> images,int type)
{
	if (type != 0 && type != 1)
		type = 0;
	
	int num = images.size();
	int newrow = 0;
	int newcol = 0;
	cv::mat result;
 	// 横向拼接
	if (type == 0)
	{
		int minrow = 10000;
		for (int i = 0; i < num; ++i)
		{
			if (minrow > images[i].rows)
				minrow = images[i].rows;
		}
		newrow = minrow;
		for (int i = 0; i < num; ++i)
		{
			int tcol = images[i].cols*minrow / images[i].rows;
			int trow = newrow;
			cv::resize(images[i], images[i], cv::size(tcol, trow));
			newcol += images[i].cols;
			if (images[i].type() != images[0].type())
				images[i].convertto(images[i], images[0].type());
		}
		result = cv::mat(newrow, newcol, images[0].type(), cv::scalar(255, 255, 255));
 		cv::range rangerow, rangecol;
		int start = 0;
		for (int i = 0; i < num; ++i)
		{
			rangerow = cv::range((newrow - images[i].rows) / 2, (newrow - images[i].rows) / 2 + images[i].rows);
			rangecol = cv::range(start, start + images[i].cols);
			images[i].copyto(result(rangerow, rangecol));
			start += images[i].cols;
		}
	}
	// 纵向拼接
	else if (type == 1) {
		int mincol = 10000;
		for (int i = 0; i < num; ++i)
		{
			if (mincol > images[i].cols)
				mincol = images[i].cols;
		}
		newcol = mincol;
		for (int i = 0; i < num; ++i)
		{
			int trow = images[i].rows*mincol / images[i].cols;
			int tcol = newcol;
			cv::resize(images[i], images[i], cv::size(tcol, trow));
			newrow += images[i].rows;
			if (images[i].type() != images[0].type())
				images[i].convertto(images[i], images[0].type());
		}
		result = cv::mat(newrow, newcol, images[0].type(), cv::scalar(255, 255, 255));
 
		cv::range rangerow, rangecol;
		int start = 0;
		for (int i = 0; i < num; ++i)
		{
			rangecol= cv::range((newcol - images[i].cols) / 2, (newcol - images[i].cols) / 2 + images[i].cols);
			rangerow = cv::range(start, start + images[i].rows);
			images[i].copyto(result(rangerow, rangecol));
			start += images[i].rows;
		}
	}
	return result;
}

测试代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
cv::mat imagesplicing(vector<cv::mat> images, int type);
int main()
{
	cv::mat src1 = imread("1.jpg");
	cv::mat src2 = imread("2.jpg");
	cv::mat src3 = imread("3.jpg");
	cv::mat src4 = imread("4.jpg");
	vector<cv::mat> images;
	images.push_back(src1);
	images.push_back(src2);
	images.push_back(src3);
	images.push_back(src4);
 
	// 0为横向
	cv::mat result1 = imagesplicing(images, 0);
	// 1为纵向
	cv::mat result2 = imagesplicing(images, 1);
	imwrite("result1.jpg",result1);
	imwrite("result2.jpg",result2);
 	return 0;
}
// 图像拼接
cv::mat imagesplicing(vector<cv::mat> images,int type)
{
	if (type != 0 && type != 1)
		type = 0;
	int num = images.size();
	int newrow = 0;
	int newcol = 0;
	cv::mat result;
 	// 横向拼接
	if (type == 0)
	{
		int minrow = 10000;
		for (int i = 0; i < num; ++i)
		{
			if (minrow > images[i].rows)
				minrow = images[i].rows;
		}
		newrow = minrow;
		for (int i = 0; i < num; ++i)
		{
			int tcol = images[i].cols*minrow / images[i].rows;
			int trow = newrow;
			cv::resize(images[i], images[i], cv::size(tcol, trow));
			newcol += images[i].cols;
			if (images[i].type() != images[0].type())
				images[i].convertto(images[i], images[0].type());
		}
		result = cv::mat(newrow, newcol, images[0].type(), cv::scalar(255, 255, 255));
 
		cv::range rangerow, rangecol;
		int start = 0;
		for (int i = 0; i < num; ++i)
		{
			rangerow = cv::range((newrow - images[i].rows) / 2, (newrow - images[i].rows) / 2 + images[i].rows);
			rangecol = cv::range(start, start + images[i].cols);
			images[i].copyto(result(rangerow, rangecol));
			start += images[i].cols;
		}
	}
	// 纵向拼接
	else if (type == 1) {
		int mincol = 10000;
		for (int i = 0; i < num; ++i)
		{
			if (mincol > images[i].cols)
				mincol = images[i].cols;
		}
		newcol = mincol;
		for (int i = 0; i < num; ++i)
		{
			int trow = images[i].rows*mincol / images[i].cols;
			int tcol = newcol;
			cv::resize(images[i], images[i], cv::size(tcol, trow));
			newrow += images[i].rows;
			if (images[i].type() != images[0].type())
				images[i].convertto(images[i], images[0].type());
		}
		result = cv::mat(newrow, newcol, images[0].type(), cv::scalar(255, 255, 255));
 		cv::range rangerow, rangecol;
		int start = 0;
		for (int i = 0; i < num; ++i)
		{
			rangecol= cv::range((newcol - images[i].cols) / 2, (newcol - images[i].cols) / 2 + images[i].cols);
			rangerow = cv::range(start, start + images[i].rows);
			images[i].copyto(result(rangerow, rangecol));
			start += images[i].rows;
		}
	}
	
	return result;
}

测试效果 

使用c++实现OpenCV图像横向&纵向拼接 
图1 横向拼接
使用c++实现OpenCV图像横向&纵向拼接
图2 纵向拼接

以上就是使用c++实现opencv图像横向&纵向拼接的详细内容,更多关于c++实现opencv图像的资料请关注其它相关文章!