Opencv C++ 图像混合(贴图)

原始图片下载地址:
链接:https://pan.baidu.com/s/1fmTNu324_K1D2rGLCmR_RQ
提取码:jap1

#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;


//图片混合
bool MixImage(Mat& srcImage, Mat mixImage, Point startPoint)
{
	//检查图片数据
	if (!srcImage.data || !mixImage.data)
	{
		cout << "输入图片 数据错误!" << endl ;
		return false;
	}
	//检查行列是否越界
	int addCols = startPoint.x + mixImage.cols > srcImage.cols ? 0 : mixImage.cols;
	int addRows = startPoint.y + mixImage.rows > srcImage.rows ? 0 : mixImage.rows;
	if (addCols ==0 || addRows ==0)
	{
		cout << "添加图片超出" << endl;
		return false;
	}

	//ROI 混合区域
	Mat roiImage = srcImage(Rect(startPoint.x, startPoint.y, addCols, addRows));

	//图片类型一致
	if (srcImage.type() == mixImage.type())
	{
		mixImage.copyTo(roiImage, mixImage);
		return true;
	}

	Mat maskImage;
	//原始图片:灰度  贴图:彩色
	if (srcImage.type() == CV_8U && mixImage.type() == CV_8UC3)
	{
		cvtColor(mixImage, maskImage, COLOR_BGR2GRAY);
		maskImage.copyTo(roiImage, maskImage);
		return true;
	}

	//原始图片:彩色  贴图:灰色
	if (srcImage.type() == CV_8UC3 && mixImage.type() == CV_8U)
	{
		cvtColor(mixImage, maskImage, COLOR_GRAY2BGR);
		maskImage.copyTo(roiImage, maskImage);
		return true;
	}

	return false;
}

int main()
{ 
	//1、读取图片;
	Mat srcImage = imread("../Data/dota_pa.jpg");
	Mat logoImage = imread("../Data/dota_logo.jpg");

	cout << "原图 通道数: " << srcImage.channels() << "  类型:"<< srcImage.type() << endl;
	cout << "logo 通道数: " << logoImage.channels() << "  类型:" << logoImage.type() << endl;

	//2、实现贴图
	MixImage(srcImage, logoImage, Point(200, 250));

	//3、显示图片
	imshow("srcImage", srcImage);
	waitKey(0);
	destroyAllWindows();

	return 1;
}

Opencv C++ 图像混合(贴图)_第1张图片

你可能感兴趣的:(OpenCV-C++)