Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)

列举单色滤镜

1.暖色
2.冷色
3.反色
4.老照片
5.灰度

github代码(持续更新)

期待你的小星星
github:https://github.com/dependon/simple-image-filter //qt美颜滤镜

Bound函数,防止越界

int Bound(int range_left, int data, int range_right)
{
    int index = data;
    if (data > range_right) {
        index = range_right;
    } else if (data < range_left) {
        index = range_left;
    }
    return index;
}

暖色滤镜

Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)_第1张图片原理如下:
​ 这里红绿越高,颜色越暖,蓝色越高,图片色彩越冷,所以算法为红色和绿色按固定数值增加,蓝色不变。
red=red+30;
green=green+30;

QImage warnImage(const QImage &img, int index)
{
    QImage imgCopy;
    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    if (nullptr == rgb) {
        return QImage();
    }
    QColor frontColor;
    int size = img.width() * img.height();

    for (int i = 0; i < size ; i++) {
        int r = rgb[i * 3] + index;
        int g = rgb[i * 3 + 1] + index;
        int b = rgb[i * 3 + 2] ;

        rgb[i * 3] = r > 255 ? 255 : r;
        rgb[i * 3 + 1] = g > 255 ? 255 : g;
        rgb[i * 3 + 2] = b > 255 ? 255 : b;
    }

    return imgCopy;
}

冷色滤镜

Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)_第2张图片
原理如下:
这里红绿越高,颜色越暖,蓝色越高,图片色彩越冷,所以算法为红色和绿色不变,蓝色增加30。
blue=blue+30

QImage coolImage(const QImage &img,  int index)
{

    QImage imgCopy;
    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    if (nullptr == rgb) {
        return QImage();
    }
    QColor frontColor;
    int size = img.width() * img.height();

    for (int i = 0; i < size ; i++) {
        int r = rgb[i * 3] ;
        int g = rgb[i * 3 + 1] ;
        int b = rgb[i * 3 + 2] + index;

        rgb[i * 3] = r > 255 ? 255 : r;
        rgb[i * 3 + 1] = g > 255 ? 255 : g;
        rgb[i * 3 + 2] = b > 255 ? 255 : b;
    }

    return imgCopy;
}

灰度滤镜

Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)_第3张图片
原理如下:
​ 任何颜色都有红、绿、蓝三原色组成,假如原来某点的颜色为RGB(R,G,B),那么,我们可以通过下面几种方法,将其转换为灰度:
  1.浮点算法:Gray = R0.3 + G0.59 + B0.11
  2.整数方法:Gray = (R
299+G587+B114)/1000
  3.移位方法:Gray =(R28+G151+B*77)>> 8
  4.平均值法:Gray = (R+G+B)/3
  5.仅取绿色:Gray = G
  通过以上任何一种方法求得Gray后,将原来的RGB(r,g,b)中的r,g,b统一用Gray替换,形成新的颜色RGB(Gray,Gray,Gray),用它替换原来的RGB(r,g,b)就是灰度图了。
这里采用平均值算法和整数算法

//方案1
QImage GreyScale(const QImage &img)
{
    QImage imgCopy;

    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    int size = img.width() * img.height();
    for (int i = 0; i < size ; i++) {
        int average = (rgb[i * 3] * 299 + rgb[i * 3 + 1] * 587 + rgb[i * 3 + 1] * 114 + 500) / 1000;
        rgb[i * 3] = average;
        rgb[i * 3 + 1] = average;
        rgb[i * 3 + 2] = average;
    }
    return imgCopy;

}
//方案2

QImage QImageAPI::GrayScaleImage(const QImage &img)
{

    QImage imgCopy;
    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    if (nullptr == rgb) {
        return QImage();
    }
    QColor frontColor;
    int size = img.width() * img.height();

    for (int i = 0; i < size ; i++) {
        int average = (rgb[i * 3] + rgb[i * 3 + 1] + rgb[i * 3 + 2]) / 3;
        rgb[i * 3] = average > 255 ? 255 : average;
        rgb[i * 3 + 1] = average > 255 ? 255 : average;
        rgb[i * 3 + 2] = average > 255 ? 255 : average;
    }
    return imgCopy;
}

老照片滤镜


原理如下:
该点red值 =0.393x该点red值+0.769x该点green值+0.189x该点blue值
该点green值 =0.349x该点red值+0.686x该点green值+0.168x该点blue值
该点blue值 =0.272x该点red值+0.534x该点green值+0.131x该点blue值
如果遇到超过255的情况,取最大值255

QImage oldImage(const QImage &img)
{
    QImage imgCopy;
    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    if (nullptr == rgb) {
        return QImage();
    } int size = img.width() * img.height();
    for (int i = 0; i < size ; i++) {
        float r = 0.393 * rgb[i * 3] + 0.769 * rgb[i * 3 + 1] + 0.189 * rgb[i * 3 + 2];
        float g = 0.349 * rgb[i * 3] + 0.686 * rgb[i * 3 + 1] + 0.168 * rgb[i * 3 + 2];
        float b = 0.272 * rgb[i * 3] + 0.534 * rgb[i * 3 + 1] + 0.131 * rgb[i * 3 + 2];
        r = Bound(0, r, 255);
        g = Bound(0, g, 255);
        b = Bound(0, b, 255);
        rgb[i * 3] = r;
        rgb[i * 3 + 1] = g ;
        rgb[i * 3 + 2] = b  ;
    }
    return imgCopy;
}

反色滤镜

Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)_第4张图片原理如下:
每个点的rgb色都变成
该点red值=255-该点red值;
该点green值=255-该点green值;
该点blue值=255-该点blue值;

QImage QImageAPI::InverseColorImage(const QImage &img)
{
    QImage imgCopy;
    if (img.format() != QImage::Format_RGB888) {
        imgCopy = QImage(img).convertToFormat(QImage::Format_RGB888);
    } else {
        imgCopy = QImage(img);
    }
    uint8_t *rgb = imgCopy.bits();
    if (nullptr == rgb) {
        return QImage();
    } int size = img.width() * img.height();
    for (int i = 0; i < size ; i++) {
        rgb[i * 3] = 255 - rgb[i * 3] ;
        rgb[i * 3 + 1] = 255 - rgb[i * 3 + 1]  ;
        rgb[i * 3 + 2] = 255 - rgb[i * 3 + 2]  ;
    }
    return imgCopy;
}

你可能感兴趣的:(C++,qt,qt图像处理,算法,图像处理,qt,c++)