# 计算机视觉_OpenCV开源库讲解（第二节：图像的算术操作）

2、减：subtract(src1, src2, dst);

3、乘：multiply(src1, src2, dst);

4、除：divide(src1, src2, dst);

#include “opencv2\opencv.hpp”
#include

using namespace std;
using namespace cv;

int main(int argv, char** argc)
{

if (src1.empty()||src2.empty())
{
printf("Could not load image src1 or src2...\n");
return -1;
}

Mat subResult = Mat::zeros(src1.size(), src1.type());
subtract(src1, src2, subResult);
imshow("subResult", subResult);

Mat mulResult = Mat::zeros(src1.size(), src1.type());
multiply(src1, src2, mulResult);
imshow("mulResult", mulResult);

Mat divResult = Mat::zeros(src1.size(), src1.type());
divide(src1, src2, divResult);
imshow("divResult", divResult);

int b1 = 0, g1 = 0, r1 = 0;
int b2 = 0, g2 = 0, r2 = 0;
int b = 0, g = 0, r = 0;
int height = src1.rows;
int width = src1.cols;

Mat result = Mat::zeros(src1.size(), src1.type());
for (int row = 0; row < height; row++){
for (int col = 0; col < width; col++) {
b1 = src1.at<Vec3b>(row, col)[0];
g1 = src1.at<Vec3b>(row, col)[1];
r1 = src1.at<Vec3b>(row, col)[2];

b2 = src2.at<Vec3b>(row, col)[0];
g2 = src2.at<Vec3b>(row, col)[1];
r2 = src2.at<Vec3b>(row, col)[2];
result .at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b1 * b2);
result .at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g1 * g2);
result .at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r1 * r2);
}
}
imshow("saturateResult", saturateResult);

waitKey(0);
return 0;


}