qt中的图像伽玛校正 [英] Image gamma correction in qt

查看:142
本文介绍了qt中的图像伽玛校正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用qt更改图像的伽玛,但没有得到理想的结果. 这是我的代码:

I try to change the gamma of an image using qt but i don't get the desirable result . Here is my code :

QImage Filters::aply_filtre_gamma(QImage image){
   // (std::pow((image.pixel(x,y)/255),2.2))*255
    QRgb val;

    for(int x=1; x < image.width(); x++)
        for(int y=1 ; y < image.height(); y++){
           val=image.pixel(x,y);
           val=qRed(val);

           val=std::pow(val/255,2)*255;
           //std::cout<<"valoare pixel:"<<valll<<std::endl;
           image.setPixel(x,y, QColor(val, val, val).rgb());
          // image.setPixel(x,y,std::pow((valll/65025),2.2)*65025);

        }

return image ;
}

我的代码中是否存在错误或者我没有正确使用公式?

Is there an error in my code or i don't use the formula correctly ?

推荐答案

恐怕会发生某种类型的不匹配. (我没有检查,但我想知道它是否可以编译.)

I'm afraid there happens some type mismatch. (I didn't check but I would wonder if it even compiles.)

QImage::pixel() 返回 qRed() 取一个QRgb并返回一个int.

因此,我想知道val=qRed(val);会做什么. (如果编译,可能无法达到预期的效果.)

Hence, I wonder what val=qRed(val); would do. (If it compiles it doesn't probably do the expected.)

请记住,我稍微修改了OP的代码:

This in mind, I changed OP's code a bit:

QImage Filters::aply_filtre_gamma(QImage image)
{
    for(int x = 0; x < image.width(); ++x)
        for(int y = 0 ; y < image.height(); ++y) {
           const QRgb rgb = image.pixel(x, y);
           const double r = qRed(rgb) / 255.0;
           const double g = qGreen(rgb) / 255.0;
           const double b = qBlue(rgb) / 255.0;
           image.setPixelColor(x, y,
             QColor(
               255 * std::pow(r, 2.2),
               255 * std::pow(g, 2.2),
               255 * std::pow(b, 2.2)));
        }
    return image;
}

之前,我对Wikipedia进行了简短的刷新,内容涉及伽玛校正.

Before, I took a short refresh in Wikipedia concerning gamma correction.

注意:

我还修复了for循环的起始值.左上角的像素的坐标为(0,0)–但不是(1,1).

I fixed also the start values of for loops. The pixel of upper left corner has coordinates (0, 0) – but not (1, 1).

完整的测试/示例– testQImageGamma.cc:

A complete test/example – testQImageGamma.cc:

#include <QtWidgets>

QPixmap fromImage(const QImage &qImg)
{
  QPixmap qPixmap;
  qPixmap.convertFromImage(qImg);
  return qPixmap;
}

QImage gamma(const QImage &qImg, double exp)
{
  QImage qImgRet(qImg);
  for (int x = 0; x < qImg.width(); ++x) {
    for (int y = 0 ; y < qImg.height(); ++y) {
      const QRgb rgb = qImg.pixel(x, y);
      const double r = qRed(rgb) / 255.0;
      const double g = qGreen(rgb) / 255.0;
      const double b = qBlue(rgb) / 255.0;
      qImgRet.setPixelColor(x, y,
        QColor(
          255 * std::pow(r, exp),
          255 * std::pow(g, exp),
          255 * std::pow(b, exp)));
    }
  }
  return qImgRet;
}

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup data
  const QImage qImg("cat.rgb.png");
  const QImage qImgGamma = gamma(qImg, 1 / 2.2);
  // setup UI
  QWidget qWin;
  qWin.setWindowTitle(QString::fromUtf8("Gamma Correction"));
  QVBoxLayout qVBox;
  QLabel qLbl(QString::fromUtf8("Original Image:"));
  qVBox.addWidget(&qLbl);
  QLabel qLblImg;
  qLblImg.setPixmap(fromImage(qImg));
  qVBox.addWidget(&qLblImg);
  qWin.setLayout(&qVBox);
  QLabel qLblGamma(QString::fromUtf8("Gamma corrected Image:"));
  qVBox.addWidget(&qLblGamma);
  QLabel qLblImgGamma;
  qLblImgGamma.setPixmap(fromImage(qImgGamma));
  qVBox.addWidget(&qLblImgGamma);
  qWin.setLayout(&qVBox);
  qWin.show();
  // runtime loop
  return app.exec();
}

Qt项目文件testQImageGamma.pro:

SOURCES = testQImageGamma.cc

QT += widgets

已在Windows 10上的 cygwin64 上进行编译和测试:

Compiled and tested on cygwin64 on Windows 10:

$ qmake-qt5 testQImageGamma.pro

$ make && ./testQImageGamma
Qt Version: 5.9.4

请注意,在我的示例中,我使用1 / 2.2作为指数. (在2.2版本中,经过伽玛校正的图像变暗了.)

Please, note that I used 1 / 2.2 as exponent in my example. (With 2.2 the gamma corrected image became darker.)

这篇关于qt中的图像伽玛校正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆