QT开发中文网

Qt C++ opencv 写一个程序 对本地jpg图像 批量增加图像 亮度

下面是一个使用Qt C++和OpenCV库编写的程序示例,用于批量增加本地jpg图像的亮度:

#include <QCoreApplication>
#include <QString>
#include <QDir>
#include <QDebug>

#include <opencv2/opencv.hpp>

using namespace cv;

void adjustBrightness(const QString& filePath, int delta)
{
    // 读取图像
    Mat image = imread(filePath.toStdString());
    if (image.empty()) {
        qDebug() << "Failed to load image: " << filePath;
        return;
    }

    // 转换为Lab颜色空间
    Mat labImage;
    cvtColor(image, labImage, COLOR_BGR2Lab);

    // 分割通道
    std::vector<Mat> channels;
    split(labImage, channels);

    // 调整亮度(增加L通道值)
    channels[0] += delta;

    // 合并通道
    merge(channels, labImage);

    // 转回BGR颜色空间
    cvtColor(labImage, image, COLOR_Lab2BGR);

    // 保存修改后的图像
    QString outputPath = QCoreApplication::applicationDirPath() + "/output/" + QFileInfo(filePath).fileName();
    
   if (!imwrite(outputPath.toStdString(), image)) {
        qDebug() << "Failed to save image: " << outputPath;
        return;
   }

   qDebug() << "Processed image saved to: " << outputPath;
}

void processImages(const QString& directory, int brightnessDelta)
{
   QDir dir(directory);
   QStringList filters;
   filters << "*.jpg"; // 只处理jpg格式图片
   dir.setNameFilters(filters);

   QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);

   foreach (const QFileInfo &fileInfo, fileList) {
       adjustBrightness(fileInfo.filePath(), brightnessDelta);
   }
}

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   QString directoryPath = "your_directory_path"; // 输入你的图片目录路径
   int brightnessDelta = 50; // 调整亮度的增量值,可以根据需要修改

   processImages(directoryPath, brightnessDelta);

   return a.exec();
}

请将上述代码中的your_directory_path替换为你实际存放图片的目录路径。该程序会遍历指定目录下的所有jpg格式图像,并对每张图像进行亮度调整,并保存到output文件夹下。调整亮度的增量值可以通过brightnessDelta参数进行设置。

此示例中使用了OpenCV库来处理图像,因此在编译和运行之前,请确保已经正确配置和安装了OpenCV库。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=9069

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?