目录
准备两张图片,第二张图片较小,且是灰度图片
程序解读
运行结果:
OpenCV-ROI区域图像叠加
准备两张图片,第二张图片较小,且是灰度图片
程序解读
//-------------------------------【头文件、命名空间】-------------------------------------
//
//--------------------------------------------------------------------------
#include <opencv2\opencv.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//-------------------------------【全局函数声明】--------------------------------------
//
//--------------------------------------------------------------------------
bool ROI_AddImage();
int main()
{
system("color 5E");
if (ROI_AddImage())
{
cout << " ROI_AddImage " << "运行成功!" << endl;
}
waitKey(0);
return 0;
}
//----------------------------------【ROI_AddImage( )函数】----------------------------------
// 函数名:ROI_AddImage()
// 描述:利用感兴趣区域ROI实现图像叠加
//----------------------------------------------------------------------------------------------
bool ROI_AddImage()
{
//【1】读入图像
Mat srcImage1 = imread("org.jpg");
Mat logoImage = imread("logo.jpg");
if (!srcImage1.data) { printf("检查,读取srcImage1错误~! \n"); return false; }
if (!logoImage.data) { printf("检查,读取logoImage错误~! \n"); return false; }
cout << srcImage1.rows << " srcImage1 row" << endl;
cout << srcImage1.cols << " srcImage1 col" << endl;
cout << logoImage.rows << " logoImage row" << endl;
cout << logoImage.cols << " logoImage col" << endl;
//【2】定义一个Mat类型并给其设定ROI区域
//方法一:左上角坐标+宽+高,这里需要根据自己图片大小修改,否则会索引错误
//Mat imageROI = srcImage1(Rect(0, 250, logoImage.cols, logoImage.rows));
//方法二:直接取范围
Mat imageROI = srcImage1(Range(0, 0 + logoImage.rows), Range(0, 0 + logoImage.cols));
//【3】加载掩模(必须是灰度图)
Mat mask = imread("logo.jpg", 0);
//【4】将掩膜拷贝到ROI
logoImage.copyTo(imageROI, mask);
//【5】显示结果
namedWindow("<1>利用ROI实现图像叠加示例窗口");
imshow("<1>利用ROI实现图像叠加示例窗口", srcImage1);
return true;
}