小白opencv学习篇(2)图像混合

简介

图像混合是指二个图像的线性混合。就像大家看电影过程中,在同一时间轴上出现二个视频的叠加,是不是感觉很cool呢,那就让小编带你们了解一下吧,嘿嘿嘿。

基本原理

g ( x ) = ( 1 σ ) f 0 ( x ) + σ f 1 ( x ) ( 0 < σ < 1 ) g(x)=(1-\sigma)f_0(x)+\sigma f_1(x)(0<\sigma<1)
f 0 a n d f 1 f_0andf_1 是源图像的像素点, g g 则是混合后像素点值。可以看到二个图像的像素值是线性混合相加,当 σ \sigma 在0到1之间取值时,二个图像所占的比例也不同,这就可以做出了二个图像混合的效果啦,是不是很简单呢。

相关API

addWeighted官方描述如下,相信大家摸索一下就会了,也很简单啦。

/** @brief Scales, calculates absolute values, and converts the result to 8-bit.

On each element of the input array, the function convertScaleAbs
performs three operations sequentially: scaling, taking an absolute
value, conversion to an unsigned 8-bit type:
\f[\texttt{dst} (I)= \texttt{saturate\_cast<uchar>} (| \texttt{src} (I)* \texttt{alpha} +  \texttt{beta} |)\f]
In case of multi-channel arrays, the function processes each channel
independently. When the output is not 8-bit, the operation can be
emulated by calling the Mat::convertTo method (or by using matrix
expressions) and then by calculating an absolute value of the result.
For example:
@code{.cpp}
    Mat_<float> A(30,30);
    randu(A, Scalar(-100), Scalar(100));
    Mat_<float> B = A*5 + 3;
    B = abs(B);
    // Mat_<float> B = abs(A*5+3) will also do the job,
    // but it will allocate a temporary matrix
@endcode
@param src input array.
@param dst output array.
@param alpha optional scale factor.
@param beta optional delta added to the scaled values.
@sa  Mat::convertTo, cv::abs(const Mat&)
*/

示例

addWeighted(src_one, 0.5, src_two, 0.5, 0.0, dst);

这里把二个源图像按五五开的比例线性混合,效果如下在这里插入图片描述

结语

各位是不是也学会了呢,赶快试试吧!!

猜你喜欢

转载自blog.csdn.net/qq_45908056/article/details/106826762