openCV——ROI区域图像叠加&初级图像混合

主要是ROI和addWeighted

Posted by XushengLee on July 19, 2016

openCV的ROI(region of interest), Rect(), addWeighted等

惯例,来一个传送门 浅墨的csdn 详细声明说明等,请各位膜法师自行传送.

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;
using namespace std;

bool ROI_AddImage(Mat srcImage1,Mat logoImage);
bool LinearBlending(Mat img1,Mat img2,double alphaValue);
bool ROI_LinearBlending(Mat img,Mat logo);

//----------------------------------【ROI_AddImage( )函数】----------------------------------
// 函数名:ROI_AddImage()
//     描述:利用感兴趣区域ROI实现图像叠加
//----------------------------------------------------------------------------------------------
bool ROI_AddImage(Mat srcImage1,Mat logoImage )
{
    if(!srcImage1.data ) { printf("failed to load srcImage1 ---ROI_AddImage \n"); return false; }
    if(!logoImage.data ) { printf("failed to load logoImage ---ROI_AddImage \n"); return false; }
    //【2】定义一个Mat类型并给其设定ROI区域,此区域其实还是在srcImage上.
    Mat imageROI= srcImage1(Rect(200,250,logoImage.cols,logoImage.rows));
    //【3】加载掩模(必须是灰度图)
    Mat mask= imread("dota_logo.jpg",0);
    //【4】将掩膜拷贝到ROI,改变其实发生在srcImage上.
    logoImage.copyTo(imageROI,mask);
    //【5】显示结果
    namedWindow("ROI_AddImage");
    imshow("ROI_AddImage",srcImage1);
    return true;
}


//---------------------------------【LinearBlending()函数】-------------------------------------
// 函数名:LinearBlending()
// 描述:利用cv::addWeighted()函数实现图像线性混合
//--------------------------------------------------------------------------------------------
bool LinearBlending(Mat img1,Mat img2,double alphaValue,double betaValue )
{
    //【0】定义一些局部变量
    Mat dstImage;
    if(!img1.data ) { printf("failed to load img1 \n"); return false; }
    if(!img2.data ) { printf("failed to load img2 \n"); return false; }
    //【2】进行图像混合加权操作
    //betaValue= ( 1.0 - alphaValue );
    addWeighted(img1, alphaValue, img2, betaValue, 0.0, dstImage);

    namedWindow("LinearBlending", 1);
    imshow("LinearBlending", dstImage );

    return true;

}

//---------------------------------【ROI_LinearBlending()】-------------------------------------
// 函数名:ROI_LinearBlending()
// 描述:线性混合实现函数,指定区域线性图像混合.利用cv::addWeighted()函数结合定义
//                     感兴趣区域ROI,实现自定义区域的线性混合
//--------------------------------------------------------------------------------------------
bool ROI_LinearBlending(Mat img,Mat logo, double alphaValue,double betaValue ,int x,int y )
{
   if(!img.data ) { printf("failed to load img ---ROI_LinearBlending \n"); return false; }
   if(!logo.data ) { printf("failed to load img ---ROI_LinearBlending \n"); return false; }
   //【2】定义一个Mat类型并给其设定ROI区域,ROI区域还是在本来的img上.
   Mat imageROI;
   //方法一
   imageROI=img(Rect(x,y,logo.cols,logo.rows));
   //方法二
   //imageROI=srcImage4(Range(y,y+logoImage.rows),Range(x,x+logoImage.cols));
   //【3】将logo加到原图上,改变是发生在iamgeROI上的,也就是img上.
    addWeighted(imageROI,alphaValue,logo,betaValue,0.0,imageROI);
    //【4】显示结果
    namedWindow("finalResult:ROI_LinearBlending");
    imshow("finalResult:ROI_LinearBlending",img);

    return true;
}

int main(){
  
    Mat img1 = imread("/Users/mac/Pictures/pixiv-dasktop/tattoo.jpg");
    Mat img2 = imread("/Users/mac/Pictures/pixiv-djsz/whiteKing1.jpg");
    Mat img3 = imread("/Users/mac/Pictures/pixiv-djsz/doge1.jpg");
    Mat logo = imread("/Users/mac/Pictures/logo/logo.jpg");
    Mat img4 = imread("/Users/mac/Pictures/pixiv-dasktop/noname.jpg");
//    if(ROI_AddImage(img1,logo ))
//    {
//        cout<<endl<<"first";
//    }
//    waitKey(2000);

//    if(LinearBlending(img2,img3,0.5,0.5 ))
//    {
//        cout<<endl<<"second";
//    }
//    waitKey(2000);


    if(ROI_LinearBlending(img1,logo,0.5,0.5,100,200 ))
    {
        cout<<endl<<"final";
    }
    namedWindow("origin---LInearBlending", 1);
    imshow("origin---LInearBlending", img1 );
    waitKey(0);
    return 0;
}

上面代码中,main( ){ }里面的三个调用应该分开使用,否则会有意想不到的瑕疵. 原因在上面函数声明的注释里_(:зゝ∠)_