c++ code

--------

#include <vector>

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

int main()

{

       // read images

        cv::Mat image1;

        cv::Mat image= cv::imread("children_of_men.jpg");

        cv::Mat logo= cv::imread("children_of_men_small.jpg");

        // define image ROI

        cv::Mat imageROI;

        imageROI = image(cv::Rect(0,0, logo.cols,logo.rows));

 

        // add logo to image

        cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);

        // show result

        cv::namedWindow("with logo");

        cv::imshow("with logo",image);

        cv::waitKey(0);

        cv::destroyAllWindows();

        return 0;

}

#include <vector>

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

int main()

{

       // read images

        cv::Mat image1;

        cv::Mat image= cv::imread("children_of_men.jpg");

        cv::Mat logo= cv::imread("children_of_men_small.jpg");

        // define image ROI

        cv::Mat imageROI;

        imageROI= image(cv::Rect(0,0,logo.cols,logo.rows));

 

        // add logo to image

        cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);

        // show result

        cv::namedWindow("with logo");

        cv::imshow("with logo",image);

        // read images

        image= cv::imread("children_of_men.jpg");

        logo= cv::imread("children_of_men_small.jpg");

        // define ROI

        imageROI= image(cv::Rect(0,0,logo.cols,logo.rows));

        // load the mask (must be gray-level)

        cv::Mat mask= cv::imread("children_of_men_small.jpg",0);

        // copy to ROI with mask

        logo.copyTo(imageROI,mask);

        // show result

        cv::namedWindow("with logo 2");

        cv::imshow("with logo 2",image);

        // read images

        image= cv::imread("children_of_men.jpg");

        logo= cv::imread("children_of_men_small.jpg");

        // define ROI

        imageROI= image(cv::Rect(0,0,logo.cols,logo.rows));

        // load the mask (must be gray-level)

        cv::Mat mask= cv::imread("children_of_men_small.jpg",0);

        // copy to ROI with mask

        logo.copyTo(imageROI,mask);

        // show result

        cv::namedWindow("with logo 2");

        cv::imshow("with logo 2",image);

        // read images

        logo= cv::imread("children_of_men_small.jpg",0);

        image1= cv::imread("children_of_men.jpg");

        // split 3-channel image into 3 1-channel images

        std::vector<cv::Mat> channels;

        cv::split(image1,channels);

        imageROI= channels.at(2);

        cv::addWeighted(imageROI(cv::Rect(0,0,logo.cols,logo.rows)),1.0,

                                logo,0.5,0.,imageROI(cv::Rect(0,0,logo.cols,logo.rows)));

        cv::merge(channels,image1);

        cv::namedWindow("with logo 3");

        cv::imshow("with logo 3",image1);

        cv::waitKey(0);

        cv::destroyAllWindows();

        return 0;

}

python code

----------------

import cv2

# Read images

img1 = cv2.imread('children_of_men.jpg')

img2 = cv2.imread('children_of_men_small.jpg')

# Define image ROI

rows,cols,channels = img2.shape

roi = img1[0:rows, 0:cols ]

# Copy to ROI with mask

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

mask_inv = cv2.bitwise_not(mask)

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Define ROI vis a vis mask

img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Show result

dst = cv2.add(img1_bg,img2_fg)

img1[0:rows, 0:cols ] = dst

cv2.imshow('with logo',img1)

cv2.waitKey(0)

cv2.destroyAllWindows()