OpenCV 实现两个图形的重叠显示


Calculates the weighted sum of two arrays.
    C++: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
    Python: cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
    C: void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst)
    Python: cv.AddWeighted(src1, alpha, src2, beta, gamma, dst) → None
    src1 – first input array.
    alpha – weight of the first array elements.
    src2 – second input array of the same size and channel number as src1.
    beta – weight of the second array elements.
    dst – output array that has the same size and number of channels as the input arrays.
    gamma – scalar added to each sum.
    dtype – optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().
    The function addWeighted calculates the weighted sum of two arrays as follows:

    where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each channel is processed independently.
    The function can be replaced with a matrix expression:

dst = src1*alpha + src2*beta + gamma;

    Note Saturation is not applied when the output array has the depth CV_32S. You may even get result of an incorrect sign in the case of overflow.


#include <opencv.hpp>
#include <iostream>

const double alpha_1 = 0.5;
const double alpha_2 = 0.5;

int main(int argc, char** argv)
    cv::Mat img = cv::imread("C:\\Users\\96921\\OneDrive\\Pictures\\Camera Roll\\001.jpg", -1);
    cv::Mat dest = cv::imread("C:\\Users\\96921\\OneDrive\\Pictures\\Camera Roll\\002.jpg", -1);
    cv::Mat tri = cv::imread("C:\\Users\\96921\\OneDrive\\Pictures\\Camera Roll\\psb.png", -1);

    std::cout << img.size() << std::endl;
    std::cout << dest.size() << std::endl;
    std::cout << tri.size() << std::endl;

    cv::addWeighted(dest, 0.5, img, 0.5, 0, dest, -1);

    for (int i = 0; i < img.size().height; ++i)
        for (int j = 0; j < img.size().width; ++j)
            cv::Vec3b it =<cv::Vec3b>(i, j) * alpha_1;
            it +=<cv::Vec3b>(i + 50, j + 50) * alpha_2;
  <cv::Vec3b>(i + 50, j + 50) = it;

    cv::namedWindow("Function", cv::WINDOW_AUTOSIZE);
    cv::imshow("Function", dest);
    cv::namedWindow("Loop", cv::WINDOW_AUTOSIZE);
    cv::imshow("Loop", tri);

    return 0;