OpenCV image pixel operation operations

Detailed explanation of addition operation

Pixel addition

Addition, subtraction, multiplication and division

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
    
    
    Mat image = imread("image.jpg");

    if (image.empty()) {
    
    
        std::cout << "无法加载图像" << std::endl;
        return -1;
    }

    // 加法变换
    Mat addResult;
    add(image, Scalar(50, 50, 50), addResult); // 将像素值增加50

    // 减法变换
    Mat subtractResult;
    subtract(image, Scalar(50, 50, 50), subtractResult); // 将像素值减去50

    // 乘法变换
    Mat multiplyResult;
    multiply(image, Scalar(0.5, 0.5, 0.5), multiplyResult); // 将像素值乘以0.5

    // 除法变换
    Mat divideResult;
    divide(image, Scalar(2.0, 2.0, 2.0), divideResult); // 将像素值除以2.0

    imshow("Original Image", image);
    imshow("Addition Result", addResult);
    imshow("Subtraction Result", subtractResult);
    imshow("Multiplication Result", multiplyResult);
    imshow("Division Result", divideResult);

    waitKey(0);

    return 0;
}

In the above example, an image is loaded and then transformed by addition, subtraction, multiplication and division respectively. Additive and subtractive transformations use a scalar to specify the transformation value, and multiplication and division transformations use a scalar to specify the scaling factor.

Note that for additive and subtractive transformations, you can use positive or negative scalar values. For multiplication and division transformations, the scalar value must be a floating point number greater than 0.


I recommend a Lingsheng Academy project class. I personally think the teacher taught it well. I would like to share it with you:
Lingsheng Platinum Learning Card (including infrastructure/high-performance storage/golang cloud native/audio and video/Linux kernel)
https://xxetb.xet .tech/s/VsFMs

Guess you like

Origin blog.csdn.net/qq_40135848/article/details/132865313