三 (3.1 core 模块) 图像混合和图像运算(加减乘除)

2018-11-05  本文已影响0人  交大小丑

使用OpenCV对两幅图像求和(求混合(blending)) — OpenCV 2.3.2 documentation http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/adding_images/adding_images.html#adding-images

图像混合:

void addWeighted(InputArray src1, 
                 double alpha,
                 InputArray src2,
                 double beta,
                 double gamma,
                 OutputArray dst, 
                 int dtype = -1);

实例

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

using namespace cv;

int main( int argc, char** argv )
{
 double alpha = 0.5; double beta; double input;

    Mat src1, src2, dst;

    /// Ask the user enter alpha
    std::cout << " Simple Linear Blender " << std::endl;
    std::cout << "-----------------------" << std::endl;
    std::cout << "* Enter alpha [0-1]: ";
    std::cin >> input;

    /// We use the alpha provided by the user iff it is between 0 and 1
    if (alpha >= 0 && alpha <= 1)
    {
        alpha = input;
    }

    /// Read image ( same size, same type )
    src1 = imread("test.jpg");
    resize(src1, src1, Size(1000, 750));
    src2 = imread("test2.jpg");
    resize(src2, src2, Size(1000, 750));
    if (!src1.data) { printf("Error loading src1 \n");  }
    if (!src2.data) { printf("Error loading src2 \n");  }

    /// Create Windows
    namedWindow("Linear Blend", 1);

    beta = (1.0 - alpha);
    addWeighted(src1, alpha, src2, beta, 0.0, dst);

    imshow("Linear Blend", dst);

    waitKey(0);
 return 0;
}

openCV之图像混合 - 大道至简 - CSDN博客 https://blog.csdn.net/weixin_37720172/article/details/72851497

图像运算

常用函数汇总

//------------两幅图像之间的基本运算----------------------------------
void add(InputArray src1, InputArray src2, OutputArray dst,
                  InputArray mask=noArray(), int dtype=-1);

void subtract(InputArray src1, InputArray src2, OutputArray dst,
                       InputArray mask=noArray(), int dtype=-1);

void multiply(InputArray src1, InputArray src2,
                       OutputArray dst, double scale=1, int dtype=-1);

void divide(InputArray src1, InputArray src2, OutputArray dst,
                     double scale=1, int dtype=-1);

void divide(double scale, InputArray src2,
                     OutputArray dst, int dtype=-1);

void scaleAdd(InputArray src1, double alpha, InputArray src2, OutputArray dst);

void addWeighted(InputArray src1, double alpha, InputArray src2,
                          double beta, double gamma, OutputArray dst, int dtype=-1);
//-------------位运算----------------
void bitwise_and(InputArray src1, InputArray src2,
                          OutputArray dst, InputArray mask=noArray());

void bitwise_or(InputArray src1, InputArray src2,
                         OutputArray dst, InputArray mask=noArray());

void bitwise_xor(InputArray src1, InputArray src2,
                          OutputArray dst, InputArray mask=noArray());

void bitwise_not(InputArray src, OutputArray dst,
                          InputArray mask=noArray());

//-------------求两幅图像像素的差的绝对值----------------
void absdiff(InputArray src1, InputArray src2, OutputArray dst);
//-------------对单幅图像进行操作的,比如对每个像素的值取平方、平方根、对数等----------------
void sqrt(InputArray src, OutputArray dst);
void pow(InputArray src, double power, OutputArray dst);
void exp(InputArray src, OutputArray dst);
void log(InputArray src, OutputArray dst);

函数详解

add
 void add(InputArray src1, 
          InputArray src2, 
          OutputArray dst,
          InputArray mask = noArray(),
          int dtype = -1);
multiply
void multiply(InputArray src1, 
              InputArray src2,
              OutputArray dst,
              double scale = 1,
              int dtype = -1);
max
CV_EXPORTS MatExpr max(const Mat& a, const Mat& b);
CV_EXPORTS MatExpr max(const Mat& a, double s);
CV_EXPORTS MatExpr max(double s, const Mat& a);
min
CV_EXPORTS MatExpr min(const Mat& a, const Mat& b);
CV_EXPORTS MatExpr min(const Mat& a, double s);
CV_EXPORTS MatExpr min(double s, const Mat& a);

实例

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

using namespace cv;

int main( int argc, char** argv )
{
 Mat src1, src2, dst;
    /// We use the alpha provided by the user iff it is between 0 and 1
    
    /// Read image ( same size, same type )
    src1 = imread("test.jpg");
    resize(src1, src1, Size(1000, 750));
    src2 = imread("test2.jpg");
    resize(src2, src2, Size(1000, 750));
    if (!src1.data) { printf("Error loading src1 \n"); }
    if (!src2.data) { printf("Error loading src2 \n"); }

    /// Create Windows
    namedWindow("Linear Blend", 1);

    //add(src1, src2,  dst, Mat()); //各个像素相加
    //multiply(src1, src2,  dst );  //各个像素相乘
    //dst = max(src1, src2); //各个像素点位置取较大的
    dst = min(src1, src2); //各个像素点位置取较小的


    imshow("Linear Blend", dst );

    waitKey(0);
 return 0;
}

OpenCV 学习(图像的基本运算) - Ivan 的专栏 - CSDN博客 https://blog.csdn.net/liyuanbhu/article/details/48916349
opencv学习之路(8)、基本图像运算——加减与或 - 进击的小猴子 - 博客园 https://www.cnblogs.com/little-monkey/p/7202628.html
OpenCV矩阵运算 ._chease新浪博客 http://blog.sina.com.cn/s/blog_afe2af380101bqhz.html

上一篇下一篇

猜你喜欢

热点阅读