Opencv achieves the combination of two images, and opencv implements the image

Source: Internet
Author: User
Tags draw box

Opencv achieves the combination of two images, and opencv implements the image
Introduction

This article uses ROI and addWeighted on opencv to mix two images.
ROI
First, let's take a look at the implementation of ROI. In "Draw box _ opencv (1) () where the image needs to be located", we have already discussed the use of ROI. Here we will look at the Code directly.
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <math.h>#include <string.h>#include <opencv/cv.h>#include <stdio.h> int main(int argc,char *argv[]){    cv::Mat src1 = cv::imread(argv[1]);    cv::Mat src2 = cv::imread(argv[2]);     cv::Mat imageROI= src1(cv::Rect(200,250,src2.cols,src2.rows));    src2.copyTo(imageROI);     cv::namedWindow("dst");    cv::imshow("dst",src1);    cv::waitKey(0);    return 0;}
The code first opens two images, and then adds the second image to the position specified by the first image. The effect is as follows.
The first image:


The second image:


After mixing:


AddWeighted
Before talking about this method, we first need to look at a formula: g (x) = (1-a) f0 (x) + af1 (x) which means: for two images (f0 (x) and f1 (x) or two videos (same as (f0 (x) and f1 (x )) generate the cross-dissolve effect on time. Core functions: void addWeighted (InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype =-1); src1: the first image to be mixed. Alpha: the weight of the first image. Src2: The second image to be mixed. Beta: Weight of the second image. Gamma: a scalar value that is added to the sum of weights. See the following formula: dst = src1 [I] * alpha + src2 [I] * beta + gamma; dst: The target image after mixing. Dtype: Optional depth of the output array.
The mixed code using this method is as follows:
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <math.h>#include <string.h>#include <opencv/cv.h>#include <stdio.h> int main(int argc,char *argv[]){    cv::Mat src1 = cv::imread(argv[1]);    cv::Mat src2 = cv::imread(argv[2]);    cv::Mat dst;     double alpha = 0.5; double beta; double input;     beta = (1.0 - alpha);    addWeighted(src1, alpha, src2, beta, 0.0, dst);     cv::namedWindow("dst");    cv::imshow("dst",dst);    cv::waitKey(0);    return 0;}
Note: The addWeighted image must have the same size. This code is used to mix weights of 0.5. The mixing effect is as follows:

Two source images used for mixing:

 

The following is a mix:


ROI and addWeighted work together
Finally, this method is used to add an image to the specified position of another image by weight. The Code is as follows:
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <math.h>#include <string.h>#include <opencv/cv.h>#include <stdio.h> int main(int argc,char *argv[]){    cv::Mat src1 = cv::imread(argv[1]);    cv::Mat src2 = cv::imread(argv[2]);    cv::Mat dst;     double alpha = 0.5; double beta; double input;     cv::Mat imageROI = src1(cv::Rect(200,250,src2.cols,src2.rows));     beta = (1.0 - alpha);    addWeighted(imageROI, alpha, src2, beta, 0.0, imageROI);     cv::namedWindow("dst");    cv::imshow("dst",src1);    cv::waitKey(0);    return 0;}
In the code, the ROI is used at the position of the first image (200,250), and the rectangle of the second image is taken out. Then, addWeighted is used to mix the ROI with the data of the second image with a 0.5 weight. As shown in the end, both original images were previously used and are displayed directly here:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.