图像灰度化的四种方法

来源:互联网 发布:淘宝开店保证金怎么交 编辑:程序博客网 时间:2024/06/06 03:02
//浮点算法:Gray=R*0.3+G*0.59+B*0.11void 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)/100void 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));        }    }}