C++ opencv RGB三通道提升亮度

#include
#include
#include
using namespace std;
using namespace cv;

//函数adjustBrightness用于图片增加亮度
void adjustBrightness(cv::Mat& image, int targetBrightness) {
    // 获取图像的通道数
    int channels = image.channels();
    // 计算调整亮度的因子
    float factor = 1.0f;
    if (targetBrightness > 0) {
        factor = static_cast(targetBrightness) / 255.0f;
    }
    else if (targetBrightness < 0) {
        factor = 255.0f / static_cast(255 - std::abs(targetBrightness));
    }
    // 遍历图像的每个像素
    for (int i = 0; i < image.rows; ++i) {
        for (int j = 0; j < image.cols; ++j) {
            // 获取像素值
            cv::Vec3b& pixel = image.at(i, j);

            // 调整亮度
            for (int c = 0; c < channels; ++c) {
                if (targetBrightness > 0) {
                    pixel[c] = cv::saturate_cast(pixel[c] * factor);
                }
                else if (targetBrightness < 0) {
                    pixel[c] = cv::saturate_cast((pixel[c] - 255) * factor + 255);
                }
            }
        }
    }
}
void saveimage(std::string file, std::string savefile, int targetBrightness = 400) {
    cv::Mat img = imread(file);
    adjustBrightness(img, targetBrightness);
    imwrite(savefile, img);
}
int main() {
    saveimage("C:/Users/lenovo/Desktop/aa/T026_26.jpg",
        "C:/Users/lenovo/Desktop/aa/aa.jpg", 800);
}
 

你可能感兴趣的:(opencv,c++,人工智能)