将QImage转换为灰度

时间:2015-01-14 18:06:05

标签: c++ qt grayscale qimage

我有一个QImage,我需要将其转换为灰度,然后再用颜色绘制它。我找到了allGray()isGrayScale()函数来检查图像是否已经是灰度,但没有toGrayScale()或类似命名的函数。

现在我正在使用此代码,但它没有很好的性能:

for (int ii = 0; ii < image.width(); ii++) {
    for (int jj = 0; jj < image.height(); jj++) {
        int gray = qGray(image.pixel(ii, jj));
        image.setPixel(ii, jj, QColor(gray, gray, gray).rgb());
    }
}

将QImage转换为灰度的最佳方法是什么,性能方面呢?

4 个答案:

答案 0 :(得分:9)

使用,而不是使用slow functions QImage::pixelQImage::setPixel QImage::scanline访问数据。扫描(水平线)上的像素是连续的。假设您有32 bpp图像,则可以使用QRgb迭代扫描。最后总是将x坐标放在内部循环中。这给出了:

for (int ii = 0; ii < image.height(); ii++) {
    uchar* scan = image.scanLine(ii);
    int depth =4;
    for (int jj = 0; jj < image.width(); jj++) {

        QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + jj*depth);
        int gray = qGray(*rgbpixel);
        *rgbpixel = QColor(gray, gray, gray).rgba();
    }
}

使用3585 x 2386图片进行快速测试

********* Start testing of TestImage *********
Config: Using QTest library 4.7.4, Qt 4.7.4
PASS   : TestImage::initTestCase()

RESULT : TestImage::grayscaleOp():
     390 msecs per iteration (total: 390, iterations: 1)
PASS   : TestImage::grayscaleOp()

RESULT : TestImage::grayscaleFast():
     125 msecs per iteration (total: 125, iterations: 1)
PASS   : TestImage::grayscaleFast()

PASS   : TestImage::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped
********* Finished testing of TestImage *********

源代码: testimage.h文件:

#ifndef TESTIMAGE_H
#define TESTIMAGE_H

#include <QtTest/QtTest>

#include <QObject>
#include <QImage>

class TestImage : public QObject
{
    Q_OBJECT
public:
    explicit TestImage(QObject *parent = 0);

signals:

private slots:
    void grayscaleOp();

    void grayscaleFast();

private:
    QImage imgop;
    QImage imgfast;
};

#endif // TESTIMAGE_H

testimage.cpp文件:

#include "testimage.h"

TestImage::TestImage(QObject *parent)
    : QObject(parent)
    , imgop("path_to_test_image.png")
    , imgfast("path_to_test_image.png")
{
}


void TestImage::grayscaleOp()
{
    QBENCHMARK
    {
        QImage& image = imgop;

        for (int ii = 0; ii < image.width(); ii++) {
            for (int jj = 0; jj < image.height(); jj++) {
                int gray = qGray(image.pixel(ii, jj));
                image.setPixel(ii, jj, QColor(gray, gray, gray).rgb());
            }
        }
    }
}

void TestImage::grayscaleFast()
{

    QBENCHMARK {

    QImage& image = imgfast;


    for (int ii = 0; ii < image.height(); ii++) {
        uchar* scan = image.scanLine(ii);
        int depth =4;
        for (int jj = 0; jj < image.width(); jj++) {

            QRgb* rgbpixel = reinterpret_cast<QRgb*>(scan + jj*depth);
            int gray = qGray(*rgbpixel);
            *rgbpixel = QColor(gray, gray, gray).rgba();
        }
    }

    }
}

QTEST_MAIN(TestImage)

专业档案:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QImageTest
TEMPLATE = app

CONFIG  += qtestlib

SOURCES += testimage.cpp

HEADERS += testimage.h

重要提示:

  • 通过反转循环,您已经获得了重要的性能提升。在此测试用例中,它是~90ms
  • 您可以使用其他库(如opencv)进行灰度转换,然后从opencv缓冲区构建Qimage。我期待更好的性能提升。

答案 1 :(得分:7)

从Qt 5.5起,您可以调用QImage::convertToFormat()将QImage转换为灰度,如下所示:

QImage image = ...;
image.convertToFormat(QImage::Format_Grayscale8);

答案 2 :(得分:4)

我将发布@ UmNyobe代码的略微修改版本。我只是为扫描线增加一个指针,而不是通过索引计算每个像素。

// We assume the format to be RGB32!!!
Q_ASSERT(image.format() == QImage::Format_RGB32);
for (int ii = 0; ii < image.height(); ii++) {
    QRgb *pixel = reinterpret_cast<QRgb*>(image.scanLine(ii));
    QRgb *end = pixel + image.width();
    for (; pixel != end; pixel++) {
        int gray = qGray(*pixel);
        *pixel = QColor(gray, gray, gray).rgb();
    }
}

答案 3 :(得分:0)

内部qt类QPixmapColorizeFilter使用解决类似主题的函数grayscale

我从中派生了以下函数,这应该可以解决问题。

重要的一点是将图像转换为32位格式,因此您可以将每个像素视为32位值,而无需考虑位对齐。

您也可以直接使用bits函数并迭代所有像素,而不是遍历行和列。通过此技巧,您可以避免在scanLine函数中执行乘法。

QImage convertToGrayScale(const QImage &srcImage) {
     // Convert to 32bit pixel format
     QImage dstImage = srcImage.convertToFormat(srcImage.hasAlphaChannel() ?
              QImage::Format_ARGB32 : QImage::Format_RGB32);

     unsigned int *data = (unsigned int*)dstImage.bits();
     int pixelCount = dstImage.width() * dstImage.height();

     // Convert each pixel to grayscale
     for(int i = 0; i < pixelCount; ++i) {
        int val = qGray(*data);
        *data = qRgba(val, val, val, qAlpha(*data));
        ++data;
     }

     return dstImage;
  }
相关问题