Gray-scale transformation of Qt image processing and qt gray-scale
Changing the brightness and contrast of an image is a common image processing operation, but Qt itself does not provide the corresponding functional code. Therefore, I wrote a simple class to implement these operations. I call this class BrightnessMapper.
The Code is as follows:
#ifndef BRIGHTNESSMAPPER_H#define BRIGHTNESSMAPPER_H#include <QImage>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 <math.h>#include <QDebug>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]; }}
This class is easy to use. You can use the following functions to set the gray-scale conversion relationship.
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 []);
You can also use the following function to determine the Conversion Relationship by setting the brightness, contrast, and gamma values.
Void updateBCG (double brightness, double contrast, double gamma );
After the conversion relationship is determined, you can use one of the following two functions to transform the image.
Void apply (const QImage & from, QImage & );
QImage apply (const QImage & from );
There are also several auxiliary functions that can be used to read the transformed relational table.
Unsigned char * redMap () {return m_red ;}
Unsigned char * blueMap () {return m_blue ;}
Unsigned char * greenMap () {return m_green ;}
Void redMap (double red [1, 256]);
Void greenMap (double green [1, 256]);
Void blueMap (double blue [256]);
Because the code is simple to answer, I will not introduce it much. I hope it will be useful to you.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.