Guest User

Untitled

a guest
Apr 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include "mainwindow.h"
  2.  
  3. MainWindow::MainWindow(QWidget *parent) :
  4.     QMainWindow(parent)
  5. {
  6.     QWidget *th=new QWidget(this);
  7.     this->setCentralWidget(th);
  8.  
  9.     wgt=new QPushButton("load",this);
  10.  
  11.     QVBoxLayout *lay=new QVBoxLayout(th);
  12.     lay->addWidget(wgt);
  13.     connect(wgt,SIGNAL(clicked()),this,SLOT(on_open_clicked()));
  14.     start=new QPushButton("start",this);
  15.     lay->addWidget(start);
  16.     connect(start,SIGNAL(clicked()),this,SLOT(on_start_clicked()));
  17.     reload= new QPushButton("reload",this);
  18.     reload->hide();
  19.     connect (reload, SIGNAL(clicked()),this,SLOT(on_reload_clicked()));
  20.  
  21.  
  22. }
  23.  
  24. MainWindow::~MainWindow()
  25. {
  26. }
  27.  
  28. void MainWindow::on_open_clicked()
  29. {
  30.    S = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("open"),"/home/bonapart/Dmitry/Image", "Jpg File (*.jpg)");
  31. }
  32. void MainWindow::on_start_clicked()
  33. {
  34.    ImgREc(S);
  35. }
  36. void MainWindow::ImgREc (QString file)
  37. {
  38.     QRgb temp;
  39.     QImage image(file);
  40.     QRgb hImage[image.width()][image.height()];
  41.     newImage=new QImage(image.width(),image.height(),QImage::Format_RGB32);
  42.     QPainter ps(newImage);
  43.  
  44.     int red=228;
  45.     int green=167;
  46.     int blue=149;
  47.  
  48.         int red2=190;
  49.         int green2=125;
  50.         int blue2=103;
  51.  
  52.         int meanCb=((-0.169)*red)-(0.332*green)+(0.500*blue);
  53.         int meanCr=(0.500*red)-(0.419*green)-(0.81*blue);
  54.             int meanCb2=((-0.169)*red2)-(0.332*green2)+(0.500*blue2);
  55.             int meanCr2=(0.500*red2)-(0.419*green2)-(0.81*blue2);
  56.     int stdCb= (-9);
  57.     int stdCr= 13;
  58.     int factor=1;
  59.     int min_Cb=meanCb-stdCb*factor;
  60.     int max_Cb=meanCb+stdCb*factor;
  61.     int min_Cr=meanCr-stdCr*factor;
  62.     int max_Cr=meanCr+stdCr*factor;
  63.         int min_Cb2=meanCb2-stdCb*factor;
  64.         int max_Cb2=meanCb2+stdCb*factor;
  65.         int min_Cr2=meanCr2-stdCr*factor;
  66.         int max_Cr2=meanCr2+stdCr*factor;
  67.     //int min_Y=meanY-stdY*factor*2;
  68.  
  69.     for (int i=0;i<image.width();i++)
  70.         for (int j=0;j<image.height();j++)
  71.         {
  72.             temp=image.pixel(i,j);
  73.             QRgb r=qRed(temp);
  74.             QRgb g=qGreen(temp);
  75.             QRgb b=qBlue(temp);
  76.                 QRgb r2=qRed(temp);
  77.                 QRgb g2=qGreen(temp);
  78.                 QRgb b2=qBlue(temp);
  79.             int Y=(0.299*r)+(0.587*g)+(0.114*b);
  80.             int Cb=((-0.169)*r)-(0.332*g)+(0.500*b);
  81.             int Cr=(0.500*r)-(0.419*g)-(0.81*b);
  82.             if ((Cr>=min_Cr) && (Cr<=max_Cr))
  83.                 if ((Cb<=min_Cb) && (Cb>=max_Cb))r=255,g=255,b=255;
  84.                  else { r=0,g=0,b=0;}
  85.             else { r=0,g=0,b=0;}
  86.  
  87.                 if ((Cr>=min_Cr2) && (Cr<=max_Cr2))
  88.                     if ((Cb<=min_Cb2) && (Cb>=max_Cb2))r2=255,g2=255,b2=255;
  89.                     else { r2=0,g2=0,b2=0;}
  90.                 else { r2=0,g2=0,b2=0;}
  91.  
  92.                 if ((r==0)&&(g==0)&&(b==0))
  93.                     if ((r2==255)&&(g2==255)&&(b2==255)) r=255,g=255,b=255;
  94.  
  95.  
  96.  
  97.            QRgb temp2=qRgb(r,g,b);
  98.            hImage[i][j]=temp2;
  99.         }
  100.     ps.drawImage(0, 0, image);
  101.     ps.setBrush ( Qt::red );
  102.  
  103.     for (int i=0;i<image.width();i++)
  104.         for (int j=0;j<image.height();j++)
  105.         {
  106.             if (hImage[i][j]==Qt::white)
  107.                 ps.drawPoint  (i,j);
  108.         }
  109.  
  110. wgt->hide();
  111. start->hide();
  112. reload->show();
  113. this->resize(newImage->width(),newImage->height());
  114. }
  115. void MainWindow::on_reload_clicked()
  116. {
  117.     this->resize(200,300);
  118.     wgt->show();
  119.     start->show();
  120.     reload->hide();
  121. }
  122.  
  123. void MainWindow::paintEvent(QPaintEvent *)
  124. {
  125.     if (wgt->isVisible())return;
  126.     QPainter p(this);
  127. ;
  128.     p.drawImage(QPoint(0,0),newImage->scaled(this->width(),this->height()));
  129.     }
Add Comment
Please, Sign In to add comment