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

pixel[c] = cv::saturate_cast<uchar>(pixel[c] * factor);

}

else if (targetBrightness < 0) {

pixel[c] = cv::saturate_cast<uchar>((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);

}

相关推荐
deephub1 分钟前
知识引导上下文优化(KgCoOp):一种解决灾难性遗忘的 Prompt Tuning 机制
人工智能·深度学习·机器学习·微调·prompt
fof9203 分钟前
Base LLM | 从 NLP 到 LLM 的算法全栈教程 第四天
人工智能·自然语言处理
前进的李工3 分钟前
LangChain使用之Model IO(提示词模版之FewShotPromptTemplate)
开发语言·人工智能·语言模型·langchain·agent
哎一入江湖岁月催3 分钟前
《洛杉矶劫案》观后感
人工智能
爱吃生蚝的于勒3 分钟前
【Linux】网络之http协议
linux·运维·服务器·网络·数据结构·c++·http
咚咚王者5 分钟前
人工智能之语言领域 自然语言处理 第二十章 数据处理工具
人工智能·自然语言处理
2501_945424806 分钟前
高性能计算资源调度
开发语言·c++·算法
fakerth8 分钟前
【Linux】调度器底层原理深入探索
linux·c++·操作系统
Agent产品评测局10 分钟前
2026 年企业自动化路线图:如何通过 LLM+RPA 实现全流程闭环?深度解析智能体架构与落地路径
人工智能·ai·chatgpt·架构·自动化·rpa
TTTrees12 分钟前
C++学习笔记(33):智能指针(工厂函数)
c++