2019-06-23(day005)——python中的新知识: - cv.add() - cv.subtract() - cv.multiply() - cv.divide()

python

import cv2 as cv
import numpy as np
def function():
    li_logo = cv.imread("../images/LinuxLogo.jpg")
    win_logo = cv.imread("../images/WindowsLogo.jpg")
    cv.imshow("li_logo", li_logo)
    cv.imshow("win_logo", win_logo)
    add_result = np.zeros(li_logo.shape, li_logo.dtype)
    cv.add(li_logo, win_logo, add_result)
    cv.imshow("add_result", add_result)
    
    sub_result = np.zeros(li_logo.shape, li_logo.dtype)
    cv.subtract(li_logo, win_logo, sub_result)
    cv.imshow("sub_result", sub_result)
    
    multi_result = np.zeros(li_logo.shape, li_logo.dtype)
    cv.multiply(li_logo, win_logo, multi_result)
    cv.imshow("multi_result", multi_result)
    
    divi_result = np.zeros(li_logo.shape, li_logo.dtype)
    cv.divide(li_logo, win_logo, divi_result)
    cv.imshow("divi_result", divi_result)
    
function()
cv.waitKey(0)
cv.destroyAllWindows()

python中的新知识:

  • cv.add()
  • cv.subtract()
  • cv.multiply()
  • cv.divide()

c++

#include "all.h"
using namespace std;
using namespace cv;

void MyClass::day005() {
    Mat linuxLogo = read(PATH + "images\\LinuxLogo.jpg");
    Mat windowsLogo = read(PATH + "images\\WindowsLogo.jpg");
    if (linuxLogo.empty())
    {
        printf("can't read linuxLogo\n");
        return;
    }
    if (windowsLogo.empty())
    {
        printf("can't read windowsLogo\n");
        return;
    }
    int b, b1 = 0; int g, g1 = 0; int r, r1 = 0;
    int b2 = 0; int g2 = 0; int r2 = 0;
    Mat result = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    for (int i = 0; i < linuxLogo.rows; i++)
    {
        uchar *linuxRow = linuxLogo.ptr(i);
        uchar *windowsRow = windowsLogo.ptr(i);
        uchar *resultRow = result.ptr(i);
        for (int j = 0; j < linuxLogo.cols; j++) {
            b1 = *linuxRow++;
            g1 = *linuxRow++;
            r1 = *linuxRow++;

            b2 = *windowsRow++;
            g2 = *windowsRow++;
            r2 = *windowsRow++;

            b = saturate_cast(b1 + b2);
            g = saturate_cast(g1 + g2);
            r = saturate_cast(r1 + r2);

            *resultRow++ = b;
            *resultRow++ = g;
            *resultRow++ = r;
        }
    }
    Mat resultDownIndex = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    for (int i = 0; i < linuxLogo.rows; i++) {
        for (int j = 0; j < linuxLogo.cols; j++) {
            b1 = linuxLogo.at(i, j)[0];
            g1 = linuxLogo.at(i, j)[1];
            r1 = linuxLogo.at(i, j)[2];

            b2 = windowsLogo.at(i, j)[0];
            g2 = windowsLogo.at(i, j)[1];
            r2 = windowsLogo.at(i, j)[2];

            resultDownIndex.at(i, j)[0] = saturate_cast(b1 + b2);
            resultDownIndex.at(i, j)[1] = saturate_cast(g1 + g2);
            resultDownIndex.at(i, j)[2] = saturate_cast(r1 + r2);

        }
    }
    imshow("addResultPtr", result);
    imshow("addResultDownIndex", resultDownIndex);
    Mat addResult = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    add(linuxLogo, windowsLogo, addResult);
    imshow("addFunctionResult", addResult);
    Mat subtractResult = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    subtract(linuxLogo, windowsLogo, subtractResult);
    imshow("subtractResult", subtractResult);
    Mat multiplyResult = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    multiply(linuxLogo, windowsLogo, multiplyResult);
    imshow("multiplyResult", multiplyResult);
    Mat divideResult = Mat::zeros(linuxLogo.size(), linuxLogo.type());
    divide(linuxLogo, windowsLogo, divideResult);
    imshow("divideResult", divideResult);
    waitKey(0);
}

c++中的新知识

  • add()
  • subtract()
  • multiply()
  • divide()
  • Mat::empty()
  • cv::saturate_cast();起到溢出保护的作用

你可能感兴趣的:(2019-06-23(day005)——python中的新知识: - cv.add() - cv.subtract() - cv.multiply() - cv.divide())