Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1742501
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类: C/C++

2013-05-13 09:06:58

原文地址:图像处理之qt实现 作者:亦心亦翼

   申报了学校的新苗计划,主要是针对图像处理的。说实话,真心对图像处理不是很懂,所以最近一直处于充电学习阶段。通过这段时间的基础学习,作为一个图像处理方面的小菜鸟,还是想对图像处理谈一些自己的刍见。
  在我看来,作为图像处理的入门,会文件操作,能顺利得到图像的像素就足够了。但是在这方面做出成就就很难了,因为它涉及了很多算法,要做的出众就要有一定的创新就很难了。像海康这样做安防的,在视频方面做得之所以出众,就是因为他们在图像处理、画面处理方面的算法都有自己的创新。
   下面是这段时间,我关于图像处理方面的一些入门算法的实现代码,使用qt实现的,为自己后期实现图像处理(如:图像变形、分割等)做准备,代码分享如下:
 

点击(此处)折叠或打开

  1. #include \"mainwindow.h\"
  2. #include \"ui_mainwindow.h\"
  3. #include <QFileDialog>
  4. #include <QMovie>
  5. #include <QRgb>
  6. #include <math.h>
  7. #include <QInputDialog>
  8. #include <QDebug>
  9. #include <QDialog>
  10. #include <QDoubleSpinBox>
  11. #include <QSpinBox>
  12. #include <QLabel>

  13. MainWindow::MainWindow(QWidget *parent) :
  14.     QMainWindow(parent),
  15.     ui(new Ui::MainWindow)
  16. {
  17.     ui->setupUi(this);
  18.     QMenu *editMenu=ui->menuBar->addMenu(QObject::tr(\"图片(&P)\"));
  19.     QAction *action_Open=editMenu->addAction(QObject::tr(\"打开图片&O\"));
  20.     QMenu *editMenuMake=ui->menuBar->addMenu(QObject::tr(\"图像几何变换(&M)\"));
  21.     QAction *action_Make1=editMenuMake->addAction(QObject::tr(\"图像平移(&T)\"));
  22.     QAction *action_Make2=editMenuMake->addAction(QObject::tr(\"图像放缩(&S)\"));
  23.     QMenu *editMenuBetter=ui->menuBar->addMenu(QObject::tr(\"图像增强(&B)\"));
  24.     QAction *action_Better1=editMenuBetter->addAction(QObject::tr(\"图像锐化(&E)\"));
  25.     QAction *action_Better2=editMenuBetter->addAction(QObject::tr(\"直方图均衡化(&A)\"));
  26.     QAction *action_Better3=editMenuBetter->addAction(QObject::tr(\"消除噪声(&D)\"));
  27.     QMenu *editMenuTiqu=ui->menuBar->addMenu(QObject::tr(\"提取(&G)\"));
  28.     QAction *action_Gray=editMenuTiqu->addAction(QObject::tr(\"图像灰度化\"));
  29.     QAction *action_Get1=editMenuTiqu->addAction(QObject::tr(\"Sobel算子提取\"));
  30.     action_Open->setCheckable(true);
  31.     action_Make1->setCheckable(true);
  32.     action_Make2->setCheckable(true);
  33.     action_Better1->setCheckable(true);
  34.     action_Better2->setCheckable(true);
  35.     action_Better3->setCheckable(true);
  36.     action_Gray->setCheckable(true);
  37.     action_Get1->setCheckable(true);
  38.     connect(action_Open,SIGNAL(triggered()),this,SLOT(OpenPicture()));
  39.     connect(action_Make1,SIGNAL(triggered()),this,SLOT(SetChangeValue()));
  40.     connect(action_Make2,SIGNAL(triggered()),this,SLOT(SetSouValue()));
  41.     connect(action_Better1,SIGNAL(triggered()),this,SLOT(SetBetterValue()));
  42.     connect(action_Better2,SIGNAL(triggered()),this,SLOT(MakePicAverage()));
  43.     connect(action_Gray,SIGNAL(triggered()),this,SLOT(RgbToGray()));
  44.     connect(action_Get1,SIGNAL(triggered()),this,SLOT(SobelGet()));
  45.     connect(action_Better3,SIGNAL(triggered()),this,SLOT(SelectYanKind()));
  46. }

  47. MainWindow::~MainWindow()
  48. {
  49.     delete ui;
  50. }

  51. //获取要处理的图像并显示出来
  52. void MainWindow::OpenPicture()
  53. {
  54.     fileName=QFileDialog::getOpenFileName( this,tr(\"打开文件\"),\"/usr/local/Trolltech\",
  55.                                                    tr(\"任何文件(*.*)\"\";;文本文件(*.txt)\"\";;XML文件(*.xml)\"\";;Images (*.png *.xpm *.jpg)\"));
  56.     QMovie *move=new QMovie(fileName);
  57.     ui->label->move(50,100);
  58.     ui->label->setMovie(move);
  59.     move->start();
  60. }

  61. //确定图像左右、上下的平移量,并触发平移函数
  62. void MainWindow::SetChangeValue()
  63. {
  64.     dialog=new QDialog(this);
  65.     QLabel *WidthLabel=new QLabel(dialog);
  66.     WidthLabel->move(30,50);
  67.     WidthLabel->setText(tr(\"设置宽度的平移量\"));
  68.     setWidthChange=new QSpinBox(dialog);
  69.     setWidthChange->setMaximum(20);
  70.     setWidthChange->setMinimum(-20);
  71.     setWidthChange->move(180,50);
  72.     QLabel *HeightLabel=new QLabel(dialog);
  73.     HeightLabel->move(30,80);
  74.     HeightLabel->setText(tr(\"设置高度的平移量\"));
  75.     setHeightChange=new QSpinBox(dialog);
  76.     setHeightChange->setMaximum(20);
  77.     setHeightChange->setMinimum(-20);
  78.     setHeightChange->move(180,80);
  79.     QPushButton *button=new QPushButton(dialog);
  80.     button->move(180,120);
  81.     button->setText(tr(\"确定\"));
  82.     dialog->show();
  83.     connect(button,SIGNAL(clicked()),this,SLOT(ShowChangePic()));
  84. }
  85. //平移函数
  86. void MainWindow::ShowChangePic()
  87. {

  88.     image=new QImage(fileName);
  89.     QImage ChangeImage;
  90.     ChangeImage=QImage(image->width(),image->height(),QImage::Format_ARGB32);
  91.     QRgb rgb;
  92.     int width,height;
  93.     int i,j;
  94.     int widthOffset,heightOffset;
  95.     width=image->width();
  96.     height=image->height();
  97.     widthOffset=setWidthChange->value();
  98.     heightOffset=setHeightChange->value();
  99.     for(i=0;i<width;i++)
  100.     {
  101.         for(j=0;j<height;j++)
  102.         {
  103.             rgb=image->pixel(i,j);
  104.             if(ChangeImage.valid(i+widthOffset,j+heightOffset))
  105.             {
  106.                 ChangeImage.setPixel(i+widthOffset,j+heightOffset,rgb);
  107.             }
  108.         }
  109.      }
  110.      ui->label_2->resize(ChangeImage.width(),ChangeImage.height());
  111.      ui->label_2->setPixmap(QPixmap::fromImage(ChangeImage));
  112.      delete image;
  113.      delete dialog;
  114. }

  115. //设置图像伸缩的比例并触发伸缩函数
  116. void MainWindow::SetSouValue()
  117. {
  118.     dialog=new QDialog(this);
  119.     QLabel *WidthLabel=new QLabel(dialog);
  120.     WidthLabel->move(30,50);
  121.     WidthLabel->setText(tr(\"设置宽度的缩放比例\"));
  122.     setWidthBi=new QDoubleSpinBox(dialog);
  123.     setWidthBi->setMaximum(20);
  124.     setWidthBi->setMinimum(0.1);
  125.     setWidthBi->move(180,50);
  126.     QLabel *HeightLabel=new QLabel(dialog);
  127.     HeightLabel->move(30,80);
  128.     HeightLabel->setText(tr(\"设置高度的缩放比例\"));
  129.     setHeightBi=new QDoubleSpinBox(dialog);
  130.     setHeightBi->setMaximum(20);
  131.     setHeightBi->setMinimum(0.1);
  132.     setHeightBi->move(180,80);
  133.     QPushButton *button=new QPushButton(dialog);
  134.     button->move(180,120);
  135.     button->setText(tr(\"确定\"));
  136.     dialog->show();
  137.     connect(button,SIGNAL(clicked()),this,SLOT(ShowSouPic()));
  138. }
  139. //伸缩函数
  140. void MainWindow::ShowSouPic()
  141. {
  142.     int width,height;
  143.     image=new QImage(fileName);
  144.     width=image->width();
  145.     height=image->height();
  146.     double x,y,r1,r2,g1,g2,b1,b2;
  147.     int i,j;
  148.     double width_bi,height_bi;
  149.     QRgb rgb00,rgb01,rgb10,rgb11;
  150.     int r,g,b;
  151.     QImage SouImage;
  152.     width_bi=setWidthBi->value();
  153.     height_bi=setHeightBi->value();
  154.     SouImage=QImage(width*width_bi,height*height_bi,QImage::Format_ARGB32);
  155.     for(i=0;i<width*width_bi;i++)
  156.     {
  157.         for(j=0;j<height*height_bi;j++)
  158.         {
  159.             x=i*(1/width_bi);
  160.             y=j*(1/height_bi);

  161.             //边界采用单线性插值
  162.             if(ceil(x)==0&&ceil(y)!=0)
  163.             {
  164.                 rgb00=image->pixel(0,ceil(y)-1);
  165.                 rgb01=image->pixel(0,ceil(y));
  166.                 r=(ceil(y)-y)*qRed(rgb00)+(y-(ceil(y)-1))*qRed(rgb01);
  167.                 g=(ceil(y)-y)*qGreen(rgb00)+(y-(ceil(y)-1))*qGreen(rgb01);
  168.                 b=(ceil(y)-y)*qBlue(rgb00)+(y-(ceil(y)-1))*qBlue(rgb01);
  169.                 SouImage.setPixel(i,j,qRgb(r,g,b));
  170.             }

  171.             if(ceil(y)==0&&ceil(y)!=0)
  172.             {
  173.                 rgb00=image->pixel(ceil(x)-1,0);
  174.                 rgb10=image->pixel(ceil(x),0);
  175.                 r=(ceil(x)-x)*qRed(rgb00)+(x-(ceil(x)-1))*qRed(rgb10);
  176.                 g=(ceil(x)-x)*qGreen(rgb00)+(x-(ceil(x)-1))*qGreen(rgb10);
  177.                 b=(ceil(x)-x)*qBlue(rgb00)+(x-(ceil(x)-1))*qBlue(rgb10);
  178.                 SouImage.setPixel(i,j,qRgb(r,g,b));
  179.             }

  180.             //(0,0)点特殊处理
  181.             if(ceil(y)==0&&ceil(y)==0)
  182.             {
  183.                 rgb00=image->pixel(0,0);
  184.                 SouImage.setPixel(i,j,rgb00);
  185.             }

  186.             //非边界采用双线性插值
  187.             if(ceil(x)!=0&&ceil(y)!=0)
  188.             {
  189.                 rgb00=image->pixel(ceil(x)-1,ceil(y)-1);
  190.                 rgb01=image->pixel(ceil(x)-1,ceil(y));
  191.                 rgb10=image->pixel(ceil(x),ceil(y)-1);
  192.                 rgb11=image->pixel(ceil(x),ceil(y));

  193.                 r1=(ceil(x)-x)*qRed(rgb00)+(x-(ceil(x)-1))*qRed(rgb10);
  194.                 r2=(ceil(x)-x)*qRed(rgb01)+(x-(ceil(x)-1))*qRed(rgb11);
  195.                 r=(int)((ceil(y)-y)*r1+(y-(ceil(y)-1))*r2);

  196.                 g1=(ceil(x)-x)*qGreen(rgb00)+(x-(ceil(x)-1))*qGreen(rgb10);
  197.                 g2=(ceil(x)-x)*qGreen(rgb01)+(x-(ceil(x)-1))*qGreen(rgb11);
  198.                 g=(int)((ceil(y)-y)*g1+(y-(ceil(y)-1))*g2);

  199.                 b1=(ceil(x)-x)*qBlue(rgb00)+(x-(ceil(x)-1))*qBlue(rgb10);
  200.                 b2=(ceil(x)-x)*qBlue(rgb01)+(x-(ceil(x)-1))*qBlue(rgb11);
  201.                 b=(int)((ceil(y)-y)*b1+(y-(ceil(y)-1))*b2);

  202.                 SouImage.setPixel(i,j,qRgb(r,g,b));
  203.             }
  204.         }
  205.     }
  206.     ui->label_2->resize(SouImage.width(),SouImage.height());
  207.     ui->label_2->setPixmap(QPixmap::fromImage(SouImage));
  208.     delete image;
  209.     delete dialog;
  210. }

  211. //设置图像锐化的阈值并触发图像锐化函数
  212. void MainWindow::SetBetterValue()
  213. {
  214.     dialog=new QDialog(this);
  215.     QLabel *WidthLabel=new QLabel(dialog);
  216.     WidthLabel->move(30,50);
  217.     WidthLabel->setText(tr(\"设置锐化的阈值\"));
  218.     setBetterValue=new QSpinBox(dialog);
  219.     setBetterValue->setMaximum(100);
  220.     setBetterValue->setMinimum(10);
  221.     setBetterValue->move(180,50);
  222.     QPushButton *button=new QPushButton(dialog);
  223.     button->move(180,70);
  224.     button->setText(tr(\"确定\"));
  225.     dialog->show();
  226.     connect(button,SIGNAL(clicked()),this,SLOT(MakePicBetter()));
  227. }
  228. //图像锐化
  229. void MainWindow::MakePicBetter()
  230. {
  231.     image=new QImage(fileName);
  232.     QImage ImageBetter;
  233.     ImageBetter=QImage(image->width(),image->height(),QImage::Format_ARGB32);
  234.     int i,j;
  235.     int r,g,b,tmpR,tmpG,tmpB;
  236.     QRgb rgb00,rgb01,rgb10;
  237.     int width=image->width(),height=image->height();
  238.     int betterValue=setBetterValue->value();
  239.     for(i=0;i<width;i++)
  240.     {
  241.         for(j=0;j<height;j++)
  242.         {
  243.             if(image->valid(i,j)&&image->valid(i+1,j)&&image->valid(i,j+1))
  244.             {
  245.                 rgb00=image->pixel(i,j);
  246.                 rgb01=image->pixel(i,j+1);
  247.                 rgb10=image->pixel(i+1,j);
  248.                 r=qRed(rgb00);
  249.                 g=qGreen(rgb00);
  250.                 b=qBlue(rgb00);
  251.                 tmpR=abs(qRed(rgb00)-qRed(rgb01))+abs(qRed(rgb00)-qRed(rgb10));
  252.                 tmpG=abs(qGreen(rgb00)-qGreen(rgb01))+abs(qGreen(rgb00)-qGreen(rgb10));
  253.                 tmpB=abs(qBlue(rgb00)-qBlue(rgb01))+abs(qBlue(rgb00)-qBlue(rgb10));
  254.                 if((tmpR+120)<255)
  255.                 {
  256.                     if(tmpR>betterValue)
  257.                     {
  258.                         r=tmpR+120;
  259.                     }
  260.                 }
  261.                 else
  262.                 {
  263.                     r=255;
  264.                 }

  265.                 if((tmpG+120)<255)
  266.                 {
  267.                     if(tmpG>betterValue)
  268.                     {
  269.                         g=tmpG+120;
  270.                     }
  271.                 }
  272.                 else
  273.                 {
  274.                     g=255;
  275.                 }

  276.                 if((tmpB+120)<255)
  277.                 {
  278.                     if(tmpB>betterValue)
  279.                     {
  280.                         b=tmpB+120;
  281.                     }
  282.                 }
  283.                 else
  284.                 {
  285.                     b=255;
  286.                 }
  287.                 ImageBetter.setPixel(i,j,qRgb(r,g,b));
  288.         }
  289.         }
  290.     }
  291.     ui->label_2->resize(ImageBetter.width(),ImageBetter.height());
  292.     ui->label_2->setPixmap(QPixmap::fromImage(ImageBetter));
  293.     delete image;
  294.     delete dialog;
  295. }
  296. //图像的直方图均衡化
  297. void MainWindow::MakePicAverage()
  298. {
  299.     image=new QImage(fileName);
  300.     QImage ImageAverage;
  301.     ImageAverage=QImage(image->width(),image->height(),QImage::Format_ARGB32);
  302.     int i,j;
  303.     int width,height;
  304.     width=image->width();
  305.     height=image->height();
  306.     QRgb rgb;
  307.     int r[256],g[256],b[256];//原图各个灰度数量的统计
  308.     int rtmp,gtmp,btmp,rj,gj,bj;
  309.     float rPro[256],gPro[256],bPro[256];//原图各个灰度级的概率
  310.     float rTemp[256],gTemp[256],bTemp[256];//均衡化后各个灰度级的概率
  311.     int rJun[256],gJun[256],bJun[256];//均衡化后对应像素的值
  312.     memset(r,0,sizeof(r));
  313.     memset(g,0,sizeof(g));
  314.     memset(b,0,sizeof(b));

  315.     //获取原图各个灰度的数量
  316.     for(i=0;i<width;i++)
  317.     {
  318.         for(j=0;j<height;j++)
  319.         {
  320.             rgb=image->pixel(i,j);
  321.             r[qRed(rgb)]++;
  322.             g[qGreen(rgb)]++;
  323.             b[qBlue(rgb)]++;
  324.         }
  325.     }

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

  333.     //均衡化后各个灰度级的概率,同时获取均衡化后对应像素的值
  334.     for(i=0;i<256;i++)
  335.     {
  336.         if(i==0)
  337.         {
  338.             rTemp[0]=rPro[0];
  339.             gTemp[0]=gPro[0];
  340.             bTemp[0]=bPro[0];
  341.         }
  342.         else
  343.         {
  344.             rTemp[i]=rTemp[i-1]+rPro[i];
  345.             gTemp[i]=gTemp[i-1]+gPro[i];
  346.             bTemp[i]=bTemp[i-1]+bPro[i];
  347.         }
  348.         rJun[i]=(int)(255*rTemp[i]+0.5);
  349.         gJun[i]=(int)(255*gTemp[i]+0.5);
  350.         bJun[i]=(int)(255*bTemp[i]+0.5);
  351.     }

  352.     for(i=0;i<width;i++)
  353.     {
  354.         for(j=0;j<height;j++)
  355.         {
  356.             rgb=image->pixel(i,j);
  357.             rtmp=qRed(rgb);
  358.             gtmp=qGreen(rgb);
  359.             btmp=qBlue(rgb);
  360.             rj=rJun[rtmp];
  361.             gj=gJun[gtmp];
  362.             bj=bJun[btmp];
  363.             ImageAverage.setPixel(i,j,qRgb(rj,gj,bj));
  364.         }
  365.     }
  366.     ui->label_2->resize(ImageAverage.width(),ImageAverage.height());
  367.     ui->label_2->setPixmap(QPixmap::fromImage(ImageAverage));
  368.     delete image;
  369. }

  370. //Sobel算子提取图像边界
  371. void MainWindow::SobelGet()
  372. {
  373.     image=new QImage(fileName);
  374.     QImage SobelIma;
  375.     SobelIma=QImage(image->width(),image->height(),QImage::Format_ARGB32);
  376.     Template(SobelIma);
  377.     ui->label_2->resize(SobelIma.width(),SobelIma.height());
  378.     ui->label_2->setPixmap(QPixmap::fromImage(SobelIma));
  379. }
  380. //与算子进行卷积的函数
  381. void MainWindow::Template(QImage &SobelImage)
  382. {
  383.     int width=image->width(),height=image->height();
  384.     int pixelNum=width*height;
  385.     int i,j,k,l;
  386.     float rResult,gResult,bResult;
  387.     float sobel1[9]={1,2,1,0,0,0,-1,-2,-1},sobel2[9]={1,0,-1,2,0,-2,1,0,-1};
  388.     QRgb pixelOld[pixelNum],pixelTemp1[pixelNum],pixelTemp2[pixelNum];
  389.     int rtmp,gtmp,btmp;
  390.     memset(pixelTemp1,255,pixelNum);
  391.     memset(pixelTemp2,255,pixelNum);
  392.     QRgb tmpRgb;
  393.     for(j=0;j<height;j++)
  394.     {
  395.         for(i=0;i<width;i++)
  396.         {
  397.             tmpRgb=image->pixel(i,j);
  398.             pixelOld[j*width+i]=tmpRgb;
  399.         }
  400.     }
  401.     for(j=1;j<height-1;j++)
  402.     {
  403.         for(i=1;i<width-1;i++)
  404.         {
  405.             rResult=0;
  406.             gResult=0;
  407.             bResult=0;
  408.             for(k=0;k<3;k++)
  409.             {
  410.                 for(l=0;l<3;l++)
  411.                 {
  412.                     rResult+=qRed(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
  413.                     gResult+=qGreen(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
  414.                     bResult+=qBlue(pixelOld[(j-1+k)*width+(i-1+l)])*sobel1[k*3+l];
  415.                 }
  416.             }
  417.             rResult=(float)fabs(rResult);
  418.             gResult=(float)fabs(gResult);
  419.             bResult=(float)fabs(bResult);
  420.             if(rResult>255)
  421.             {
  422.                 rtmp=255;
  423.             }
  424.             else
  425.                 rtmp=(int)(rResult+0.5);
  426.             if(gResult>255)
  427.                 gtmp=255;
  428.             else
  429.                 gtmp=(int)(gResult+0.5);
  430.             if(bResult>255)
  431.                 btmp=255;
  432.             else
  433.                 btmp=(int)(bResult+0.5);
  434.             pixelTemp1[j*width+i]=qRgb(rtmp,gtmp,btmp);
  435.         }
  436.     }

  437.     for(j=1;j<height-1;j++)
  438.     {
  439.         for(i=1;i<width-1;i++)
  440.         {
  441.             rResult=0;
  442.             gResult=0;
  443.             bResult=0;
  444.             for(k=0;k<3;k++)
  445.             {
  446.                 for(l=0;l<3;l++)
  447.                 {
  448.                     rResult+=qRed(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
  449.                     gResult+=qGreen(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
  450.                     bResult+=qBlue(pixelOld[(j-1+k)*width+(i-1+l)])*sobel2[k*3+l];
  451.                 }
  452.             }
  453.             rResult=(float)fabs(rResult);
  454.             gResult=(float)fabs(gResult);
  455.             bResult=(float)fabs(bResult);
  456.             if(rResult>255)
  457.                 rtmp=255;
  458.             else
  459.                 rtmp=(int)(rResult+0.5);
  460.             if(gResult>255)
  461.                 gtmp=255;
  462.             else
  463.                 gtmp=(int)(gResult+0.5);
  464.             if(bResult>255)
  465.                 btmp=255;
  466.             else
  467.                 btmp=(int)(bResult+0.5);
  468.             pixelTemp2[j*width+i]=qRgb(rtmp,gtmp,btmp);
  469.         }
  470.     }

  471.     for(i=0;i<pixelNum;i++)
  472.     {
  473.         if(pixelTemp2[i]>pixelTemp1[i])
  474.             pixelTemp1[i]=pixelTemp2[i];
  475.     }
  476.     for(j=0;j<height;j++)
  477.     {
  478.         for(i=0;i<width;i++)
  479.         {
  480.             SobelImage.setPixel(i,j,pixelTemp1[j*width+i]);
  481.         }
  482.     }
  483.     delete[] pixelTemp1;
  484.     delete[] pixelTemp2;
  485.     delete image;
  486. }

  487. //设置图像平滑的方法
  488. void MainWindow::SelectYanKind()
  489. {
  490.     dialog=new QDialog(this);
  491.     QLabel *Label=new QLabel(dialog);
  492.     Label->move(45,20);
  493.     Label->setText(tr(\"设置图像平滑的方法\"));
  494.     QLabel *DescLabel=new QLabel(dialog);
  495.     DescLabel->move(60,45);
  496.     DescLabel->setText(tr(\" 1--邻域平均法\\n 2--加权平均法\\n 3--选择式掩膜平滑\\n\"));
  497.     setKind=new QSpinBox(dialog);
  498.     setKind->setMaximum(3);
  499.     setKind->setMinimum(1);
  500.     setKind->move(65,100);
  501.     QPushButton *button=new QPushButton(dialog);
  502.     button->move(65,130);
  503.     button->setText(tr(\"确定\"));
  504.     dialog->show();
  505.     connect(button,SIGNAL(clicked()),this,SLOT(DecreseVoice()));
  506. }
  507. //将图像的像素信息存在缓存中,并通过调用图像平滑函数更改缓存中的信息
  508. void MainWindow::DecreseVoice()
  509. {
  510.     image=new QImage(fileName);
  511.     int i,j,k,num;
  512.     int width=image->width(),height=image->height();
  513.     QImage MoreClear;
  514.     MoreClear=QImage(width,height,QImage::Format_ARGB32);
  515.     num=width*height;
  516.     QRgb tmpPixel[num];
  517.     int yanKind=setKind->value();
  518.     for(i=0;i<width;i++)
  519.     {
  520.         for(j=0;j<height;j++)
  521.         {
  522.             k=i*height+j;
  523.             tmpPixel[k]=image->pixel(i,j);
  524.         }
  525.     }
  526.     MoreClearPic(tmpPixel,yanKind,width,height);
  527.     for(i=0;i<width;i++)
  528.     {
  529.         for(j=0;j<height;j++)
  530.         {
  531.             k=i*height+j;
  532.             MoreClear.setPixel(i,j,tmpPixel[k]);
  533.         }
  534.     }
  535.     ui->label_2->resize(width,height);
  536.     ui->label_2->setPixmap(QPixmap::fromImage(MoreClear));
  537.     delete dialog;
  538. }
  539. //,图形平滑函数,它通过调用相应的平滑化函数实现图像的不同平滑化
  540. void MainWindow::MoreClearPic(QRgb *tmpPixel,int yanKind,int width,int height)
  541. {
  542.     int i,j,k;
  543.     int num=width*height;
  544.     int rtmpPixel[num],gtmpPixel[num],btmpPixel[num];
  545.     for(i=0;i<width;i++)
  546.     {
  547.        for(j=0;j<height;j++)
  548.        {
  549.            k=i*height+j;
  550.            rtmpPixel[k]=qRed(tmpPixel[k]);
  551.            gtmpPixel[k]=qGreen(tmpPixel[k]);
  552.            btmpPixel[k]=qBlue(tmpPixel[k]);
  553.        }
  554.     }
  555.     if(yanKind==1)
  556.     {
  557.         YanMo1(rtmpPixel,width,height);
  558.         YanMo1(gtmpPixel,width,height);
  559.         YanMo1(btmpPixel,width,height);
  560.     }
  561.     if(yanKind==2)
  562.     {
  563.         YanMo2(rtmpPixel,width,height);
  564.         YanMo2(gtmpPixel,width,height);
  565.         YanMo2(btmpPixel,width,height);
  566.     }
  567.     /*if(yanKind==3)
  568.     {
  569.         YanMo3(rtmpPixel,width,height);
  570.         YanMo3(gtmpPixel,width,height);
  571.         YanMo3(btmpPixel,width,height);
  572.     }*/
  573.     for(i=1;i<width-1;i++)
  574.     {
  575.         for(j=1;j<height-1;j++)
  576.         {
  577.             k=i*height+j;
  578.             tmpPixel[k]=qRgb(rtmpPixel[k],gtmpPixel[k],btmpPixel[k]);
  579.         }
  580.     }
  581. }
  582. //邻域平均法平滑化函数
  583. void MainWindow::YanMo1(int *tmpPixel, int width, int height)
  584. {
  585.     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};
  586.     int i,j;
  587.     for(i=1;i<width-1;i++)
  588.     {
  589.        for(j=1;j<height-1;j++)
  590.        {
  591.          tmpPixel[i*height+j]=tmpPixel[(i-1)*height+j-1]*Template[0]+tmpPixel[(i-1)*height+j]*Template[1]+
  592.          tmpPixel[(i-1)*height+j+1]*Template[2]+tmpPixel[i*height+j-1]*Template[3]+tmpPixel[i*height+j]*Template[4]
  593.          +tmpPixel[i*height+j+1]*Template[5]+tmpPixel[(i+1)*height+j-1]*Template[6]+tmpPixel[(i+1)*height+j]*Template[7]
  594.          +tmpPixel[(i+1)*height+j+1]*Template[8];
  595.        }
  596.     }
  597. }
  598. //加权平均法平滑化函数
  599. void MainWindow::YanMo2(int *tmpPixel, int width, int height)
  600. {
  601.     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};
  602.     int i,j;
  603.     for(i=1;i<width-1;i++)
  604.     {
  605.        for(j=1;j<height-1;j++)
  606.        {
  607.          tmpPixel[i*height+j]=tmpPixel[(i-1)*height+j-1]*Template[0]+tmpPixel[(i-1)*height+j]*Template[1]+
  608.          tmpPixel[(i-1)*height+j+1]*Template[2]+tmpPixel[i*height+j-1]*Template[3]+tmpPixel[i*height+j]*Template[4]
  609.          +tmpPixel[i*height+j+1]*Template[5]+tmpPixel[(i+1)*height+j-1]*Template[6]+tmpPixel[(i+1)*height+j]*Template[7]
  610.          +tmpPixel[(i+1)*height+j+1]*Template[8];
  611.        }
  612.     }
  613. }


阅读(598) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~