SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QtWidgets>
  2. #ifndef QT_NO_PRINTER
  3. #include <QPrintDialog>
  4. #endif
  5. #include<iostream>
  6.  
  7.  
  8. #include "imageviewer-qt5.h"
  9.  
  10. ImageViewer::ImageViewer()
  11. {
  12.  
  13.     image=NULL;
  14.     widthX = 0;
  15.     resize(1600, 600);
  16.  
  17.     startLogging();
  18.  
  19.     generateMainGui();
  20.     renewLogging();
  21.  
  22.     generateControlPanels();
  23.         createActions();
  24.         createMenus();
  25.  
  26.         resize(QGuiApplication::primaryScreen()->availableSize() * 0.85 );
  27.  
  28. }
  29.  
  30. void ImageViewer::getMittlereHelligkeit()
  31. {
  32.     tempImage2 = image;
  33.     tempImage = *tempImage2;
  34.  
  35.     for(int i = 0 ; i < tempImage2->height(); i++){
  36.         for(int j = 0 ; j < tempImage2->width(); j++){
  37.             tempLightness += qGray(tempImage2->pixel(j,i));
  38.         }
  39.     }
  40.     renewLogging();
  41.     tempLightness = tempLightness / (tempImage2->height() * tempImage2->width());
  42.     QString str = "Mittlere helligkeit = " + QString::number(tempLightness);
  43.     mittlereHelligkeit->setText(str);
  44.     getVarianz();
  45. }
  46.  
  47. void ImageViewer::getVarianz(){
  48.     for(int i = 0 ; i < tempImage2->height(); i++){
  49.         for(int j = 0 ; j < tempImage2->width(); j++){
  50.             tempVarianz += pow((qGray(tempImage2->pixel(j,i)) - tempLightness),2);
  51.         }
  52.     }
  53.     tempVarianz = tempVarianz / (tempImage2->height() * tempImage2->width());
  54.     tempVarianz = sqrt(tempVarianz);
  55.     QString str = "Varianz = " + QString::number(tempVarianz);
  56.     varianz->setText(str);
  57. }
  58.  
  59. void ImageViewer::erstelleHistogramm(){
  60.     //fülle map mit 0 werten
  61.     for(int h = 0 ;h<256;h++){
  62.         histogramm[h] = 0;
  63.     }
  64.     //zähle die helligkeit aller pixel
  65.     for(int i = 0 ; i < tempImage2->height(); i++){
  66.         for(int j = 0 ; j < tempImage2->width(); j++){
  67.             histogramm[qGray(tempImage2->pixel(j,i))] = histogramm.value(qGray(tempImage2->pixel(j,i)))+1;
  68.         }
  69.     }
  70.     //whiteout mache das bild weiss
  71. loadFileExt("/home/stud/nwd-k92/Schreibtisch/CG2 2019/Aufgabe 1/white.jpg");
  72.  
  73.     //mahle die balken ins bild
  74.     QRgb tempcolor = qRgb(0,0,1);
  75.     int temppos = 0;
  76.     int maxsize = 0;
  77.     //ermitle grösten wert
  78.     for(int t = 0;t<histogramm.size();t++){
  79.         if(maxsize<histogramm.value(t)){
  80.             maxsize = histogramm.value(t);
  81.         }
  82.     }
  83.     //maxsize ist jetzt ein skalar um die balken in eine 200 pixel hohe bild rein zu bekommen
  84.     maxsize = maxsize / image->height();
  85.     //zeichne für jeden wert aus histogramm eine 2 pixel breiten und eine wert / maxsize grossen
  86.     //balken
  87.     for(int u = 0;u<histogramm.size();u++){
  88.         logFile << u << " = " << histogramm.value(u) << std::endl;
  89.         for(int g = 0; g<(int)(histogramm.value(u)/maxsize);g++){
  90.             image->setPixel(temppos, g,tempcolor);
  91.             image->setPixel(temppos+1, g,tempcolor);
  92.         }
  93.         temppos = temppos + 2;
  94.     }
  95.     updateImageDisplay();
  96.     logFile << "histogram erstellt" << std::endl;
  97.     renewLogging();
  98. }
  99.  
  100. void ImageViewer::kontrastAnpassen(int h)
  101. {
  102.     mult = 1 + (double)h/100;
  103.     resetImage();
  104.         for(int i = 0 ; i <image->height();i++){
  105.             for(int j = 0; j<image->width();j++){
  106.                 tempPixel = image->pixel(j,i);
  107.  
  108.                 y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
  109.                 Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
  110.                 Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
  111.  
  112.                 y = y * mult;
  113.                 Cb = Cb - 128;
  114.                 Cr = Cr - 128;
  115.  
  116.                 tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
  117.                 tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
  118.                 tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
  119.  
  120.                 if( tRed>255 ){tRed = 255;}
  121.                 if( tRed<0 ){tRed = 0;}
  122.                 if( tGreen>255 ){tGreen = 255;}
  123.                 if( tGreen<0 ){tGreen = 0;}
  124.                 if( tBlue>255 ){tBlue = 255;}
  125.                 if( tBlue<0 ){tBlue = 0;}
  126.  
  127.  
  128.                 image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  129.             }
  130.         }
  131.         updateImageDisplay();
  132. }
  133.  
  134. void ImageViewer::helligkeitAnpassen(int h)
  135. {
  136. resetImage();
  137.     for(int i = 0 ; i <image->height();i++){
  138.         for(int j = 0; j<image->width();j++){
  139.             tempPixel = image->pixel(j,i);
  140.  
  141.             y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
  142.             Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
  143.             Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
  144.  
  145.             y = y + h;
  146.             Cb = Cb - 128;
  147.             Cr = Cr - 128;
  148.  
  149.             tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
  150.             tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
  151.             tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
  152.  
  153.             if( tRed>255 ){tRed = 255;}
  154.             if( tRed<0 ){tRed = 0;}
  155.             if( tGreen>255 ){tGreen = 255;}
  156.             if( tGreen<0 ){tGreen = 0;}
  157.             if( tBlue>255 ){tBlue = 255;}
  158.             if( tBlue<0 ){tBlue = 0;}
  159.  
  160.  
  161.             image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  162.         }
  163.     }
  164.     updateImageDisplay();
  165. }
  166.  
  167. void ImageViewer::applyExampleAlgorithm()
  168. {
  169.     if(image!=NULL)
  170.     {
  171.         resetImage();
  172.         for(int i=0;i<std::min(image->width(),image->height());i++)
  173.         {
  174.             // macht die Farbe schwarz, bitte recherchieren wie eine andere Farbe gesetzt wird ...
  175.             for(int j=0;j<widthX;j++){
  176.                 value = qRgb(255, 0, 0);
  177.                 if(i+j<image->width()){
  178.                     image->setPixel(i+j, i, value);
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     updateImageDisplay();
  184.     logFile << "example algorithm applied " << std::endl;
  185.     renewLogging();
  186. }
  187.  
  188. void ImageViewer::doSmth()
  189. {
  190.     logFile << "done something " << std::endl;
  191.     renewLogging();
  192. }
  193.  
  194. void ImageViewer::resetImage()
  195. {
  196.     if(image!=NULL)
  197.     {
  198.     delete image;
  199.     image=NULL;
  200.     }
  201.  
  202.     image = new QImage(imageOld);
  203.  
  204.     updateImageDisplay();
  205. }
  206.  
  207. void ImageViewer::inkWidthX(int i)
  208. {
  209.     widthX = i;
  210. }
  211.  
  212. void ImageViewer::AktualesiereKontrast(int i)
  213. {
  214.     tempstr = "kontrast = " + QString::number(i);
  215.     kontrastSlider->setText(tempstr);
  216. }
  217.  
  218. void ImageViewer::AktualesiereHell(int i)
  219. {
  220.     tempstr = "helligkeit =  " + QString::number(i);
  221.     helligkeitSlider->setText(tempstr);
  222. }
  223.  
  224. void ImageViewer::autoKontrast()
  225. {
  226.     mult = (double)spinbox2->value()/100;
  227.     aLow = 255 * mult;
  228.     aHigh = 255 - aLow;
  229.     resetImage();
  230.         helligkeitAnpassen(aLow*(-1));
  231.  
  232.  
  233.         for(int i = 0 ; i <image->height();i++){
  234.             for(int j = 0; j<image->width();j++){
  235.                 tempPixel = image->pixel(j,i);
  236.  
  237.                 y = qRed(tempPixel)*0.299+qGreen(tempPixel)*0.587+qBlue(tempPixel)*0.114;
  238.                 Cb = qRed(tempPixel)*(-0.169)+qGreen(tempPixel)*(-0.331)+qBlue(tempPixel)*0.5+128;
  239.                 Cr = qRed(tempPixel)*0.5+qGreen(tempPixel)*(-0.419)+qBlue(tempPixel)*(-0.081)+128;
  240.  
  241.                 y = y * (1+mult);
  242.                 Cb = Cb - 128;
  243.                 Cr = Cr - 128;
  244.  
  245.                 tRed = (y + Cb * (-0.000926745) + Cr * 1.40169);
  246.                 tGreen = (y + Cb * (-0.343695) + Cr * (-0.714169));
  247.                 tBlue = (y + Cb * 1.77216 + Cr * 0.000990221);
  248.  
  249.                 if( tRed>255 ){tRed = 255;}
  250.                 if( tRed<0 ){tRed = 0;}
  251.                 if( tGreen>255 ){tGreen = 255;}
  252.                 if( tGreen<0 ){tGreen = 0;}
  253.                 if( tBlue>255 ){tBlue = 255;}
  254.                 if( tBlue<0 ){tBlue = 0;}
  255.  
  256.  
  257.                 image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  258.             }
  259.         }
  260.         updateImageDisplay();
  261. }
  262.  
  263. void ImageViewer::restoreImage()
  264. {
  265.     loadFile(origImage);
  266. }
  267.  
  268. void ImageViewer::updateColomn(int i)
  269. {
  270.     table->setColumnCount(i);
  271.     for(int i = 0 ; i < table->width();i++){
  272.         for(int j = 0;j < table->height();j++){
  273.             table->setItem(i,j,new QTableWidgetItem("1"));
  274.         }
  275.     }
  276. }
  277.  
  278. void ImageViewer::updateRow(int i)
  279. {
  280.     table->setRowCount(i);
  281.     for(int i = 0 ; i < table->width();i++){
  282.         for(int j = 0;j < table->height();j++){
  283.             table->setItem(i,j,new QTableWidgetItem("1"));
  284.         }
  285.     }
  286. }
  287. void ImageViewer::gausFilterAnwenden()
  288. {
  289.     ho.clear();
  290.     sumFaktor = 0;
  291.     sigma = spinbox5->value();
  292.     center = (int)(3.0 * sigma);
  293.     sigma2 = sigma * sigma ;
  294.     for(int i = 0; i<(2*center+1);i++){
  295.         double r = center - i;
  296.         ho.push_back(exp(-0.5 * (r*r) / sigma2));
  297.         sumFaktor = sumFaktor + ho[i];
  298.     }
  299.     hSize = ho.size();
  300.  
  301.     filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
  302.     PfilteredImage = &filteredImage;
  303.  
  304.     // wende filter an
  305.  
  306.     for(int x = 0; x<image->width();x++){
  307.         for(int y = 0;y<image->height();y++){
  308.             tRed = 0;
  309.             tGreen = 0;
  310.             tBlue = 0;
  311.  
  312.             for(int w = (-1)*(int)(hSize/2) ;w<=(int)(hSize/2);w++){
  313.  
  314.                     tempMaxY = y+w;
  315.  
  316.                     faktor = ho[w+hSize/2];
  317.  
  318.                     if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
  319.                     if(tempMaxY<0){tempMaxY=0;}
  320.  
  321.                     tRed = tRed + (int)(qRed(PfilteredImage->pixel(x,tempMaxY)) * faktor);
  322.  
  323.                     tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(x,tempMaxY)) * faktor);
  324.  
  325.                     tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(x,tempMaxY)) * faktor);
  326.             }
  327.             tRed = tRed /sumFaktor;
  328.             tGreen = tGreen /sumFaktor;
  329.             tBlue = tBlue /sumFaktor;
  330.  
  331.             if( tRed>255 ){tRed = 255;}
  332.             if( tRed<0 ){tRed = 0;}
  333.             if( tGreen>255 ){tGreen = 255;}
  334.             if( tGreen<0 ){tGreen = 0;}
  335.             if( tBlue>255 ){tBlue = 255;}
  336.             if( tBlue<0 ){tBlue = 0;}
  337.  
  338.             PfilteredImage->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  339.         }
  340.     }
  341.  
  342.     for(int x = 0; x<image->width();x++){
  343.         for(int y = 0;y<image->height();y++){
  344.             tRed = 0;
  345.             tGreen = 0;
  346.             tBlue = 0;
  347.  
  348.             for(int w = (-1)*(int)(hSize/2) ;w<=(int)(hSize/2);w++){
  349.  
  350.                     tempMaxX = x+w;
  351.  
  352.                     faktor = ho[w+hSize/2];
  353.  
  354.                     if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
  355.                     if(tempMaxX<0){tempMaxX=0;}
  356.  
  357.                     tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,y)) * faktor);
  358.  
  359.                     tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,y)) * faktor);
  360.  
  361.                     tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,y)) * faktor);
  362.             }
  363.             tRed = tRed /sumFaktor;
  364.             tGreen = tGreen /sumFaktor;
  365.             tBlue = tBlue /sumFaktor;
  366.  
  367.             if( tRed>255 ){tRed = 255;}
  368.             if( tRed<0 ){tRed = 0;}
  369.             if( tGreen>255 ){tGreen = 255;}
  370.             if( tGreen<0 ){tGreen = 0;}
  371.             if( tBlue>255 ){tBlue = 255;}
  372.             if( tBlue<0 ){tBlue = 0;}
  373.  
  374.             image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  375.         }
  376.     }
  377.     updateImageDisplay();
  378. }
  379.  
  380. void ImageViewer::kantDetSobel(){
  381.  
  382. spinbox3 -> setValue(3);
  383. spinbox4 -> setValue(3);
  384.  
  385.     table->setItem(0,0,new QTableWidgetItem("-1"));
  386.     table->setItem(0,1,new QTableWidgetItem("0"));
  387.     table->setItem(0,2,new QTableWidgetItem("1"));
  388.     table->setItem(1,0,new QTableWidgetItem("-1"));
  389.     table->setItem(1,1,new QTableWidgetItem("0"));
  390.     table->setItem(1,2,new QTableWidgetItem("1"));
  391.     table->setItem(2,0,new QTableWidgetItem("-1"));
  392.     table->setItem(2,1,new QTableWidgetItem("0"));
  393.     table->setItem(2,2,new QTableWidgetItem("1"));
  394.  
  395.     filterAnwenden();
  396.     sobelXImage = image->copy(QRect(0,0,image->width(),image->height()));
  397.  
  398.     resetImage();
  399.  
  400.     table->setItem(0,0,new QTableWidgetItem("-1"));
  401.     table->setItem(0,1,new QTableWidgetItem("-1"));
  402.     table->setItem(0,2,new QTableWidgetItem("-1"));
  403.     table->setItem(1,0,new QTableWidgetItem("0"));
  404.     table->setItem(1,1,new QTableWidgetItem("0"));
  405.     table->setItem(1,2,new QTableWidgetItem("0"));
  406.     table->setItem(2,0,new QTableWidgetItem("1"));
  407.     table->setItem(2,1,new QTableWidgetItem("1"));
  408.     table->setItem(2,2,new QTableWidgetItem("1"));
  409.  
  410.     filterAnwenden();
  411.     sobelYImage = image->copy(QRect(0,0,image->width(),image->height()));
  412.  
  413.     resetImage();
  414.  
  415.     for(int i = 0; i < image->height();i++){
  416.         for(int j = 0; j < image->width();j++){
  417.             tempXSobelPixel = sobelXImage.pixel(j,i);
  418.             tempYSobelPixel = sobelYImage.pixel(j,i);
  419.             tRed = ((int)(qRed(tempXSobelPixel))+(int)(qRed(tempYSobelPixel)));
  420.             tGreen = ((int)(qGreen(tempXSobelPixel))+(int)(qGreen(tempYSobelPixel)));
  421.             tBlue = ((int)(qBlue(tempXSobelPixel))+(int)(qBlue(tempYSobelPixel)));
  422.             image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  423.         }
  424.     }
  425.  
  426.     updateImageDisplay();
  427.  
  428.     /* 1). wende filterAnwenden() mit sobel matrix in x richtung an
  429.      * 2). speichere entstandenes bild in copyA
  430.      * 3). wende filterAnwenden() mit sobel matrix in y richtung an
  431.      * 4). speichere entstandenes bild in copyB
  432.      * 5). summiere die pixel von copyA und copyB zu neuem Image
  433.      */
  434.  
  435. }
  436.  
  437. void ImageViewer::filterAnwenden()
  438. {
  439.  
  440.     filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
  441.     PfilteredImage = &filteredImage;
  442.  
  443.     // wende filter an
  444.     for(int x = 0; x<image->width();x++){
  445.         for(int y = 0;y<image->height();y++){
  446.             tRed = 0;
  447.             tGreen = 0;
  448.             tBlue = 0;
  449.  
  450.             for(int w = (-1)*(int)(spinbox3->value()/2) ;w<=(int)(spinbox3->value()/2);w++){
  451.                 for(int h = (-1)*(int)(spinbox4->value()/2) ;h<=(int)(spinbox4->value()/2);h++){
  452.  
  453.                     tempMaxX = x+w;
  454.                     tempMaxY = y+h;
  455.  
  456.                     pTableItem = (table->item(h+(int)(spinbox4->value()/2),w+(int)(spinbox3->value()/2)));
  457.  
  458.                     if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
  459.                     if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
  460.  
  461.                     if(tempMaxX<0){tempMaxX=0;}
  462.                     if(tempMaxY<0){tempMaxY=0;}
  463.  
  464.  
  465.                     tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  466.                     tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  467.                     tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  468.                 }
  469.             }
  470.             tRed = tRed /(spinbox3->value()*spinbox4->value());
  471.             tGreen = tGreen /(spinbox3->value()*spinbox4->value());
  472.             tBlue = tBlue /(spinbox3->value()*spinbox4->value());
  473.  
  474.             if( tRed>255 ){tRed = 255;}
  475.             if( tRed<0 ){tRed = 0;}
  476.             if( tGreen>255 ){tGreen = 255;}
  477.             if( tGreen<0 ){tGreen = 0;}
  478.             if( tBlue>255 ){tBlue = 255;}
  479.             if( tBlue<0 ){tBlue = 0;}
  480.  
  481.             image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  482.         }
  483.     }
  484.  
  485.    // image = PfilteredImage;
  486.     updateImageDisplay();
  487.  
  488. }
  489.  
  490.  
  491. /****************************************************************************************
  492. *
  493. *  mit dieser Methode können sie sich pro Aufgabe ein  Tab anlegen, in der die Ein-
  494. *  stellungen per Slider, Button, Checkbox etc. gemacht werden und die zu implemen-
  495. *  tierenden Algorithmen gestatet werden.
  496. *
  497. *****************************************************************************************/
  498.  
  499. void ImageViewer::generateControlPanels()
  500. {
  501.     // first tab-----------------------------------------------------
  502.  
  503.         m_option_panel1 = new QWidget();
  504.     m_option_layout1 = new QVBoxLayout();
  505.     m_option_panel1->setLayout(m_option_layout1);
  506.  
  507. //tabelle vorher mit 1-en füllen
  508.     button1 = new QPushButton();
  509.     button1->setText("Apply algorithm");
  510.  
  511.     button2 = new QPushButton();
  512.     button2->setText("do something else");
  513.  
  514.     button3 = new QPushButton();
  515.     button3->setText("reset Image");
  516.  
  517.     spinbox1 = new QSpinBox(tabWidget);
  518.     spinbox1 -> setRange(0,20);
  519.     spinbox1 -> setSingleStep(1);
  520.     spinbox1 -> setValue(0);
  521.  
  522.     QObject::connect(button1, SIGNAL (clicked()), this, SLOT (applyExampleAlgorithm()));
  523.     QObject::connect(button2, SIGNAL (clicked()), this, SLOT (doSmth()));
  524.     QObject::connect(button3, SIGNAL (clicked()), this, SLOT (resetImage()));
  525.     QObject::connect(spinbox1, SIGNAL (valueChanged(int)), this, SLOT (inkWidthX(int)));
  526.  
  527.  
  528.  
  529.     m_option_layout1->addWidget(new QLabel("string thikness."));
  530.     m_option_layout1->addWidget(spinbox1);
  531.     m_option_layout1->addWidget(button1);
  532.     m_option_layout1->addWidget(button2);
  533.     m_option_layout1->addWidget(button3);
  534.  
  535.     tabWidget->addTab(m_option_panel1,"first tab");
  536.  
  537.  
  538.     // second tab--------------------------------------------------------------------
  539.  
  540.         m_option_panel2 = new QWidget();
  541.     m_option_layout2 = new QVBoxLayout();
  542.     m_option_panel2->setLayout(m_option_layout2);
  543.  
  544.     mittlereHelligkeit = new QLabel("Mittlere helligkeit = ");
  545.     varianz = new QLabel("Varianz = ");
  546.  
  547.     button6 = new QPushButton();
  548.     button6->setText("ermittle Mittlere Hell. und Varianz");
  549.  
  550.     button4 = new QPushButton();
  551.     button4->setText("erstelle histogramm");
  552.  
  553.     slider1 = new QSlider();
  554.     slider1->setSingleStep(1);
  555.     slider1->setMaximum(25);
  556.     slider1->setMinimum(-25);
  557.     slider1->setOrientation(Qt::Horizontal);
  558.  
  559.  
  560.     helligkeitSlider = new QLabel("helligkeit = 1");
  561.  
  562.     slider2 = new QSlider();
  563.     slider2->setSingleStep(1);
  564.     slider2->setMaximum(25);
  565.     slider2->setMinimum(-25);
  566.     slider2->setOrientation(Qt::Horizontal);
  567.  
  568.     kontrastSlider = new QLabel("kontrast = 1");
  569.  
  570.     spinbox2 = new QSpinBox(tabWidget);
  571.     spinbox2 -> setRange(0,20);
  572.     spinbox2 -> setSingleStep(1);
  573.     spinbox2 -> setValue(0);
  574.  
  575.     button5 = new QPushButton();
  576.     button5->setText("automatische kontrastanpassung");
  577.  
  578.     button7 = new QPushButton();
  579.     button7->setText("kehre zu bild zurück");
  580.  
  581.     QObject::connect(button6, SIGNAL (clicked()), this, SLOT (getMittlereHelligkeit()));
  582.     QObject::connect(button5, SIGNAL (clicked()), this, SLOT (autoKontrast()));
  583.     QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereHell(int)));
  584.     QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereKontrast(int)));
  585.     QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (helligkeitAnpassen(int)));
  586.     QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (kontrastAnpassen(int)));
  587.     QObject::connect(button4, SIGNAL (clicked()), this, SLOT (erstelleHistogramm()));
  588.     QObject::connect(button7, SIGNAL (clicked()), this, SLOT (restoreImage()));
  589.  
  590.     m_option_layout2->addWidget(mittlereHelligkeit);
  591.     m_option_layout2->addWidget(varianz);
  592.     m_option_layout2->addWidget(button6);
  593.     m_option_layout2->addWidget(button4);
  594.     m_option_layout2->addWidget(helligkeitSlider);
  595.     m_option_layout2->addWidget(slider1);
  596.     m_option_layout2->addWidget(kontrastSlider);
  597.     m_option_layout2->addWidget(slider2);
  598.     m_option_layout2->addWidget(spinbox2);
  599.     m_option_layout2->addWidget(button5);
  600.     m_option_layout2->addWidget(button7);
  601.  
  602.  
  603.     tabWidget->addTab(m_option_panel2,"second tab");
  604.  
  605.     //third tab-------------------------------------------------------
  606.  
  607.     m_option_panel3 = new QWidget();
  608.     m_option_layout3 = new QVBoxLayout();
  609.     m_option_panel3->setLayout(m_option_layout3);
  610.  
  611.     filterSize = new QLabel("Filter Grösse X oben, Y Unten");
  612.  
  613.     spinbox3 = new QSpinBox(tabWidget);
  614.     spinbox3 -> setRange(1,15);
  615.     spinbox3 -> setSingleStep(2);
  616.     spinbox3 -> setValue(5);
  617.  
  618.     spinbox4 = new QSpinBox(tabWidget);
  619.     spinbox4 -> setRange(1,15);
  620.     spinbox4 -> setSingleStep(2);
  621.     spinbox4 -> setValue(5);
  622.  
  623.     button8 = new QPushButton();
  624.     button8->setText("Filter anwenden");
  625.  
  626.     table = new QTableWidget();
  627.     table->setColumnCount(spinbox3->value());
  628.     table->setRowCount(spinbox4->value());
  629.     for(int i = 0 ; i < table->width();i++){
  630.         for(int j = 0;j < table->height();j++){
  631.             table->setItem(i,j,new QTableWidgetItem("1"));
  632.         }
  633.     }
  634.  
  635.     gausFilter = new QLabel("2D-Gausfilter");
  636.  
  637.     spinbox5 = new QSpinBox(tabWidget);
  638.     spinbox5 -> setRange(0,15);
  639.     spinbox5 -> setSingleStep(2);
  640.     spinbox5 -> setValue(3);
  641.  
  642.     button9 = new QPushButton();
  643.     button9->setText("wende Gaus filter an");
  644.  
  645.     button10 = new QPushButton();
  646.     button10->setText("Reset Image");
  647.  
  648.     QObject::connect(button8, SIGNAL (clicked()), this, SLOT (filterAnwenden()));
  649.     QObject::connect(spinbox3, SIGNAL (valueChanged(int)), this, SLOT (updateColomn(int)));
  650.     QObject::connect(spinbox4, SIGNAL (valueChanged(int)), this, SLOT (updateRow(int)));
  651.     QObject::connect(button9, SIGNAL (clicked()), this, SLOT (gausFilterAnwenden()));
  652.     QObject::connect(button10, SIGNAL (clicked()), this, SLOT (resetImage()));
  653.  
  654.  
  655.     m_option_layout3->addWidget(filterSize);
  656.     m_option_layout3->addWidget(spinbox3);
  657.     m_option_layout3->addWidget(spinbox4);
  658.     m_option_layout3->addWidget(button8);
  659.     m_option_layout3->addWidget(table);
  660.     m_option_layout3->addWidget(gausFilter);
  661.     m_option_layout3->addWidget(spinbox5);
  662.     m_option_layout3->addWidget(button9);
  663.     m_option_layout3->addWidget(button10);
  664.  
  665.  
  666.     tabWidget->addTab(m_option_panel3,"third tab");
  667.  
  668.     //forth tab-------------------------------------------------------
  669.  
  670.     m_option_panel4 = new QWidget();
  671.     m_option_layout4 = new QVBoxLayout();
  672.     m_option_panel4->setLayout(m_option_layout4);
  673.  
  674.     button11 = new QPushButton();
  675.     button11->setText("Use Sobel Kantenoperation");
  676.  
  677.     QObject::connect(button11, SIGNAL (clicked()), this, SLOT (kantDetSobel()));
  678.  
  679.     m_option_layout4->addWidget(button11);
  680.  
  681.     tabWidget->addTab(m_option_panel4,"forth tab");
  682.  
  683.  
  684.  
  685.  
  686.     tabWidget->show();
  687.  
  688.  
  689.     // Hinweis: Es bietet sich an pro Aufgabe jeweils einen solchen Tab zu erstellen
  690.  
  691. }
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699. /****************************************************************************************
  700. *
  701. *   ab hier kommen technische Details, die nicht notwenig für das Verständnis und die
  702. *   Bearbeitung sind.
  703. *
  704. *
  705. *****************************************************************************************/
  706.  
  707.  
  708.  
  709. void ImageViewer::startLogging()
  710. {
  711.     //LogFile
  712.     logFile.open("log.txt", std::ios::out);
  713.     logFile << "Logging: \n" << std::endl;
  714. }
  715.  
  716. void ImageViewer::renewLogging()
  717. {
  718.     QFile file("log.txt"); // Create a file handle for the file named
  719.     QString line;
  720.     file.open(QIODevice::ReadOnly); // Open the file
  721.  
  722.     QTextStream stream( &file ); // Set the stream to read from myFile
  723.     logBrowser->clear();
  724.     while(!stream.atEnd()){
  725.  
  726.         line = stream.readLine(); // this reads a line (QString) from the file
  727.         logBrowser->append(line);
  728.     }
  729. }
  730.  
  731.  
  732. void ImageViewer::resizeEvent(QResizeEvent * event)
  733. {
  734.     QMainWindow::resizeEvent(event);
  735.     centralwidget->setMinimumWidth(width());
  736.         centralwidget->setMinimumHeight(height());
  737.     centralwidget->setMaximumWidth(width());
  738.         centralwidget->setMaximumHeight(height());
  739.     logBrowser->setMinimumWidth(width()-40);
  740.         logBrowser->setMaximumWidth(width()-40);
  741. }
  742.  
  743. void ImageViewer::updateImageDisplay()
  744. {
  745.     imageLabel->setPixmap(QPixmap::fromImage(*image));
  746. }
  747.  
  748.  
  749. void ImageViewer::generateMainGui()
  750. {
  751.     /* Tab widget */
  752.         tabWidget = new QTabWidget(this);
  753.     tabWidget->setObjectName(QStringLiteral("tabWidget"));
  754.  
  755.  
  756.  
  757.     /* Center widget */
  758.     centralwidget = new QWidget(this);
  759.     centralwidget->setObjectName(QStringLiteral("centralwidget"));
  760.     centralwidget->setFixedSize(1000,300);
  761.     //setCentralWidget(centralwidget);
  762.  
  763.         imageLabel = new QLabel;
  764.     imageLabel->setBackgroundRole(QPalette::Base);
  765.         imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  766.         imageLabel->setScaledContents(true);
  767.  
  768.  
  769.     /* Center widget */
  770.     scrollArea = new QScrollArea;
  771.     scrollArea->setBackgroundRole(QPalette::Dark);
  772.         scrollArea->setWidget(imageLabel);
  773.  
  774.  
  775.         setCentralWidget(scrollArea);
  776.  
  777.     /* HBox layout */
  778.     QGridLayout* gLayout = new QGridLayout(centralwidget);
  779.     gLayout->setObjectName(QStringLiteral("hboxLayout"));
  780.     gLayout->addWidget(new QLabel(),1,1);
  781.     gLayout->setVerticalSpacing(50);
  782.     gLayout->addWidget(tabWidget,2,1);
  783.     gLayout->addWidget(scrollArea,2,2);
  784.  
  785.     logBrowser= new QTextEdit(this);
  786.     logBrowser->setMinimumHeight(100);
  787.     logBrowser->setMaximumHeight(200);
  788.     logBrowser->setMinimumWidth(width());
  789.     logBrowser->setMaximumWidth(width());
  790.     gLayout->addWidget(logBrowser,3,1,1,2);
  791.     gLayout->setVerticalSpacing(50);
  792. }
  793.  
  794.  
  795. bool ImageViewer::loadFile(const QString &fileName)
  796. {
  797.     if(image!=NULL)
  798.     {
  799.     delete image;
  800.     image=NULL;
  801.     }
  802.  
  803.     image = new QImage(fileName);
  804.     imageOld = fileName;
  805.  
  806.  
  807.     if (image->isNull()) {
  808.         QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
  809.                                  tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
  810.         setWindowFilePath(QString());
  811.         imageLabel->setPixmap(QPixmap());
  812.         imageLabel->adjustSize();
  813.         return false;
  814.     }
  815.  
  816.     scaleFactor = 1.0;
  817.  
  818.  
  819.     updateImageDisplay();
  820.  
  821.     printAct->setEnabled(true);
  822.     fitToWindowAct->setEnabled(true);
  823.     updateActions();
  824.  
  825.     if (!fitToWindowAct->isChecked())
  826.         imageLabel->adjustSize();
  827.  
  828.     setWindowFilePath(fileName);
  829.     logFile << "geladen: " << fileName.toStdString().c_str()  << std::endl;
  830.     renewLogging();
  831.     return true;
  832. }
  833.  
  834. bool ImageViewer::loadFileExt(const QString &fileName)
  835. {
  836.     origImage = imageOld;
  837.     loadFile(fileName);
  838.     return true;
  839. }
  840.  
  841.  
  842. void ImageViewer::open()
  843. {
  844.     QStringList mimeTypeFilters;
  845.     foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
  846.         mimeTypeFilters.append(mimeTypeName);
  847.     mimeTypeFilters.sort();
  848.     const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
  849.     QFileDialog dialog(this, tr("Open File"),
  850.                        picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.first());
  851.     dialog.setAcceptMode(QFileDialog::AcceptOpen);
  852.     dialog.setMimeTypeFilters(mimeTypeFilters);
  853.     dialog.selectMimeTypeFilter("image/jpeg");
  854.  
  855.     while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
  856. }
  857.  
  858. void ImageViewer::print()
  859. {
  860.     Q_ASSERT(imageLabel->pixmap());
  861. #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
  862.     QPrintDialog dialog(&printer, this);
  863.     if (dialog.exec()) {
  864.         QPainter painter(&printer);
  865.         QRect rect = painter.viewport();
  866.         QSize size = imageLabel->pixmap()->size();
  867.         size.scale(rect.size(), Qt::KeepAspectRatio);
  868.         painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
  869.         painter.setWindow(imageLabel->pixmap()->rect());
  870.         painter.drawPixmap(0, 0, *imageLabel->pixmap());
  871.     }
  872. #endif
  873. }
  874.  
  875. void ImageViewer::zoomIn()
  876. {
  877.     scaleImage(1.25);
  878. }
  879.  
  880. void ImageViewer::zoomOut()
  881. {
  882.     scaleImage(0.8);
  883. }
  884.  
  885. void ImageViewer::normalSize()
  886. {
  887.     imageLabel->adjustSize();
  888.     scaleFactor = 1.0;
  889. }
  890.  
  891. void ImageViewer::fitToWindow()
  892. {
  893.     bool fitToWindow = fitToWindowAct->isChecked();
  894.     scrollArea->setWidgetResizable(fitToWindow);
  895.     if (!fitToWindow) {
  896.         normalSize();
  897.     }
  898.     updateActions();
  899. }
  900.  
  901. void ImageViewer::about()
  902. {
  903.     QMessageBox::about(this, tr("About Image Viewer"),
  904.             tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
  905.                "and QScrollArea to display an image. QLabel is typically used "
  906.                "for displaying a text, but it can also display an image. "
  907.                "QScrollArea provides a scrolling view around another widget. "
  908.                "If the child widget exceeds the size of the frame, QScrollArea "
  909.                "automatically provides scroll bars. </p><p>The example "
  910.                "demonstrates how QLabel's ability to scale its contents "
  911.                "(QLabel::scaledContents), and QScrollArea's ability to "
  912.                "automatically resize its contents "
  913.                "(QScrollArea::widgetResizable), can be used to implement "
  914.                "zooming and scaling features. </p><p>In addition the example "
  915.                "shows how to use QPainter to print an image.</p>"));
  916. }
  917.  
  918. void ImageViewer::createActions()
  919. {
  920.     openAct = new QAction(tr("&Open..."), this);
  921.     openAct->setShortcut(tr("Ctrl+O"));
  922.     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  923.  
  924.     printAct = new QAction(tr("&Print..."), this);
  925.     printAct->setShortcut(tr("Ctrl+P"));
  926.     printAct->setEnabled(false);
  927.     connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
  928.  
  929.     exitAct = new QAction(tr("E&xit"), this);
  930.     exitAct->setShortcut(tr("Ctrl+Q"));
  931.     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
  932.  
  933.     zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
  934.     zoomInAct->setShortcut(tr("Ctrl++"));
  935.     zoomInAct->setEnabled(false);
  936.     connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
  937.  
  938.     zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
  939.     zoomOutAct->setShortcut(tr("Ctrl+-"));
  940.     zoomOutAct->setEnabled(false);
  941.     connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
  942.  
  943.     normalSizeAct = new QAction(tr("&Normal Size"), this);
  944.     normalSizeAct->setShortcut(tr("Ctrl+S"));
  945.     normalSizeAct->setEnabled(false);
  946.     connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
  947.  
  948.     fitToWindowAct = new QAction(tr("&Fit to Window"), this);
  949.     fitToWindowAct->setEnabled(false);
  950.     fitToWindowAct->setCheckable(true);
  951.     fitToWindowAct->setShortcut(tr("Ctrl+F"));
  952.     connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
  953.  
  954.     aboutAct = new QAction(tr("&About"), this);
  955.     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  956.  
  957.     aboutQtAct = new QAction(tr("About &Qt"), this);
  958.     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  959. }
  960.  
  961. void ImageViewer::createMenus()
  962. {
  963.     fileMenu = new QMenu(tr("&File"), this);
  964.     fileMenu->addAction(openAct);
  965.     fileMenu->addAction(printAct);
  966.     fileMenu->addSeparator();
  967.     fileMenu->addAction(exitAct);
  968.  
  969.     viewMenu = new QMenu(tr("&View"), this);
  970.     viewMenu->addAction(zoomInAct);
  971.     viewMenu->addAction(zoomOutAct);
  972.     viewMenu->addAction(normalSizeAct);
  973.     viewMenu->addSeparator();
  974.     viewMenu->addAction(fitToWindowAct);
  975.  
  976.     helpMenu = new QMenu(tr("&Help"), this);
  977.     helpMenu->addAction(aboutAct);
  978.     helpMenu->addAction(aboutQtAct);
  979.  
  980.     menuBar()->addMenu(fileMenu);
  981.     menuBar()->addMenu(viewMenu);
  982.     menuBar()->addMenu(helpMenu);
  983. }
  984.  
  985. void ImageViewer::updateActions()
  986. {
  987.     zoomInAct->setEnabled(!fitToWindowAct->isChecked());
  988.     zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
  989.     normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
  990. }
  991.  
  992. void ImageViewer::scaleImage(double factor)
  993. {
  994.     Q_ASSERT(imageLabel->pixmap());
  995.     scaleFactor *= factor;
  996.     imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
  997.  
  998.     adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
  999.     adjustScrollBar(scrollArea->verticalScrollBar(), factor);
  1000.  
  1001.     zoomInAct->setEnabled(scaleFactor < 10.0);
  1002.     zoomOutAct->setEnabled(scaleFactor > 0.05);
  1003. }
  1004.  
  1005. void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
  1006. {
  1007.     scrollBar->setValue(int(factor * scrollBar->value()
  1008.                             + ((factor - 1) * scrollBar->pageStep()/2)));
  1009. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top