Fork me on GitHub

opencv之图像的叠加与混合

ROI区域

cv::Mat image_roi = image(cv::Rect(x, y, width, height));
cv::Mat image_roi = image(cv::Range(y, y + height), cv::Range(x, x + width));

线性混合

void addWeighted(InputArray src1, double alpha, InputArray src2,
                 double beta, double gamma, OutputArray dst, int dtype = -1);

注: dst = src1[I] * alpha + src2[I] * beta + gamma

实例

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

bool  ROI_AddImage()
{
    Mat srcImage1= imread("/home/test/dota_pa.jpg");
    Mat logoImage= imread("/home/test/dota_logo.jpg");
    if( !srcImage1.data ) { printf("srcImage1 dont exist! \n"); return false; }
    if( !logoImage.data ) { printf("logoImage dont exist! \n"); return false; }

    // create roi
    Mat imageROI= srcImage1(Rect(200,250,logoImage.cols,logoImage.rows));
    // create mask
    Mat mask= imread("dota_logo.jpg",0);
    
    logoImage.copyTo(imageROI,mask);
    namedWindow("ROI_AddImage");
    imshow("ROI_AddImage",srcImage1);

    return true;
}

bool  LinearBlending()
{
	double alphaValue = 0.5; 
	double betaValue = 1.0 - alphaValue;
	Mat srcImage2, srcImage3, dstImage;
	srcImage2 = imread("/home/test/mogu.jpg");
	srcImage3 = imread("/home/test/rain.jpg");
	if( !srcImage2.data ) { printf("srcImage2 dont exist! \n"); return false; }
	if( !srcImage3.data ) { printf("srcImage3 dont exist! \n"); return false; }

  // line blending
	addWeighted( srcImage2, alphaValue, srcImage3, betaValue, 0.0, dstImage);
	imshow( "raw_image", srcImage2 );
	imshow( "LinearBlending", dstImage );

	return true;
}

bool  ROI_LinearBlending()
{
	Mat srcImage4= imread("/home/test/dota_pa.jpg",1);
	Mat logoImage= imread("/home/test/dota_logo.jpg");
	if( !srcImage4.data ) { printf("srcImage4 dont exist! \n"); return false; }
	if( !logoImage.data ) { printf("logoImage dont exist! \n"); return false; }

	Mat imageROI = srcImage4(Rect(200,250,logoImage.cols,logoImage.rows));
	//imageROI= srcImage4(Range(250,250+logoImage.rows),Range(200,200+logoImage.cols));
    
	addWeighted(imageROI,0.5,logoImage,0.3,0.,imageROI);
	imshow("ROI_LinearBlending",srcImage4);

	return true;
}

int main(   )
{
	if(ROI_AddImage( )&& LinearBlending( )&&ROI_LinearBlending( ))
	{
		cout<<endl<<"yes!";
	}

	waitKey(0);
	return 0;
}

注: 使用roi和mask方式,添加图标

注:使用addWeighted()混合两张图像。

注:也可以使用roi和addWeighted()方式,添加图标

Mat imageROI = srcImage4(Rect(200,250,logoImage.cols,logoImage.rows));
addWeighted(imageROI,0,logoImage,1,0.,imageROI);
imshow("ROI_LinearBlending",srcImage4);
posted @ 2021-06-27 11:14  chrislzy  阅读(359)  评论(0编辑  收藏  举报