OPENCV学习笔记2-6_简单的图像运算

  Images can be combined(结合) in different ways. Since they are regular(一般) matrices, they can be added, subtracted, multiplied, or divided.

1. Example analysis 

  可以使用cv::add函数来实现相加功能。如想得到加权和,可使用更精确的cv::addWeighted函数。

#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;

int main()
{
    Mat image1;
    Mat image2;

    image1 = imread("test.jpg");
    image2 = imread("test_b.jpg");

    namedWindow("Image 1");
    imshow("Image 1", image1);
    namedWindow("Image 2");
    imshow("Image 2", image2);

    Mat result;
    /*
    add two images:
    cvAddWeighted( const CvArr* src1, double alpha,  const CvArr* src2, double beta,  double gamma, CvArr* dst );
    dst(I)=src1(I)*alpha+src2(I)*beta+gamma
    */
    addWeighted(image1, 0.7, image2, 0.5, 0., result);
    namedWindow("result");
    imshow("result", result);

    waitKey();
    return 0;
}

 1.2 Overloaded image operators(重载图像运算符)

  Most arithmetic functions(运算函数) have their corresponding operator(相应操作) overloaded in OpenCV 2. Consequently(因此), the call to cv::addWeighted can be written as follows:

  result = 0.7*image1 + 0.9*image2;

  The preceding(前面的) code is a more compact form(紧凑的形式) that is also easier to read.

1.3 Splitting the image channels(分割图像通道)

  use the cv::split function that will copy the three channels of a color image into three distinct(不同) cv::Mat instances(实例). Suppose we want to add our rain image to the blue channel only. The following is how we would proceed:

  // create vector of 3 images

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

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

  cv::split(image1, planes);

  // add to blue channel

  planes[0] += image2;

  // merge the 3 1-channel images into 1 3-channel image

  cv::merge(planes, result);

 

转载于:https://www.cnblogs.com/yunfung/p/7560901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值