Qt画像処理の階調変換


画像の明るさ、コントラストを変換するのはよく使われる画像処理操作ですが、Qt それ自体は対応する機能コードを提供していません.そこで、これらの操作を実現するために簡単なクラスを書きました.私はこのクラスを BrightnessMapper.
 
コードは次のとおりです.
#ifndef BRIGHTNESSMAPPER_H
#define BRIGHTNESSMAPPER_H
#include 

class BrightnessMapper
{
public:
    BrightnessMapper();
    ~BrightnessMapper();
    void setRedMap(unsigned char red[]);
    void setGreenMap(unsigned char green[]);
    void setBlueMap(unsigned char blue[]);
    void setMap(unsigned char map[]);
    void updateBCG(double brightness, double contrast, double gamma);
    void setColorMap(unsigned char red[], unsigned char green[], unsigned char blue[]);
    void apply(const QImage &from, QImage &to);
    QImage apply(const QImage &from);
    unsigned char *redMap(){ return m_red;}
    unsigned char *blueMap(){return m_blue;}
    unsigned char *greenMap(){return m_green;}
    void redMap(double red[256]);
    void greenMap(double green[256]);
    void blueMap(double blue[256]);
private:
    unsigned char m_red[256];
    unsigned char m_green[256];
    unsigned char m_blue[256];
};

#endif // BRIGHTNESSMAPPER_H
#include "brightnessmapper.h"
#include 
#include 
BrightnessMapper::BrightnessMapper()
{
    for(int i = 0; i < 256; i++)
    {
        m_red[i] = i;
        m_green[i] = i;
        m_blue[i] = i;
    }
}

BrightnessMapper::~BrightnessMapper()
{

}

void BrightnessMapper::updateBCG(double brightness, double contrast, double gamma)
{
    double x, y;
    for(int i = 0; i < 256; i ++)
    {
        x = i / 255.0;
        y = exp(log(x) * gamma);
        y = (y - 0.5) * brightness + 0.5 + contrast / 255;
        y = y * 255.0;
        m_red[i] = qBound(0.0, y, 255.0);
        m_green[i] = m_red[i];
        m_blue[i] = m_red[i];

    }
}

QImage BrightnessMapper::apply(const QImage &from)
{
    QImage to = from;
    apply(from, to);
    return to;
}

void BrightnessMapper::apply(const QImage &from, QImage &to)
{
    if(to.size() != from.size() || to.format()!= from.format())
    {
        to = from.copy();
    }
    int height = from.height();
    int width = from.width();
    switch(from.format())
    {
    case QImage::Format_Indexed8:
        for(int i = 0; i < height; i++)
        {
            const uchar *pFrom = (const uchar *)from.constScanLine(i);
            uchar *pTo = (uchar *)to.scanLine(i);
            for(int j = 0; j < width; j++)
            {
                pTo[j] = m_red[pFrom[i]];
            }
        }
        break;
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
        for(int i = 0; i < height; i++)
        {
            const QRgb *pFrom = (const QRgb *)from.constScanLine(i);
            QRgb *pTo = (QRgb *)to.scanLine(i);
            for(int j = 0; j < width; j++)
            {
                int r, g, b;
                r = qRed(pFrom[j]);
                g = qGreen(pFrom[j]);
                b = qBlue(pFrom[j]);
                r = m_red[r];
                g = m_green[g];
                b = m_blue[b];
                pTo[j] = qRgb(r, g, b);
            }
        }
        break;
    }
}
void BrightnessMapper::setRedMap(unsigned char red[])
{
    for(int i = 0; i < 256; i++)
    {
        m_red[i] = red[i];
    }
}

void BrightnessMapper::setGreenMap(unsigned char green[])
{
    for(int i = 0; i < 256; i++)
    {
        m_green[i] = green[i];
    }
}

void BrightnessMapper::setBlueMap(unsigned char blue[])
{
    for(int i = 0; i < 256; i++)
    {
        m_blue[i] = blue[i];
    }
}

void BrightnessMapper::setMap(unsigned char map[])
{
    for(int i = 0; i < 256; i++)
    {
        m_red[i] = map[i];
        m_green[i] = map[i];
        m_blue[i] = map[i];
    }
}

void BrightnessMapper::setColorMap(unsigned char red[], unsigned char green[], unsigned char blue[])
{
    for(int i = 0; i < 256; i++)
    {
        m_red[i] = red[i];
        m_green[i] = green[i];
        m_blue[i] = blue[i];
    }
}
void BrightnessMapper::redMap(double red[256])
{
    for(int i = 0; i < 256; i++)
    {
        red[i] = m_red[i];
    }
}

void BrightnessMapper::greenMap(double green[256])
{
    for(int i = 0; i < 256; i++)
    {
        green[i] = m_green[i];
    }
}

void BrightnessMapper::blueMap(double blue[256])
{
    for(int i = 0; i < 256; i++)
    {
        blue[i] = m_blue[i];
    }
}

この類の使い方は簡単です.階調変換関係は、次の関数で設定できます.
 
void setRedMap(unsigned char red[]);
void setGreenMap(unsigned char green[]);
void setBlueMap(unsigned char blue[]);
void setMap(unsigned char map[]);
void setColorMap(unsigned char red[], unsigned char green[], unsigned char blue[]);
 
次の関数で明るさ、コントラスト、gammaを設定することもできます 値を指定して、変換関係を決定します.
void updateBCG(double brightness, double contrast, double gamma);
 
変換関係が決定されると、次の2つの関数の1つを用いて画像を変換処理することができる.
void apply(const QImage &from, QImage &to);
QImage apply(const QImage &from);
 
変換関係テーブルを読み込むには、いくつかの補助関数があります.
unsigned char *redMap(){ return m_red;}
unsigned char *blueMap(){return m_blue;}
unsigned char *greenMap(){return m_green;}
void redMap(double red[256]);
void greenMap(double green[256]);
void blueMap(double blue[256]);
 
コードが簡単なので、あまり紹介しません.皆さんに役に立つことを願っています.