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);
 }
 ?
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!