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

}

相关推荐
Li emily3 小时前
成功接入A股实时行情API获取实时市场数据
人工智能·python·金融·fastapi
China_Yanhy3 小时前
转型AI运维工程师·Day 7:构建“数据飞轮” —— 每一句对话都是资产
运维·人工智能·状态模式
苍何3 小时前
爆肝 2 天,用 GLM5 开发了 OpenClaw 接入微信 bot,已开源!
人工智能
kuankeTech3 小时前
“数改智转”加速跑:外贸ERP助力钢铁智能工厂“提质增效”
大数据·人工智能·经验分享·软件开发·erp
Mr_WangAndy3 小时前
C++数据结构与算法_线性表_数组_概念动态数组,刷题
c++·二分查找·数组刷题·数组字符串逆序·零移动·有序数组的平方
阿猿收手吧!4 小时前
【C++】jthread:优雅终止线程新方案
开发语言·c++
澳鹏Appen4 小时前
澳鹏无锡成功获评国家高新技术企业
人工智能
threerocks4 小时前
前端将死,Agent 永生
前端·人工智能·ai编程
苍何4 小时前
偶然间发现一款逆天的 AI PPT 工具!免费生成!
人工智能
苍何4 小时前
Openclaw + OpenCode 才是 vibe coding 的最棒组合!
人工智能