//浮点算法: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));
}
}
}