QT图像处理算法

.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSpinBox>
#include <QPushButton>
#include <QImage>
#include <QDoubleSpinBox>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void imageSharpening();
    void imageGray();
    QImage qImageToGray();

    void YanMo2(int *tmpPixel, int width, int height);
    void YanMo1(int *tmpPixel, int width, int height);
    void MoreClearPic(QRgb *tmpPixel,int yanKind,int width,int height);
    void DecreseVoice();
    void SobelGet();
    void Template(QImage &SobelImage);
    QImage * saturation(int delta, QImage * origin);
    QImage * blur(QImage * origin);
    QImage * sharpen(QImage * origin);
    void cool();
    void brightness();
    void directionalGray();
    void filter();
    void define(QImage);
    void MakePicAverage();
private:
    Ui::MainWindow *ui;
    QImage *image;

};

#endif // MAINWINDOW_H


.cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QImage aImage = qImageToGray();
    define(aImage);


    //MakePicAverage();
    //brightness();
    //imageSharpening();
    //imageGray();
    //filter();
    //directionalGray();
}

MainWindow::~MainWindow()
{
    delete ui;
}
//图像锐化
void MainWindow::imageSharpening()
{
    int betterValue = 5;
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\piliexian.jpg");
    QImage ImageBetter;
    ImageBetter = QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int r,g,b,tmpR,tmpG,tmpB;
    QRgb rgb00,rgb01,rgb10;
    int width = image->width();
    int height = image->height();
    for(i = 0;i < width;i++)
    {
        for(j = 0;j < height;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                rgb00 = image->pixel(i,j);//获取rgb
                rgb01 = image->pixel(i,j+1);
                rgb10 = image->pixel(i+1,j);
                r = qRed(rgb00);
                g = qGreen(rgb00);
                b = qBlue(rgb00);
                tmpR = abs(qRed(rgb00)-qRed(rgb01))+abs(qRed(rgb00)-qRed(rgb10));
                tmpG = abs(qGreen(rgb00)-qGreen(rgb01))+abs(qGreen(rgb00)-qGreen(rgb10));
                tmpB = abs(qBlue(rgb00)-qBlue(rgb01))+abs(qBlue(rgb00)-qBlue(rgb10));
                if((tmpR+120) < 255)
                {
                    if(tmpR > betterValue)
                    {
                        r = tmpR+120;
                    }
                }
                else
                {
                    r = 255;
                }

                if((tmpG + 120) < 255)
                {
                    if(tmpG > betterValue)
                    {
                        g = tmpG+120;
                    }
                }
                else
                {
                    g = 255;
                }

                if((tmpB + 120) < 255)
                {
                    if(tmpB > betterValue)
                    {
                        b = tmpB+120;
                    }
                }
                else
                {
                    b = 255;
                }
                ImageBetter.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }
    ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
}
//转灰度图
QImage MainWindow::qImageToGray()
{
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    int height = image->height();
    int width = image->width();

    QImage ret(width, height, QImage::Format_Indexed8);
    ret.setColorCount(256);
    for(int i = 0; i < 256; i++)
    {
        ret.setColor(i, qRgb(i, i, i));
    }
    switch(image->format())
    {
    case QImage::Format_Indexed8:
        for(int i = 0; i < height; i ++)
        {
            const uchar *pSrc = (uchar *)image->constScanLine(i);
            uchar *pDest = (uchar *)ret.scanLine(i);
            memcpy(pDest, pSrc, width);
        }
        break;
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
        for(int i = 0; i < height; i ++)
        {
            const QRgb *pSrc = (QRgb *)image->constScanLine(i);
            uchar *pDest = (uchar *)ret.scanLine(i);

            for( int j = 0; j < width; j ++)
            {
                pDest[j] = qGray(pSrc[j]);
            }
        }
        break;
    }
    return ret;
}
//饱和度
void MainWindow::cool()
{
    int delta = 30;
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    QImage *newImage = new QImage(image->width(), image->height(), QImage::Format_ARGB32);

    QColor oldColor;
    int r,g,b;

    for(int x=0; x<newImage->width(); x++){
        for(int y=0; y<newImage->height(); y++){
            oldColor = QColor(image->pixel(x,y));

            r = oldColor.red() + delta;
            g = oldColor.green() + delta;
            b = oldColor.blue();

            //we check if the new values are between 0 and 255
            //            r = qBound(0, r, 255);
            //            g = qBound(0, g, 255);
            b = qBound(0, b, 255);
            newImage->setPixel(x,y, qRgb(r,g,b));
        }
    }
    ui->label_2->resize(newImage->width(),newImage->height());
    ui->label_2->setPixmap(QPixmap::fromImage(*newImage));
}
//饱和度
QImage * MainWindow::saturation(int delta, QImage * origin){
    QImage * newImage = new QImage(origin->width(), origin->height(), QImage::Format_ARGB32);

    QColor oldColor;
    QColor newColor;
    int h,s,l;

    for(int x=0; x<newImage->width(); x++){
        for(int y=0; y<newImage->height(); y++){
            oldColor = QColor(origin->pixel(x,y));

            newColor = oldColor.toHsl();
            h = newColor.hue();
            s = newColor.saturation()+delta;
            l = newColor.lightness();

            //we check if the new value is between 0 and 255
            s = qBound(0, s, 255);

            newColor.setHsl(h, s, l);

            newImage->setPixel(x, y, qRgb(newColor.red(), newColor.green(), newColor.blue()));
        }
    }

    return newImage;
}
//模糊
QImage * MainWindow::blur(QImage * origin){
    QImage * newImage = new QImage(*origin);

    int kernel [5][5]= {{0,0,1,0,0},
                        {0,1,3,1,0},
                        {1,3,7,3,1},
                        {0,1,3,1,0},
                        {0,0,1,0,0}};
    int kernelSize = 5;
    int sumKernel = 27;
    int r,g,b;
    QColor color;

    for(int x=kernelSize/2; x<newImage->width()-(kernelSize/2); x++){
        for(int y=kernelSize/2; y<newImage->height()-(kernelSize/2); y++){

            r = 0;
            g = 0;
            b = 0;

            for(int i = -kernelSize/2; i<= kernelSize/2; i++){
                for(int j = -kernelSize/2; j<= kernelSize/2; j++){
                    color = QColor(origin->pixel(x+i, y+j));
                    r += color.red()*kernel[kernelSize/2+i][kernelSize/2+j];
                    g += color.green()*kernel[kernelSize/2+i][kernelSize/2+j];
                    b += color.blue()*kernel[kernelSize/2+i][kernelSize/2+j];
                }
            }

            r = qBound(0, r/sumKernel, 255);
            g = qBound(0, g/sumKernel, 255);
            b = qBound(0, b/sumKernel, 255);

            newImage->setPixel(x,y, qRgb(r,g,b));

        }
    }
    return newImage;
}
//锐化
QImage * MainWindow::sharpen(QImage * origin){
    QImage * newImage = new QImage(* origin);

    int kernel [3][3]= {{0,-1,0},
                        {-1,5,-1},
                        {0,-1,0}};
    int kernelSize = 3;
    int sumKernel = 1;
    int r,g,b;
    QColor color;

    for(int x=kernelSize/2; x<newImage->width()-(kernelSize/2); x++){
        for(int y=kernelSize/2; y<newImage->height()-(kernelSize/2); y++){

            r = 0;
            g = 0;
            b = 0;

            for(int i = -kernelSize/2; i<= kernelSize/2; i++){
                for(int j = -kernelSize/2; j<= kernelSize/2; j++){
                    color = QColor(origin->pixel(x+i, y+j));
                    r += color.red()*kernel[kernelSize/2+i][kernelSize/2+j];
                    g += color.green()*kernel[kernelSize/2+i][kernelSize/2+j];
                    b += color.blue()*kernel[kernelSize/2+i][kernelSize/2+j];
                }
            }

            r = qBound(0, r/sumKernel, 255);
            g = qBound(0, g/sumKernel, 255);
            b = qBound(0, b/sumKernel, 255);

            newImage->setPixel(x,y, qRgb(r,g,b));

        }
    }
    return newImage;
}
//亮度调节
void MainWindow::brightness()
{
    int delta = 30;
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    QImage *newImage = new QImage(image->width(), image->height(), QImage::Format_ARGB32);

    QColor oldColor;
    int r,g,b;

    for(int x=0; x<newImage->width(); x++){
        for(int y=0; y<newImage->height(); y++){
            oldColor = QColor(image->pixel(x,y));

            r = oldColor.red() + delta;
            g = oldColor.green() + delta;
            b = oldColor.blue();

            //we check if the new values are between 0 and 255
            r = qBound(0, r, 255);
            g = qBound(0, g, 255);

            newImage->setPixel(x,y, qRgb(r,g,b));
        }
    }
    ui->label_2->resize(newImage->width(),newImage->height());
    ui->label_2->setPixmap(QPixmap::fromImage(*newImage));
}
//灰度
void MainWindow::directionalGray()
{
    image=new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\people.jpg");
    QImage ImageBetter;
    ImageBetter = QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int r,g,b;
    QRgb rgb00,rgb01,rgb10;
    int width = image->width();
    int height = image->height();
    int threshold = 200;
    for(i = 0;i<width;i++)
    {
        int ta = 0, tr = 0, tg = 0, tb = 0;
        for(j = 0;j < height;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                rgb00 = image->pixel(i,j);//获取rgb
                ta = (rgb00 >> 24) & 0xff;
                tr = (rgb00 >> 16) & 0xff;
                tg = (rgb00 >> 8) & 0xff;
                tb = rgb00 & 0xff;
                int gray = (int)(0.299 * (double)tr + 0.587 * (double)tg + 0.114 * (double)tb);
                double distance = qRgb(tr, tg, tb);
                if(distance < threshold) {
                    rgb01 = qRgba(tr, tg, tb, gray);
                    tr = qRed(rgb01);
                    tg = qGreen(rgb01);
                    tb = qBlue(rgb01);
                    rgb10 = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                    r = qRed(rgb10);
                    g = qGreen(rgb10);
                    b = qBlue(rgb10);
                    ImageBetter.setPixel(i,j,qRgb(r,g,b));
                }
                else {
                    rgb10 = (ta << 24) | (gray << 16) | (gray << 8) | gray;
                    r = qRed(rgb10);
                    g = qGreen(rgb10);
                    b = qBlue(rgb10);
                    ImageBetter.setPixel(i,j,qRgb(r,g,b));
                }
                //qRgba
            }
        }
    }
    ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
}
//二值化
void MainWindow::filter()
{
    image=new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    QImage ImageBetter;
    ImageBetter = QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int r,g,b;
    QRgb rgb00,rgb01;
    int width = image->width();
    int height = image->height();
    double total = height * width;
    double redSum = 0, greenSum = 0, blueSum = 0;
    for(i = 0;i<width;i++)
    {
        int ta = 0, tr = 0, tg = 0, tb = 0;
        for(j = 0;j < height;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                rgb00 = image->pixel(i,j);//获取rgb
                ta = (rgb00 >> 24) & 0xff;
                tr = (rgb00 >> 16) & 0xff;
                tg = (rgb00 >> 8) & 0xff;
                tb = rgb00 & 0xff;
                redSum += tr;
                greenSum += tg;
                blueSum +=tb;
            }
        }
    }
    int means = (int)(redSum / total);//获取阈值
#ifdef QT_DEBUG
    means = 225;
#else

#endif
    qDebug()<<means;
    //
    for(i = 0;i<width;i++)
    {
        int ta = 0, tr = 0, tg = 0, tb = 0;
        for(j = 0;j < height;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                rgb00 = image->pixel(i,j);//获取rgb
                ta = (rgb00 >> 24) & 0xff;
                tr = (rgb00 >> 16) & 0xff;
                tg = (rgb00 >> 8) & 0xff;
                tb = rgb00 & 0xff;
                if(tr >=means) {
                    tr = tg = tb = 255;
                } else {
                    tr = tg = tb = 0;
                }
                rgb01 = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                r = qRed(rgb01);
                g = qGreen(rgb01);
                b = qBlue(rgb01);
                ImageBetter.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }
    ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
}
//自定义
void MainWindow::define(QImage img)
{
    QImage ImageBetter;
    ImageBetter = QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int r,g,b;
    QRgb rgb00,rgb01,rgb10;
    int width = image->width();
    int height = image->height();
    char up[200000] = {0};
    char down[200000] = {0};
    int upCount = 0,downCount = 0;
    bool state = false;
    int jNum = 0;
    bool jstate = false;
    double upNum = 0;
    double downNum = 0;
    int ret = 0;
    for(i = 150;i  < width - 150;i++)
    {
        int ta = 0, tr = 0,tr1 = 0, tg = 0, tb = 0;
        for(j = 150;j < height - 150;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                char count = 0;
                rgb00 = image->pixel(i,j);
                tr = (rgb00 >> 16) & 0xff;
                for(int m = -15;m < 15; m++)
                {
                    rgb10 = image->pixel(i + m,j);
                    tr1 = (rgb10 >> 16) & 0xff;
                    char ch = tr - tr1;
                    if(ch >= -1 || ch <= 1)
                    {
                        count++;
                    }
                }
                for(int n = -15;n < 15; n++)
                {
                    rgb10 = image->pixel(i,j + n);
                    tr1 = (rgb10 >> 16) & 0xff;
                    if(tr == tr1)
                    {
                        count++;
                    }
                }
                if(count > 50)
                {
                    tr = tg = tb = 255;
                    if(jstate == false)
                    {
                      jstate = true;
                      jNum = j;
                    }
                    if(j - jNum > 20)
                    {
                        state = true;
                    }
                    jNum = j;
                    if(state == false)
                    {
                        up[upCount++] = i;
                    }
                    else
                    {
                        down[downCount++] = i;
                    }
                }
                else
                {
                    tr = tg = tb = 0;
                }

                rgb01 = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                r = qRed(rgb01);
                g = qGreen(rgb01);
                b = qBlue(rgb01);
                ImageBetter.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }

    for(int ic = 0;ic < upCount;ic++)
    {
        upNum += up[ic];
    }
    for(int jc = 0;jc < downCount;jc++)
    {
        downNum += down[jc];
    }
  ret = upNum / upCount - downNum / downCount;

  qDebug()<< ret;
    ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
}
//转灰度
void MainWindow::imageGray()
{
    image=new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\1.jpg");
    QImage ImageBetter;
    ImageBetter = QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int r,g,b;
    QRgb rgb00,rgb01;
    int width = image->width();
    int height = image->height();
    for(i = 0;i<width;i++)
    {
        for(j = 0;j < height;j++)
        {
            if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
            {
                rgb00 = image->pixel(i,j);//获取rgb
#ifdef QT_DEBUG
                unsigned int blue  = (rgb00 & 0x000000FF)>>0;
                unsigned int green = (rgb00 & 0x0000FF00) >> 8;
                unsigned int red   = (rgb00 & 0x00FF0000) >> 16;
#else
                unsigned int blue   = (rgb00 >> 0) & 0xff;
                unsigned int green  = (rgb00 >> 8) & 0xff;
                unsigned int red    = (rgb00 >> 16) & 0xff;
#endif
                rgb01 = ( red*38 +  green * 75 +  blue * 15 )>>7;
                //qDebug()<<rgb01;
                r = qRed(rgb01);
                g = qGreen(rgb01);
                b = qBlue(rgb01);
                ImageBetter.setPixel(i,j,qRgb(r,g,b));
            }
        }
    }
    ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
}
//直方图均衡化
void MainWindow::MakePicAverage()
{
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    QImage ImageAverage;
    ImageAverage=QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int i,j;
    int width,height;
    width=image->width();
    height=image->height();
    QRgb rgb;
    int r[256],g[256],b[256];//原图各个灰度数量的统计
    int rtmp,gtmp,btmp,rj,gj,bj;
    float rPro[256],gPro[256],bPro[256];//原图各个灰度级的概率
    float rTemp[256],gTemp[256],bTemp[256];//均衡化后各个灰度级的概率
    int rJun[256],gJun[256],bJun[256];//均衡化后对应像素的值
    memset(r,0,sizeof(r));
    memset(g,0,sizeof(g));
    memset(b,0,sizeof(b));

    //获取原图各个灰度的数量
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            rgb=image->pixel(i,j);
            r[qRed(rgb)]++;
            g[qGreen(rgb)]++;
            b[qBlue(rgb)]++;
        }
    }

    //获取原图各个灰度级的概率
    for(i=0;i<256;i++)
    {
        rPro[i]=(r[i]*1.0)/(width*height);
        gPro[i]=(g[i]*1.0)/(width*height);
        bPro[i]=(b[i]*1.0)/(width*height);
    }

    //均衡化后各个灰度级的概率,同时获取均衡化后对应像素的值
    for(i=0;i<256;i++)
    {
        if(i==0)
        {
            rTemp[0]=rPro[0];
            gTemp[0]=gPro[0];
            bTemp[0]=bPro[0];
        }
        else
        {
            rTemp[i]=rTemp[i-1]+rPro[i];
            gTemp[i]=gTemp[i-1]+gPro[i];
            bTemp[i]=bTemp[i-1]+bPro[i];
        }
        rJun[i]=(int)(255*rTemp[i]+0.5);
        gJun[i]=(int)(255*gTemp[i]+0.5);
        bJun[i]=(int)(255*bTemp[i]+0.5);
    }

    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            rgb=image->pixel(i,j);
            rtmp=qRed(rgb);
            gtmp=qGreen(rgb);
            btmp=qBlue(rgb);
            rj=rJun[rtmp];
            gj=gJun[gtmp];
            bj=bJun[btmp];
            ImageAverage.setPixel(i,j,qRgb(rj,gj,bj));
        }
    }
    ui->label_2->resize(ImageAverage.width(),ImageAverage.height());
    ui->label_2->setPixmap(QPixmap::fromImage(ImageAverage));
    delete image;
}
//Sobel算子提取图像边界
void MainWindow::SobelGet()
{
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    QImage SobelIma;
    SobelIma=QImage(image->width(),image->height(),QImage::Format_ARGB32);
    Template(SobelIma);
    ui->label_2->resize(SobelIma.width(),SobelIma.height());
    ui->label_2->setPixmap(QPixmap::fromImage(SobelIma));
}
//与算子进行卷积的函数
void MainWindow::Template(QImage &SobelImage)
{
    int width=image->width(),height=image->height();
    int pixelNum=width*height;
    int i,j,k,l;
    float rResult,gResult,bResult;
    float sobel1[9]={1,2,1,0,0,0,-1,-2,-1},sobel2[9]={1,0,-1,2,0,-2,1,0,-1};
    QRgb pixelOld[pixelNum],pixelTemp1[pixelNum],pixelTemp2[pixelNum];
    int rtmp,gtmp,btmp;
    memset(pixelTemp1,255,pixelNum);
    memset(pixelTemp2,255,pixelNum);
    QRgb tmpRgb;
    for(j=0;j<height;j++)
    {
        for(i=0;i<width;i++)
        {
            tmpRgb=image->pixel(i,j);
            pixelOld[j*width+i]=tmpRgb;
        }
    }
    for(j=1;j<height-1;j++)
    {
        for(i=1;i<width-1;i++)
        {
            rResult=0;
            gResult=0;
            bResult=0;
            for(k=0;k<3;k++)
            {
                for(l=0;l<3;l++)
                {
                    rResult+=qRed(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
                    gResult+=qGreen(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
                    bResult+=qBlue(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
                }
            }
            rResult=(float)fabs(rResult);
            gResult=(float)fabs(gResult);
            bResult=(float)fabs(bResult);
            if(rResult>255)
            {
                rtmp=255;
            }
            else
                rtmp=(int)(rResult+0.5);
            if(gResult>255)
                gtmp=255;
            else
                gtmp=(int)(gResult+0.5);
            if(bResult>255)
                btmp=255;
            else
                btmp=(int)(bResult+0.5);
            pixelTemp1[j*width+i]=qRgb(rtmp,gtmp,btmp);
        }
    }

    for(j=1;j<height-1;j++)
    {
        for(i=1;i<width-1;i++)
        {
            rResult=0;
            gResult=0;
            bResult=0;
            for(k=0;k<3;k++)
            {
                for(l=0;l<3;l++)
                {
                    rResult+=qRed(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
                    gResult+=qGreen(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
                    bResult+=qBlue(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
                }
            }
            rResult=(float)fabs(rResult);
            gResult=(float)fabs(gResult);
            bResult=(float)fabs(bResult);
            if(rResult>255)
                rtmp=255;
            else
                rtmp=(int)(rResult+0.5);
            if(gResult>255)
                gtmp=255;
            else
                gtmp=(int)(gResult+0.5);
            if(bResult>255)
                btmp=255;
            else
                btmp=(int)(bResult+0.5);
            pixelTemp2[j*width+i]=qRgb(rtmp,gtmp,btmp);
        }
    }

    for(i=0;i<pixelNum;i++)
    {
        if(pixelTemp2[i]>pixelTemp1[i])
            pixelTemp1[i]=pixelTemp2[i];
    }
    for(j=0;j<height;j++)
    {
        for(i=0;i<width;i++)
        {
            SobelImage.setPixel(i,j,pixelTemp1[j*width+i]);
        }
    }
    delete[] pixelTemp1;
    delete[] pixelTemp2;
    delete image;
}
//将图像的像素信息存在缓存中,并通过调用图像平滑函数更改缓存中的信息
void MainWindow::DecreseVoice()
{
    image = new QImage("C:\\Users\\Administrator\\Desktop\\QTsucai\\timg.jpg");
    int i,j,k,num;
    int width=image->width(),height=image->height();
    QImage MoreClear;
    MoreClear=QImage(width,height,QImage::Format_ARGB32);
    num=width*height;
    QRgb tmpPixel[num];
    int yanKind = 125;
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            k=i*height+j;
            tmpPixel[k]=image->pixel(i,j);
        }
    }
    MoreClearPic(tmpPixel,yanKind,width,height);
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            k=i*height+j;
            MoreClear.setPixel(i,j,tmpPixel[k]);
        }
    }
    ui->label_2->resize(width,height);
    ui->label_2->setPixmap(QPixmap::fromImage(MoreClear));

}
//,图形平滑函数,它通过调用相应的平滑化函数实现图像的不同平滑化
void MainWindow::MoreClearPic(QRgb *tmpPixel,int yanKind,int width,int height)
{
    int i,j,k;
    int num=width*height;
    int rtmpPixel[num],gtmpPixel[num],btmpPixel[num];
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            k=i*height+j;
            rtmpPixel[k]=qRed(tmpPixel[k]);
            gtmpPixel[k]=qGreen(tmpPixel[k]);
            btmpPixel[k]=qBlue(tmpPixel[k]);
        }
    }
    if(yanKind==1)
    {
        YanMo1(rtmpPixel,width,height);
        YanMo1(gtmpPixel,width,height);
        YanMo1(btmpPixel,width,height);
    }
    if(yanKind==2)
    {
        YanMo2(rtmpPixel,width,height);
        YanMo2(gtmpPixel,width,height);
        YanMo2(btmpPixel,width,height);
    }
    /*if(yanKind==3)
    {
        YanMo3(rtmpPixel,width,height);
        YanMo3(gtmpPixel,width,height);
        YanMo3(btmpPixel,width,height);
    }*/
    for(i=1;i<width-1;i++)
    {
        for(j=1;j<height-1;j++)
        {
            k=i*height+j;
            tmpPixel[k]=qRgb(rtmpPixel[k],gtmpPixel[k],btmpPixel[k]);
        }
    }
}
//邻域平均法平滑化函数
void MainWindow::YanMo1(int *tmpPixel, int width, int height)
{
    float Template[9]={1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9};
    int i,j;
    for(i=1;i<width-1;i++)
    {
        for(j=1;j<height-1;j++)
        {
            tmpPixel[i*height+j]=tmpPixel[(i-1)*height+j-1]*Template[0]+tmpPixel[(i-1)*height+j]*Template[1]+
                    tmpPixel[(i-1)*height+j+1]*Template[2]+tmpPixel[i*height+j-1]*Template[3]+tmpPixel[i*height+j]*Template[4]
                    +tmpPixel[i*height+j+1]*Template[5]+tmpPixel[(i+1)*height+j-1]*Template[6]+tmpPixel[(i+1)*height+j]*Template[7]
                    +tmpPixel[(i+1)*height+j+1]*Template[8];
        }
    }
}
//加权平均法平滑化函数
void MainWindow::YanMo2(int *tmpPixel, int width, int height)
{
    float Template[9]={1.0/16,2.0/16,1.0/16,2.0/16,4.0/16,2.0/16,1.0/16,2.0/16,1.0/16};
    int i,j;
    for(i=1;i<width-1;i++)
    {
        for(j=1;j<height-1;j++)
        {
            tmpPixel[i*height+j]=tmpPixel[(i-1)*height+j-1]*Template[0]+tmpPixel[(i-1)*height+j]*Template[1]+
                    tmpPixel[(i-1)*height+j+1]*Template[2]+tmpPixel[i*height+j-1]*Template[3]+tmpPixel[i*height+j]*Template[4]
                    +tmpPixel[i*height+j+1]*Template[5]+tmpPixel[(i+1)*height+j-1]*Template[6]+tmpPixel[(i+1)*height+j]*Template[7]
                    +tmpPixel[(i+1)*height+j+1]*Template[8];
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

vqt5_qt6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值