C++ Qt数字图像处理作业(无Opencv)

C++ Qt数字图像处理作业(无Opencv)

1.界面

数字图像处理老师不让用opencv来实现直方图均衡化、中值滤波、锐化、边缘检测、直方图阈值分割。各个处理模块有参考csdn上大佬的算法。本着分享的精神,上传上来。代码和结构还有很大的优化空间,希望看客么也能给一些建议。

主界面图片
在这里插入图片描述
每一次图片处理的直方图
在这里插入图片描述
边缘检测效果
在这里插入图片描述

2.代码结构

1.pro配置
在这里插入图片描述
在这里插入图片描述
2.代码
代码结构很简单,有打开图片,直方图的显示公共函数,代码中重复的内容很多,还可以进行优化。

在这里插入图片描述
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
   QImage *SmoothMedianRGB(QImage* image);
    QImage* SmoothMedianRGB_ShiZi(QImage* image);
    QImage* SmoothMedianRGB5X5(QImage* image);
    QImage* SmoothMedianRGB_7X7(QImage* image);
    QImage * sharpen(QImage * image);
    QImage *border_detection (QImage *image);
    QImage *roberts_border_detection (QImage *image);
    QImage *sober_border_detection (QImage *image);
    QImage *gradesharpen(QImage * image);
    QImage toGray( QImage *image );
    QImage *MakePicAverage(QImage *image);
   int Sort(int a1[],int n);
private slots:
    void on_pushButton_median_filter_clicked();
    void Zhifangtu(QImage grayimage,QString strname);
    void on_pushButton_median_filter_shizi_clicked();

    void on_pushButton_median_filter_ruihua_clicked();

    void on_pushButton_median_filter_2_clicked();

    void on_pushButton_median_filter_7x7_clicked();

    void on_pushButton_histogram_equ_clicked();

    void on_pushButton_median_filter_ruihua_2_clicked();

    void on_pushButton_median_filter_ruihua_3_clicked();


    void on_pushButton_sobel_clicked();

    void on_pushButton_sobel_2_clicked();



private:
    void File_open();                           // 打开
    Ui::MainWindow *ui;
    QString currentPath;                        // 当前图像的路径
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
     ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->action_open, &QAction::triggered, this, &MainWindow::File_open);
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
    connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));

}
MainWindow::~MainWindow()
{
    delete ui;
}
//*********************************打开图片****************************************
void MainWindow::File_open()
{
    /* 文件选择框 */
    QString path = QFileDialog::getOpenFileName
            (this,"打开文件","C:\\Users\\ming\\Desktop");
    if (!path.isEmpty())                                    // 判断当前路径是否正确
    {
        QImage* img = new QImage();
        if (!(img->load(path))
                )
        {
            QMessageBox::information(this, tr("错误"), tr("打开图像失败"));
            delete img;
            return;
        }
        qDebug() << "打开图片成功";                           // 仅用于检测是否进入if判断
        ui->label->setPixmap(QPixmap::fromImage(*img));
        ui->label->resize(img->size());
        currentPath = path;
        QImage img1;
        img1.load (currentPath);
        Zhifangtu(img1,"直方图 ");
    }
}
//******************************直方图显示****************************************
void MainWindow::Zhifangtu(QImage grayimage, QString strname)
{
    int data[256]{0};
    int yRange = 0;
    int width  = grayimage.width();
    int height = grayimage.height();
    for(int i = 0; i <width; i++)
    {
        for(int j = 0; j < height; j++)
        {
            int index = grayimage.pixelIndex(i, j);
            ++data[index];
        }
    }

    //设置每个“柱”的颜色,值,宽度等
    QBarSet *set = new QBarSet("灰度值 ");
    for (int i=0;i<256;i++) {
       set->append(data[i]);
       if(yRange<data[i]) yRange = data[i];
       set->setColor(QColor::Rgb);
    }
    QBarSeries *series = new QBarSeries();
    series->append(set);
    series->setBarWidth(1.2);

    //QFont fontText("SimHei");
    //fontText.setPixelSize(12);
    //设置横坐标
//    QStringList categories;
//    for(int i=0; i<256; i++)
//        categories.push_back(QString::number(i));
//    QBarCategoryAxis *axisX = new QBarCategoryAxis();
//    axisX->append(categories);
//    axisX->setGridLineColor("transparent");    //参考线颜色透明
//    axisX->setLinePenColor("#5da4f2");         //标尺 线颜色
//    axisX->setLabelsColor("#5da4f2");          //文本颜色
//    axisX->setLabelsFont(fontText);
//    axisX->setTitleText(QStringLiteral("pix value"));
//    axisX->setTitleFont(QFont("宋体"));
//    QBrush brush;
//    brush.setStyle(Qt::SolidPattern);
//    brush.setColor(Qt::red);
//    axisX->setTitleBrush(brush);
    QValueAxis *axisX = new QValueAxis;//数值类型轴(用作Y轴)
    axisX->setLabelFormat("%d");
    axisX->setRange(0,255);

    //设置纵坐标
    QValueAxis *axisY = new QValueAxis;//数值类型轴(用作Y轴)
    axisY->setLabelFormat("%d");
    axisY->setRange(0,yRange);


    //建表
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->createDefaultAxes();
    chart->setAxisX(axisX);
    chart->setAxisY(axisY);
    chart->setTitle("直方");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->legend()->setVisible(false);

    //表的容器
   ui->chartview = new QChartView();
   ui->chartview->setObjectName(QStringLiteral("widget"));
   ui->chartview->setGeometry(QRect(20, 40, 611, 391));
   ui->chartview->setChart(chart);
   ui->chartview->setRenderHint(QPainter::Antialiasing);
   ui->chartview->setVisible(true);
   ui->chartview->setWindowTitle(strname);

}
//*******************************转为灰度图*****************************************
QImage MainWindow::toGray( QImage *image )
{
    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;
}
//************************************锐化**********************************************************
//拉普拉斯
QImage * MainWindow::sharpen(QImage * image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    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(image->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));
        }
    }

    QImage image2 =  toGray(newImage);
    ui->label_ruihuashow->setPixmap(QPixmap::fromImage(image2));         // 显示图像
    ui->label_ruihuashow->resize(image2.width(), image2.height());
    Zhifangtu(image2,"拉普拉斯锐化直方 ");
    return newImage;
}
void MainWindow::on_pushButton_median_filter_ruihua_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    sharpen(img1);
    delete img1;
}
//梯度
QImage * MainWindow::gradesharpen(QImage * image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
        QColor color0;
        QColor color1;
        QColor color2;
        int  r = 0;
        int g = 0;
        int b = 0;

        int r1 = 0;
        int g1 = 0;
        int b1 = 0;

        int r2 = 0;
        int g2 = 0;
        int b2 = 0;

        for( int y = 0; y < image->height() - 1; y++)
           {
               for(int x = 0; x < image->width() - 1; x++){

           color0 =   QColor ( image->pixel(x,y));
           color1 =   QColor ( image->pixel(x + 1,y));
           color2 =   QColor ( image->pixel(x,y + 1));

           r = color0.red();
           g = color0.green();
           b = color0.blue();


           r2 = abs(color0.red() - color1.red());
           g2 = abs(color0.green() - color1.green());
           b2 = abs(color0.blue() - color1.blue());

           r1 = abs(color0.red() - color2.red());
           g1= abs(color0.green() - color2.green());
           b1 = abs(color0.blue() - color2.blue());

            r= r+r1+r2;
            g =g+g1+g2;
            b= b+b1+b2;

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

    QImage image2 =  toGray(newImage);
    ui->label_ruihuashow_2->setPixmap(QPixmap::fromImage(image2));         // 显示图像
    ui->label_ruihuashow_2->resize(image2.width(), image2.height());
    Zhifangtu(image2,"梯度锐化直方 ");
    return newImage;
}
void MainWindow::on_pushButton_median_filter_ruihua_2_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    gradesharpen(img1);
    delete img1;
}
//************************************中值滤波**********************************************************
//中值滤波排序
int MainWindow::Sort(int a1[],int n)
{
    int k;
    int j;
    int c;
    for (int i = 0;i<n;i++)
    {
        k = i;
        for(j = k +1;j<n;j++)
        {
        if (a1[i]>a1[j])
        {
            c = a1[j];
            a1[j] = a1[i];
            a1[i] = c;
        }
        }
    }
    return a1[n/2];
}
//3x3 窗口中值滤波
void MainWindow::on_pushButton_median_filter_clicked()
{
   QImage *img1 = new QImage;
   img1->load (currentPath);
   SmoothMedianRGB(img1);
   delete img1;
}
QImage *MainWindow::SmoothMedianRGB(QImage* image)
{
    QImage *newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int kernel [3][3] = {
        {1,1,1},
        {1,1,1},
        {1,1,1}};

//    {1,1,1,1,1},
//     {1,1,1,1,1},
//         {1,1,1,1,1},
//         {1,1,1,1,1},
//         {1,1,1,1,1}};

    int sizeKernel = 3;
    int sizeKernel1= sizeKernel* sizeKernel;
    int sumKernel = 1;
    QColor color;
     for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
    {
       for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
        {
           int * rgb1 = new int [sizeKernel1];
           int * rgb2 = new int [sizeKernel1];
           int * rgb3 = new int [sizeKernel1];
           int rgb = 0;
            int r = 0;
            int g = 0;
            int b = 0;
            for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
            {
               for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
                {
                 color = QColor(image->pixel(x+i,y+j));
                 rgb1[rgb]=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb2[rgb]=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb3[rgb]=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb ++;
                 if (rgb == sizeKernel1)
                 {
                     r = Sort(rgb1,sizeKernel1);
                     g = Sort(rgb2,sizeKernel1);
                     b = Sort(rgb3,sizeKernel1);
                 }
                }
            }
            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));
            delete [] rgb1;
            delete [] rgb2;
            delete [] rgb3;
        }
    }
       QImage image2 =  toGray(newImage);
         ui->label_2->setPixmap(QPixmap::fromImage(image2));         // 显示图像
         ui->label_2->resize(image2.width(), image2.height());
         Zhifangtu(image2,"矩形3X3");
    return newImage;

}
//3x3 十字 中值滤波
void MainWindow::on_pushButton_median_filter_shizi_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    QImage *newImage =  SmoothMedianRGB_ShiZi(img1);
    QImage image2 =  toGray(newImage);
     ui->label_shizishow->setPixmap(QPixmap::fromImage(image2));         // 显示图像
     ui->label_shizishow->resize(image2.width(), image2.height());
    Zhifangtu(image2,"十字窗口直方");
    delete img1;
}
QImage* MainWindow::SmoothMedianRGB_ShiZi(QImage* image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int kernel [3][3] = {
    {1,1,1},
    {1,1,1},
    {1,1,1}};
    int sizeKernel = 3;
    int sizeKernel1= 5;
    int sumKernel = 1;
    QColor color;
     for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
    {
       for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
        {
           int * rgb1 = new int [sizeKernel1];
           int * rgb2 = new int [sizeKernel1];
           int * rgb3 = new int [sizeKernel1];
           int rgb = 0;
            int r = 0;
            int g = 0;
            int b = 0;
            for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
            {
             int j=0;
                 color = QColor(image->pixel(x+i,y+j));
                 rgb1[rgb]=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb2[rgb]=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb3[rgb]=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb ++;
            }
            for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
            {
             int i=0;
                 color = QColor(image->pixel(x+i,y+j));
                 rgb1[rgb]=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb2[rgb]=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb3[rgb]=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb ++;
            }

             if (rgb == sizeKernel1+1)
             {
                 r = Sort(rgb1,sizeKernel1);
                 g = Sort(rgb2,sizeKernel1);
                 b = Sort(rgb3,sizeKernel1);
             }


            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));
            delete [] rgb1;
            delete [] rgb2;
            delete [] rgb3;
      }
  }
    return newImage;

}

//5x5 矩形窗口中值滤波
QImage* MainWindow::SmoothMedianRGB5X5(QImage* image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int kernel [5][5] = {
//        {1,1,1},
//        {1,1,1},
//        {1,1,1}};

         {1,1,1,1,1},
         {1,1,1,1,1},
         {1,1,1,1,1},
         {1,1,1,1,1},
         {1,1,1,1,1}};

    int sizeKernel = 5;
    int sizeKernel1= sizeKernel* sizeKernel;
    int sumKernel = 1;
    QColor color;
     for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
    {
       for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
        {
           int * rgb1 = new int [sizeKernel1];
           int * rgb2 = new int [sizeKernel1];
           int * rgb3 = new int [sizeKernel1];
           int rgb = 0;
            int r = 0;
            int g = 0;
            int b = 0;
            for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
            {
               for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
                {
                 color = QColor(image->pixel(x+i,y+j));
                 rgb1[rgb]=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb2[rgb]=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb3[rgb]=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb ++;
                 if (rgb == sizeKernel1)
                 {
                     r = Sort(rgb1,sizeKernel1);
                     g = Sort(rgb2,sizeKernel1);
                     b = Sort(rgb3,sizeKernel1);
                 }
                }
            }
            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));
            delete [] rgb1;
            delete [] rgb2;
            delete [] rgb3;
        }
    }


         QImage image2 =  toGray(newImage);
         ui->label_5x5->setPixmap(QPixmap::fromImage(image2));         // 显示图像
         ui->label_5x5->resize(image2.width(), image2.height());
         Zhifangtu(image2,"矩形5X5");
    return newImage;
}
void MainWindow::on_pushButton_median_filter_2_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    SmoothMedianRGB5X5(img1);
    delete img1;
}
//7x7 矩形窗口中值滤波
QImage* MainWindow::SmoothMedianRGB_7X7(QImage* image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int kernel [7][7] = {
//        {1,1,1},
//        {1,1,1},
//        {1,1,1}};

         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1},
         {1,1,1,1,1,1,1}};


    int sizeKernel = 7;
    int sizeKernel1= sizeKernel* sizeKernel;
    int sumKernel = 1;
    QColor color;
     for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
    {
       for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
        {
           int * rgb1 = new int [sizeKernel1];
           int * rgb2 = new int [sizeKernel1];
           int * rgb3 = new int [sizeKernel1];
           int rgb = 0;
            int r = 0;
            int g = 0;
            int b = 0;
            for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
            {
               for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
                {
                 color = QColor(image->pixel(x+i,y+j));
                 rgb1[rgb]=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb2[rgb]=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb3[rgb]=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 rgb ++;
                 if (rgb == sizeKernel1)
                 {
                     r = Sort(rgb1,sizeKernel1);
                     g = Sort(rgb2,sizeKernel1);
                     b = Sort(rgb3,sizeKernel1);
                 }
                }
            }
            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));
            delete [] rgb1;
            delete [] rgb2;
            delete [] rgb3;
        }
    }

         QImage image2 =  toGray(newImage);
         ui->label_7x7->setPixmap(QPixmap::fromImage(image2));         // 显示图像
         ui->label_7x7->resize(image2.width(), image2.height());
         Zhifangtu(image2,"矩形7X7");
    return newImage;
}
void MainWindow::on_pushButton_median_filter_7x7_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    SmoothMedianRGB_7X7(img1);
            delete img1;
}
//************************************直方图均衡化*********************************************************
QImage *MainWindow::MakePicAverage(QImage *image)
{
     QImage *ImageAverage= new 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));
        }
    }
    QImage image2 =  toGray(ImageAverage);
    ui->label_histogram_equ->resize(image2.width(),image2.height());
    ui->label_histogram_equ->setPixmap(QPixmap::fromImage(image2));
    Zhifangtu(image2,"均衡化后 ");
    return ImageAverage;
}
void MainWindow::on_pushButton_histogram_equ_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    MakePicAverage(img1);
    delete img1;
}
//************************************边缘检测*********************************************************
//梯度算子边缘检测
QImage *MainWindow::border_detection (QImage *image)
{

    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    QColor color0;
    QColor color1;
    QColor color2;
    int  r = 0;
    int g = 0;
    int b = 0;
    int rgb = 0;
    int r1 = 0;
    int g1 = 0;
    int b1 = 0;
    int rgb1 = 0;
    int a = 0;
    for( int y = 0; y < image->height() - 1; y++)
    {
        for(int x = 0; x < image->width() - 1; x++)
        {
            color0 =   QColor ( image->pixel(x,y));
            color1 =   QColor ( image->pixel(x + 1,y));
            color2 =   QColor ( image->pixel(x,y + 1));
            r = abs(color0.red() - color1.red());
            g = abs(color0.green() - color1.green());
            b = abs(color0.blue() - color1.blue());
            rgb = r + g + b;

            r1 = abs(color0.red() - color2.red());
            g1= abs(color0.green() - color2.green());
            b1 = abs(color0.blue() - color2.blue());
            rgb1 = r1 + g1 + b1;

            a = rgb + rgb1;
            a = a * 2;
            a = a>255?255:a;

            newImage->setPixel(x,y,qRgb(a,a,a));
        }
    }
    QImage image2 =  toGray(newImage);
    ui->label_ruihuashow_3->setPixmap(QPixmap::fromImage(image2));         // 显示图像
    ui->label_ruihuashow_3->resize(image2.width(), image2.height());
    Zhifangtu(image2,"梯度算子边缘检测直方 ");
    return newImage;

}
void MainWindow::on_pushButton_median_filter_ruihua_3_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
   border_detection(img1);
    delete img1;
}
//Roberts边缘检测
QImage *MainWindow::roberts_border_detection (QImage *image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    QColor color0;
    QColor color1;
    QColor color2;
    QColor color3;
    int  r = 0;
    int g = 0;
    int b = 0;
    int rgb = 0;
    int r1 = 0;
    int g1 = 0;
    int b1 = 0;
    int rgb1 = 0;
    int a = 0;
    for( int y = 0; y < image->height() - 1; y++)
    {
        for(int x = 0; x < image->width() - 1; x++)
        {
            color0 =   QColor ( image->pixel(x,y));
            color1 =   QColor ( image->pixel(x + 1,y));
            color2 =   QColor ( image->pixel(x,y + 1));
            color3 =   QColor ( image->pixel(x + 1,y + 1));
            r = abs(color0.red() - color3.red());
            g = abs(color0.green() - color3.green());
            b = abs(color0.blue() - color3.blue());
            rgb = r + g + b;

            r1 = abs(color1.red() - color2.red());
            g1= abs(color1.green() - color2.green());
            b1 = abs(color1.blue() - color2.blue());
            rgb1 = r1 + g1 + b1;

            a = rgb + rgb1;
            a = a * 1;
            a = a>255?255:a;

            newImage->setPixel(x,y,qRgb(a,a,a));
        }
    }
    QImage image2 =  toGray(newImage);
    ui->label_ruihuashow_4->setPixmap(QPixmap::fromImage(image2));         // 显示图像
    ui->label_ruihuashow_4->resize(image2.width(), image2.height());
    Zhifangtu(image2,"roberts算子边缘检测直方 ");
    return newImage;
  }
void MainWindow::on_pushButton_sobel_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
    roberts_border_detection(img1);
    delete img1;
}
//soberts边缘检测
QImage * MainWindow::sober_border_detection(QImage *image)
{
QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
  QColor color0;
  QColor color1;
  QColor color2;
  QColor color3;
  QColor color4;
  QColor color5;
  QColor color6;
  QColor color7;
  QColor color8;
  int  r = 0;
  int g = 0;
  int b = 0;
  int rgb = 0;
  int r1 = 0;
  int g1 = 0;
  int b1 = 0;
  int rgb1 = 0;
  int a = 0;
  for( int y = 1; y < image->height() - 1; y++)
  {
      for(int x = 1; x < image->width() - 1; x++)
      {
          color0 =   QColor ( image->pixel(x,y));

          color1=   QColor ( image->pixel(x-1,y-1));
          color2 =   QColor ( image->pixel(x,y-1));
          color3 =   QColor ( image->pixel(x+1,y));

          color4 =   QColor ( image->pixel(x-1,y));
          color5 =   QColor ( image->pixel(x+1,y));

          color6 =   QColor ( image->pixel(x-1,y+1));
          color7=   QColor ( image->pixel(x,y+1));
          color8 =   QColor ( image->pixel(x+1,y+1));
          r = abs(color1.red() + color2.red() * 2 + color3.red() - color6.red() - color7.red() * 2 - color8.red());
          g = abs(color1.green() + color2.green() * 2 + color3.green() - color6.green() - color7.green() * 2 - color8.green());
          b = abs(color1.blue() + color2.blue() * 2 + color3.blue() - color6.blue() - color7.blue() * 2 - color8.blue());
          rgb = r + g + b;

          r1 = abs(color1.red() + color4.red() * 2 + color6.red() - color3.red() - color5.red() * 2 - color8.red());
          g1= abs(color1.green() + color4.green() * 2 + color6.green() - color3.green() - color5.green() * 2 - color8.green());
          b1 = abs(color1.blue() + color4.blue() * 2 + color6.blue() - color3.blue() - color5.blue() * 2 - color8.blue());
          rgb1 = r1 + g1 + b1;

          a = (rgb + rgb1)/2;

          a = a>255?255:a;

          newImage->setPixel(x,y,qRgb(a,a,a));
      }
  }

  QImage image2 =  toGray(newImage);
  ui->label_ruihuashow_5->setPixmap(QPixmap::fromImage(image2));         // 显示图像
  ui->label_ruihuashow_5->resize(image2.width(), image2.height());
  Zhifangtu(image2,"sober算子边缘检测直方 ");

  return newImage;
  }
void MainWindow::on_pushButton_sobel_2_clicked()
{
    QImage *img1 = new QImage;
    img1->load (currentPath);
   sober_border_detection(img1);
    delete img1;
}


相关推荐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论 2

打赏作者

四维碎片

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值