OpenCv 005---图像像素的算术操作

 

图像像素的四种算术运算:

1、加:add(src1, src2, addResult);

2、减:subtract(src1, src2, subResult);

3、乘:multiply(src1, src2, mulResult);

4、除:divide(src1, src2, divResult);

 

还可以使用以下方式进行:像素之间的 加、减、乘法

saturateResult.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b1 * b2);

saturateResult.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g1 * g2);

saturateResult.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r1 * r2);

#include "opencv2\opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argv, char** argc)
{
    Mat src1 = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\Blender_Suzanne1.jpg");
    Mat src2 = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\Blender_Suzanne2.jpg");

    if (src1.empty()||src2.empty())
    {
        printf("Could not load image src1 or src2...\n");
        return -1;
    }
    Mat addResult = Mat::zeros(src1.size(), src1.type());
    add(src1, src2, addResult);
    imshow("addResult", addResult);

    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 saturateResult = 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];
            saturateResult.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b1 * b2);
            saturateResult.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g1 * g2);
            saturateResult.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r1 * r2);
        }
    }
    imshow("saturateResult", saturateResult);
    
    waitKey(0);
    return 0;
}

src1

src2

运行结果:

相加:

相减:

 

 相乘:

相除:

saturateResult:(相乘)

 

posted @ 2019-06-26 21:17  鸡鸣昧旦  阅读(479)  评论(0编辑  收藏  举报