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);

}

相关推荐
习明然43 分钟前
我的本地化AI项目(三)
人工智能·python·electron·c#·avalonia
程序猿炎义1 小时前
一人内容团队——用Amazon Quick Desktop实现小红书从选题到发布的全流程自动化
大数据·人工智能·microsoft·自动化·小红书
阿虎儿2 小时前
daytona创建snapshot: Failed to get initial runner: Error: No available runners
人工智能
字节跳动视频云技术团队3 小时前
火山引擎 × 央视网 打造 2026 世界杯沉浸式观赛盛宴
人工智能·音视频开发
IT_陈寒3 小时前
Java线程池这个坑我算是踩明白了
前端·人工智能·后端
创安达科技3 小时前
2026 市政太阳能警示设施采购,多维度设备性能评估参考
人工智能·太阳能道钉灯·创安达
isyangli_blog3 小时前
西钻—— 图影智绘创作课堂 (豆包、千问)
人工智能
影山飞雄3 小时前
Systematic-Debugging skill深度解析
人工智能