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

#include <iostream>

#include <iomanip>

#include<opencv2//opencv.hpp>

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<float>(targetBrightness) / 255.0f;

}

else if (targetBrightness < 0) {

factor = 255.0f / static_cast<float>(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<cv::Vec3b>(i, j);

// 调整亮度

for (int c = 0; c < channels; ++c) {

if (targetBrightness > 0) {

pixelc = cv::saturate_cast<uchar>(pixelc * factor);

}

else if (targetBrightness < 0) {

pixelc = cv::saturate_cast<uchar>((pixelc - 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);

}

相关推荐
九酒1 天前
AI Agent 开发踩坑记:口播功能非得用 APP 原生实现吗?
前端·人工智能·agent
蝎子莱莱爱打怪1 天前
DSpark 讲透:DeepSeek 不换模型,硬把 V4 提速 85%,是怎么做到的?
人工智能·面试·程序员
BadBadBad__AK1 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
巫山老妖1 天前
置身AI内
人工智能
IT_陈寒1 天前
JavaScript项目实战经验分享
前端·人工智能·后端
vanuan1 天前
两个AI智能体第一次对话-A2A双Agent协作实战
人工智能
kfaino1 天前
码农的AI翻身(四)你好,我叫 Attention
人工智能·后端
雨落Re1 天前
如何设计一个高质量Skill
人工智能
Token炼金师1 天前
大模型权重文件全指南:从格式选择到优化实战
人工智能
阿牛哥_GX1 天前
CDP 浏览器操控原理:让脚本接管你的浏览器
人工智能