图像灰度化的四种方法

98 篇文章 5 订阅
//浮点算法:Gray=R*0.3+G*0.59+B*0.11
void MainWindow::toGrayFloat(QImage *image, QImage *ImageBetter)
{
    int height = image->height();
    int width = image->width();
    QRgb rgb00,rgb01;
    int ta = 0, tr = 0, tg = 0, tb = 0;
    for(int i = 0; i < width; i ++){
        for(int j = 0; j < height; j ++){
            rgb00 = image->pixel(i,j);//获取rgb
            ta = (rgb00 >> 24) & 0xff;
            tr = (rgb00 >> 16) & 0xff;  // r = qRed(rgb00);
            tg = (rgb00 >> 8) & 0xff;   //  g = qGreen(rgb00)
            tb = rgb00 & 0xff;          //b = qBlue(rgb00);
            int gray = tr*0.3+tg*0.59+tb*0.11;
            ImageBetter->setPixel(i,j,qRgb(gray,gray,gray));
        }
    }
}
//整数方法:Gray=(R*30+G*59+B*11)/100
void MainWindow::toGrayInt(QImage *image, QImage *ImageBetter)
{
    int height = image->height();
    int width = image->width();
    QRgb rgb00,rgb01;
    int ta = 0, tr = 0, tg = 0, tb = 0;
    for(int i = 0; i < width; i ++){
        for(int j = 0; j < height; j ++){
            rgb00 = image->pixel(i,j);//获取rgb
            ta = (rgb00 >> 24) & 0xff;
            tr = (rgb00 >> 16) & 0xff;   // r = qRed(rgb00);
            tg = (rgb00 >> 8) & 0xff;    //  g = qGreen(rgb00)
            tb = rgb00 & 0xff;           //b = qBlue(rgb00);
            int gray = (tr*30+tg*59+tb*11)/100;
            ImageBetter->setPixel(i,j,qRgb(gray,gray,gray));
        }
    }
}
//.移位方法:Gray =(R*76+G*151+B*28)>>8;
void MainWindow::toGrayDisplacement(QImage *image, QImage *ImageBetter)
{
    int height = image->height();
    int width = image->width();
    QRgb rgb00,rgb01;
    int ta = 0, tr = 0, tg = 0, tb = 0;
    for(int i = 0; i < width; i ++){
        for(int j = 0; j < height; j ++){
            rgb00 = image->pixel(i,j);//获取rgb
            ta = (rgb00 >> 24) & 0xff;//
            tr = (rgb00 >> 16) & 0xff; // r = qRed(rgb00);
            tg = (rgb00 >> 8) & 0xff;  //  g = qGreen(rgb00);
            tb = rgb00 & 0xff;         //b = qBlue(rgb00);
            int gray = (tr*76+tg*151+tb*28)>>8;
            ImageBetter->setPixel(i,j,qRgb(gray,gray,gray));
        }
    }
}
//.平均值法:Gray=(R+G+B)/3;
void MainWindow::toGrayAverage(QImage *image, QImage *ImageBetter)
{
    int height = image->height();
    int width = image->width();
    QRgb rgb00,rgb01;
    int ta = 0, tr = 0, tg = 0, tb = 0;
    for(int i = 0; i < width; i ++){
        for(int j = 0; j < height; j ++){
            rgb00 = image->pixel(i,j);//获取rgb
            ta = (rgb00 >> 24) & 0xff;//
            tr = (rgb00 >> 16) & 0xff; // r = qRed(rgb00);
            tg = (rgb00 >> 8) & 0xff;  //  g = qGreen(rgb00);
            tb = rgb00 & 0xff;         //b = qBlue(rgb00);
            int gray = (tr+tg+tb)/3;
            ImageBetter->setPixel(i,j,qRgb(gray,gray,gray));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值