Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.44 KB | None | 0 0
  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::kantDetSobel2(){
  381.  
  382. filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
  383. PfilteredImage = &filteredImage;
  384.  
  385. // kanten in Y achse detecten
  386.  
  387. for(int x = 0; x<image->width();x++){
  388. for(int y = 0;y<image->height();y++){
  389. tRed = 0;
  390. tGreen = 0;
  391. tBlue = 0;
  392.  
  393. for(int w = (-1)*(int)(spinbox6->value()/2) ;w<=(int)(spinbox6->value()/2);w++){
  394. for(int h = (-1)*(int)(spinbox7->value()/2) ;h<=(int)(spinbox7->value()/2);h++){
  395.  
  396. tempMaxX = x+w;
  397. tempMaxY = y+h;
  398.  
  399. if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
  400. if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
  401.  
  402. if(tempMaxX<0){tempMaxX=0;}
  403. if(tempMaxY<0){tempMaxY=0;}
  404.  
  405. tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,tempMaxY)) * h);
  406. tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,tempMaxY)) * h);
  407. tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,tempMaxY)) * h);
  408. }
  409. }
  410.  
  411. tRed = tRed /((spinbox6->value())*(spinbox7->value()-1));
  412. tGreen = tGreen /((spinbox6->value())*(spinbox7->value()-1));
  413. tBlue = tBlue /((spinbox6->value())*(spinbox7->value()-1));
  414.  
  415. if( tRed>255 ){tRed = 255;}
  416. if( tRed<0 ){tRed = 0;}
  417. if( tGreen>255 ){tGreen = 255;}
  418. if( tGreen<0 ){tGreen = 0;}
  419. if( tBlue>255 ){tBlue = 255;}
  420. if( tBlue<0 ){tBlue = 0;}
  421.  
  422. image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  423. }
  424. }
  425. sobelYImage = image->copy(QRect(0,0,image->width(),image->height()));
  426.  
  427. resetImage();
  428.  
  429. //kanten in X achse detecten
  430.  
  431. for(int x = 0; x<image->width();x++){
  432. for(int y = 0;y<image->height();y++){
  433. tRed = 0;
  434. tGreen = 0;
  435. tBlue = 0;
  436.  
  437. for(int w = (-1)*(int)(spinbox6->value()/2) ;w<=(int)(spinbox6->value()/2);w++){
  438. for(int h = (-1)*(int)(spinbox7->value()/2) ;h<=(int)(spinbox7->value()/2);h++){
  439.  
  440. tempMaxX = x+w;
  441. tempMaxY = y+h;
  442.  
  443. if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
  444. if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
  445.  
  446. if(tempMaxX<0){tempMaxX=0;}
  447. if(tempMaxY<0){tempMaxY=0;}
  448.  
  449. tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,tempMaxY)) * w);
  450. tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,tempMaxY)) * w);
  451. tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,tempMaxY)) * w);
  452. }
  453. }
  454.  
  455. tRed = tRed /((spinbox6->value())*(spinbox7->value()-1));
  456. tGreen = tGreen /((spinbox6->value())*(spinbox7->value()-1));
  457. tBlue = tBlue /((spinbox6->value())*(spinbox7->value()-1));
  458.  
  459. if( tRed>255 ){tRed = 255;}
  460. if( tRed<0 ){tRed = 0;}
  461. if( tGreen>255 ){tGreen = 255;}
  462. if( tGreen<0 ){tGreen = 0;}
  463. if( tBlue>255 ){tBlue = 255;}
  464. if( tBlue<0 ){tBlue = 0;}
  465.  
  466. image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  467. }
  468. }
  469. sobelXImage = image->copy(QRect(0,0,image->width(),image->height()));
  470.  
  471. resetImage();
  472.  
  473. // die X und Y detection verschmelzen
  474.  
  475. for(int i = 0; i < image->height();i++){
  476. for(int j = 0; j < image->width();j++){
  477. tempXSobelPixel = sobelXImage.pixel(j,i);
  478. tempYSobelPixel = sobelYImage.pixel(j,i);
  479. tRed = ((int)(qRed(tempXSobelPixel))+(int)(qRed(tempYSobelPixel)));
  480. tGreen = ((int)(qGreen(tempXSobelPixel))+(int)(qGreen(tempYSobelPixel)));
  481. tBlue = ((int)(qBlue(tempXSobelPixel))+(int)(qBlue(tempYSobelPixel)));
  482. image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  483. }
  484. }
  485. updateImageDisplay();
  486. }
  487.  
  488. void ImageViewer::kantDetSobel(){
  489.  
  490. spinbox3 -> setValue(3);
  491. spinbox4 -> setValue(3);
  492.  
  493. table->setItem(0,0,new QTableWidgetItem("-1"));
  494. table->setItem(0,1,new QTableWidgetItem("0"));
  495. table->setItem(0,2,new QTableWidgetItem("1"));
  496. table->setItem(1,0,new QTableWidgetItem("-1"));
  497. table->setItem(1,1,new QTableWidgetItem("0"));
  498. table->setItem(1,2,new QTableWidgetItem("1"));
  499. table->setItem(2,0,new QTableWidgetItem("-1"));
  500. table->setItem(2,1,new QTableWidgetItem("0"));
  501. table->setItem(2,2,new QTableWidgetItem("1"));
  502.  
  503. filterAnwenden();
  504. sobelXImage = image->copy(QRect(0,0,image->width(),image->height()));
  505.  
  506. resetImage();
  507.  
  508. table->setItem(0,0,new QTableWidgetItem("-1"));
  509. table->setItem(0,1,new QTableWidgetItem("-1"));
  510. table->setItem(0,2,new QTableWidgetItem("-1"));
  511. table->setItem(1,0,new QTableWidgetItem("0"));
  512. table->setItem(1,1,new QTableWidgetItem("0"));
  513. table->setItem(1,2,new QTableWidgetItem("0"));
  514. table->setItem(2,0,new QTableWidgetItem("1"));
  515. table->setItem(2,1,new QTableWidgetItem("1"));
  516. table->setItem(2,2,new QTableWidgetItem("1"));
  517.  
  518. filterAnwenden();
  519. sobelYImage = image->copy(QRect(0,0,image->width(),image->height()));
  520.  
  521. resetImage();
  522.  
  523. for(int i = 0; i < image->height();i++){
  524. for(int j = 0; j < image->width();j++){
  525. tempXSobelPixel = sobelXImage.pixel(j,i);
  526. tempYSobelPixel = sobelYImage.pixel(j,i);
  527. tRed = ((int)(qRed(tempXSobelPixel))+(int)(qRed(tempYSobelPixel)));
  528. tGreen = ((int)(qGreen(tempXSobelPixel))+(int)(qGreen(tempYSobelPixel)));
  529. tBlue = ((int)(qBlue(tempXSobelPixel))+(int)(qBlue(tempYSobelPixel)));
  530. image->setPixel(j,i,qRgb(tRed,tGreen,tBlue));
  531. }
  532. }
  533.  
  534. updateImageDisplay();
  535.  
  536. /* 1). wende filterAnwenden() mit sobel matrix in x richtung an
  537. * 2). speichere entstandenes bild in copyA
  538. * 3). wende filterAnwenden() mit sobel matrix in y richtung an
  539. * 4). speichere entstandenes bild in copyB
  540. * 5). summiere die pixel von copyA und copyB zu neuem Image
  541. */
  542.  
  543. }
  544.  
  545. void ImageViewer::filterAnwenden()
  546. {
  547.  
  548. filteredImage = image->copy(QRect(0,0,image->width(),image->height()));
  549. PfilteredImage = &filteredImage;
  550.  
  551. // wende filter an
  552. for(int x = 0; x<image->width();x++){
  553. for(int y = 0;y<image->height();y++){
  554. tRed = 0;
  555. tGreen = 0;
  556. tBlue = 0;
  557.  
  558. for(int w = (-1)*(int)(spinbox3->value()/2) ;w<=(int)(spinbox3->value()/2);w++){
  559. for(int h = (-1)*(int)(spinbox4->value()/2) ;h<=(int)(spinbox4->value()/2);h++){
  560.  
  561. tempMaxX = x+w;
  562. tempMaxY = y+h;
  563.  
  564. pTableItem = (table->item(h+(int)(spinbox4->value()/2),w+(int)(spinbox3->value()/2)));
  565.  
  566. if(tempMaxX>=image->width()){tempMaxX=image->width()-1;}
  567. if(tempMaxY>=image->height()){tempMaxY=image->height()-1;}
  568.  
  569. if(tempMaxX<0){tempMaxX=0;}
  570. if(tempMaxY<0){tempMaxY=0;}
  571.  
  572.  
  573. tRed = tRed + (int)(qRed(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  574. tGreen = tGreen + (int)(qGreen(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  575. tBlue = tBlue + (int)(qBlue(PfilteredImage->pixel(tempMaxX,tempMaxY)) * (*pTableItem).text().toDouble());
  576. }
  577. }
  578. tRed = tRed /(spinbox3->value()*spinbox4->value());
  579. tGreen = tGreen /(spinbox3->value()*spinbox4->value());
  580. tBlue = tBlue /(spinbox3->value()*spinbox4->value());
  581.  
  582. if( tRed>255 ){tRed = 255;}
  583. if( tRed<0 ){tRed = 0;}
  584. if( tGreen>255 ){tGreen = 255;}
  585. if( tGreen<0 ){tGreen = 0;}
  586. if( tBlue>255 ){tBlue = 255;}
  587. if( tBlue<0 ){tBlue = 0;}
  588.  
  589. image->setPixel(x,y,qRgb(tRed,tGreen,tBlue));
  590. }
  591. }
  592.  
  593. // image = PfilteredImage;
  594. updateImageDisplay();
  595.  
  596. }
  597.  
  598.  
  599. /****************************************************************************************
  600. *
  601. * mit dieser Methode können sie sich pro Aufgabe ein Tab anlegen, in der die Ein-
  602. * stellungen per Slider, Button, Checkbox etc. gemacht werden und die zu implemen-
  603. * tierenden Algorithmen gestatet werden.
  604. *
  605. *****************************************************************************************/
  606.  
  607. void ImageViewer::generateControlPanels()
  608. {
  609. // first tab-----------------------------------------------------
  610.  
  611. m_option_panel1 = new QWidget();
  612. m_option_layout1 = new QVBoxLayout();
  613. m_option_panel1->setLayout(m_option_layout1);
  614.  
  615. //tabelle vorher mit 1-en füllen
  616. button1 = new QPushButton();
  617. button1->setText("Apply algorithm");
  618.  
  619. button2 = new QPushButton();
  620. button2->setText("do something else");
  621.  
  622. button3 = new QPushButton();
  623. button3->setText("reset Image");
  624.  
  625. spinbox1 = new QSpinBox(tabWidget);
  626. spinbox1 -> setRange(0,20);
  627. spinbox1 -> setSingleStep(1);
  628. spinbox1 -> setValue(0);
  629.  
  630. QObject::connect(button1, SIGNAL (clicked()), this, SLOT (applyExampleAlgorithm()));
  631. QObject::connect(button2, SIGNAL (clicked()), this, SLOT (doSmth()));
  632. QObject::connect(button3, SIGNAL (clicked()), this, SLOT (resetImage()));
  633. QObject::connect(spinbox1, SIGNAL (valueChanged(int)), this, SLOT (inkWidthX(int)));
  634.  
  635.  
  636.  
  637. m_option_layout1->addWidget(new QLabel("string thikness."));
  638. m_option_layout1->addWidget(spinbox1);
  639. m_option_layout1->addWidget(button1);
  640. m_option_layout1->addWidget(button2);
  641. m_option_layout1->addWidget(button3);
  642.  
  643. tabWidget->addTab(m_option_panel1,"first tab");
  644.  
  645.  
  646. // second tab--------------------------------------------------------------------
  647.  
  648. m_option_panel2 = new QWidget();
  649. m_option_layout2 = new QVBoxLayout();
  650. m_option_panel2->setLayout(m_option_layout2);
  651.  
  652. mittlereHelligkeit = new QLabel("Mittlere helligkeit = ");
  653. varianz = new QLabel("Varianz = ");
  654.  
  655. button6 = new QPushButton();
  656. button6->setText("ermittle Mittlere Hell. und Varianz");
  657.  
  658. button4 = new QPushButton();
  659. button4->setText("erstelle histogramm");
  660.  
  661. slider1 = new QSlider();
  662. slider1->setSingleStep(1);
  663. slider1->setMaximum(25);
  664. slider1->setMinimum(-25);
  665. slider1->setOrientation(Qt::Horizontal);
  666.  
  667.  
  668. helligkeitSlider = new QLabel("helligkeit = 1");
  669.  
  670. slider2 = new QSlider();
  671. slider2->setSingleStep(1);
  672. slider2->setMaximum(25);
  673. slider2->setMinimum(-25);
  674. slider2->setOrientation(Qt::Horizontal);
  675.  
  676. kontrastSlider = new QLabel("kontrast = 1");
  677.  
  678. spinbox2 = new QSpinBox(tabWidget);
  679. spinbox2 -> setRange(0,20);
  680. spinbox2 -> setSingleStep(1);
  681. spinbox2 -> setValue(0);
  682.  
  683. button5 = new QPushButton();
  684. button5->setText("automatische kontrastanpassung");
  685.  
  686. button7 = new QPushButton();
  687. button7->setText("kehre zu bild zurück");
  688.  
  689. QObject::connect(button6, SIGNAL (clicked()), this, SLOT (getMittlereHelligkeit()));
  690. QObject::connect(button5, SIGNAL (clicked()), this, SLOT (autoKontrast()));
  691. QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereHell(int)));
  692. QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (AktualesiereKontrast(int)));
  693. QObject::connect(slider1, SIGNAL (sliderMoved(int)), this, SLOT (helligkeitAnpassen(int)));
  694. QObject::connect(slider2, SIGNAL (sliderMoved(int)), this, SLOT (kontrastAnpassen(int)));
  695. QObject::connect(button4, SIGNAL (clicked()), this, SLOT (erstelleHistogramm()));
  696. QObject::connect(button7, SIGNAL (clicked()), this, SLOT (restoreImage()));
  697.  
  698. m_option_layout2->addWidget(mittlereHelligkeit);
  699. m_option_layout2->addWidget(varianz);
  700. m_option_layout2->addWidget(button6);
  701. m_option_layout2->addWidget(button4);
  702. m_option_layout2->addWidget(helligkeitSlider);
  703. m_option_layout2->addWidget(slider1);
  704. m_option_layout2->addWidget(kontrastSlider);
  705. m_option_layout2->addWidget(slider2);
  706. m_option_layout2->addWidget(spinbox2);
  707. m_option_layout2->addWidget(button5);
  708. m_option_layout2->addWidget(button7);
  709.  
  710.  
  711. tabWidget->addTab(m_option_panel2,"second tab");
  712.  
  713. //third tab-------------------------------------------------------
  714.  
  715. m_option_panel3 = new QWidget();
  716. m_option_layout3 = new QVBoxLayout();
  717. m_option_panel3->setLayout(m_option_layout3);
  718.  
  719. filterSize = new QLabel("Filter Grösse X oben, Y Unten");
  720.  
  721. spinbox3 = new QSpinBox(tabWidget);
  722. spinbox3 -> setRange(1,15);
  723. spinbox3 -> setSingleStep(2);
  724. spinbox3 -> setValue(5);
  725.  
  726. spinbox4 = new QSpinBox(tabWidget);
  727. spinbox4 -> setRange(1,15);
  728. spinbox4 -> setSingleStep(2);
  729. spinbox4 -> setValue(5);
  730.  
  731. button8 = new QPushButton();
  732. button8->setText("Filter anwenden");
  733.  
  734. table = new QTableWidget();
  735. table->setColumnCount(spinbox3->value());
  736. table->setRowCount(spinbox4->value());
  737. for(int i = 0 ; i < table->width();i++){
  738. for(int j = 0;j < table->height();j++){
  739. table->setItem(i,j,new QTableWidgetItem("1"));
  740. }
  741. }
  742.  
  743. gausFilter = new QLabel("2D-Gausfilter");
  744.  
  745. spinbox5 = new QSpinBox(tabWidget);
  746. spinbox5 -> setRange(0,15);
  747. spinbox5 -> setSingleStep(2);
  748. spinbox5 -> setValue(3);
  749.  
  750. button9 = new QPushButton();
  751. button9->setText("wende Gaus filter an");
  752.  
  753. button10 = new QPushButton();
  754. button10->setText("Reset Image");
  755.  
  756. QObject::connect(button8, SIGNAL (clicked()), this, SLOT (filterAnwenden()));
  757. QObject::connect(spinbox3, SIGNAL (valueChanged(int)), this, SLOT (updateColomn(int)));
  758. QObject::connect(spinbox4, SIGNAL (valueChanged(int)), this, SLOT (updateRow(int)));
  759. QObject::connect(button9, SIGNAL (clicked()), this, SLOT (gausFilterAnwenden()));
  760. QObject::connect(button10, SIGNAL (clicked()), this, SLOT (resetImage()));
  761.  
  762.  
  763. m_option_layout3->addWidget(filterSize);
  764. m_option_layout3->addWidget(spinbox3);
  765. m_option_layout3->addWidget(spinbox4);
  766. m_option_layout3->addWidget(button8);
  767. m_option_layout3->addWidget(table);
  768. m_option_layout3->addWidget(gausFilter);
  769. m_option_layout3->addWidget(spinbox5);
  770. m_option_layout3->addWidget(button9);
  771. m_option_layout3->addWidget(button10);
  772.  
  773.  
  774. tabWidget->addTab(m_option_panel3,"third tab");
  775.  
  776. //forth tab-------------------------------------------------------
  777.  
  778. m_option_panel4 = new QWidget();
  779. m_option_layout4 = new QVBoxLayout();
  780. m_option_panel4->setLayout(m_option_layout4);
  781.  
  782. spinbox6 = new QSpinBox(tabWidget);
  783. spinbox6 -> setRange(1,15);
  784. spinbox6 -> setSingleStep(2);
  785. spinbox6 -> setValue(3);
  786.  
  787. spinbox7 = new QSpinBox(tabWidget);
  788. spinbox7 -> setRange(1,15);
  789. spinbox7 -> setSingleStep(2);
  790. spinbox7 -> setValue(3);
  791.  
  792. button11 = new QPushButton();
  793. button11->setText("Use Sobel Kantenoperation with spinbox");
  794.  
  795. button12 = new QPushButton();
  796. button12->setText("Use Sobel Kantenoperation static");
  797.  
  798. QObject::connect(button11, SIGNAL (clicked()), this, SLOT (kantDetSobel2()));
  799.  
  800. QObject::connect(button12, SIGNAL (clicked()), this, SLOT (kantDetSobel()));
  801.  
  802. m_option_layout4->addWidget(spinbox6);
  803. m_option_layout4->addWidget(spinbox7);
  804. m_option_layout4->addWidget(button11);
  805. m_option_layout4->addWidget(button12);
  806.  
  807. tabWidget->addTab(m_option_panel4,"forth tab");
  808.  
  809.  
  810.  
  811.  
  812. tabWidget->show();
  813.  
  814.  
  815. // Hinweis: Es bietet sich an pro Aufgabe jeweils einen solchen Tab zu erstellen
  816.  
  817. }
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825. /****************************************************************************************
  826. *
  827. * ab hier kommen technische Details, die nicht notwenig für das Verständnis und die
  828. * Bearbeitung sind.
  829. *
  830. *
  831. *****************************************************************************************/
  832.  
  833.  
  834.  
  835. void ImageViewer::startLogging()
  836. {
  837. //LogFile
  838. logFile.open("log.txt", std::ios::out);
  839. logFile << "Logging: \n" << std::endl;
  840. }
  841.  
  842. void ImageViewer::renewLogging()
  843. {
  844. QFile file("log.txt"); // Create a file handle for the file named
  845. QString line;
  846. file.open(QIODevice::ReadOnly); // Open the file
  847.  
  848. QTextStream stream( &file ); // Set the stream to read from myFile
  849. logBrowser->clear();
  850. while(!stream.atEnd()){
  851.  
  852. line = stream.readLine(); // this reads a line (QString) from the file
  853. logBrowser->append(line);
  854. }
  855. }
  856.  
  857.  
  858. void ImageViewer::resizeEvent(QResizeEvent * event)
  859. {
  860. QMainWindow::resizeEvent(event);
  861. centralwidget->setMinimumWidth(width());
  862. centralwidget->setMinimumHeight(height());
  863. centralwidget->setMaximumWidth(width());
  864. centralwidget->setMaximumHeight(height());
  865. logBrowser->setMinimumWidth(width()-40);
  866. logBrowser->setMaximumWidth(width()-40);
  867. }
  868.  
  869. void ImageViewer::updateImageDisplay()
  870. {
  871. imageLabel->setPixmap(QPixmap::fromImage(*image));
  872. }
  873.  
  874.  
  875. void ImageViewer::generateMainGui()
  876. {
  877. /* Tab widget */
  878. tabWidget = new QTabWidget(this);
  879. tabWidget->setObjectName(QStringLiteral("tabWidget"));
  880.  
  881.  
  882.  
  883. /* Center widget */
  884. centralwidget = new QWidget(this);
  885. centralwidget->setObjectName(QStringLiteral("centralwidget"));
  886. centralwidget->setFixedSize(1000,300);
  887. //setCentralWidget(centralwidget);
  888.  
  889. imageLabel = new QLabel;
  890. imageLabel->setBackgroundRole(QPalette::Base);
  891. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  892. imageLabel->setScaledContents(true);
  893.  
  894.  
  895. /* Center widget */
  896. scrollArea = new QScrollArea;
  897. scrollArea->setBackgroundRole(QPalette::Dark);
  898. scrollArea->setWidget(imageLabel);
  899.  
  900.  
  901. setCentralWidget(scrollArea);
  902.  
  903. /* HBox layout */
  904. QGridLayout* gLayout = new QGridLayout(centralwidget);
  905. gLayout->setObjectName(QStringLiteral("hboxLayout"));
  906. gLayout->addWidget(new QLabel(),1,1);
  907. gLayout->setVerticalSpacing(50);
  908. gLayout->addWidget(tabWidget,2,1);
  909. gLayout->addWidget(scrollArea,2,2);
  910.  
  911. logBrowser= new QTextEdit(this);
  912. logBrowser->setMinimumHeight(100);
  913. logBrowser->setMaximumHeight(200);
  914. logBrowser->setMinimumWidth(width());
  915. logBrowser->setMaximumWidth(width());
  916. gLayout->addWidget(logBrowser,3,1,1,2);
  917. gLayout->setVerticalSpacing(50);
  918. }
  919.  
  920.  
  921. bool ImageViewer::loadFile(const QString &fileName)
  922. {
  923. if(image!=NULL)
  924. {
  925. delete image;
  926. image=NULL;
  927. }
  928.  
  929. image = new QImage(fileName);
  930. imageOld = fileName;
  931.  
  932.  
  933. if (image->isNull()) {
  934. QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
  935. tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
  936. setWindowFilePath(QString());
  937. imageLabel->setPixmap(QPixmap());
  938. imageLabel->adjustSize();
  939. return false;
  940. }
  941.  
  942. scaleFactor = 1.0;
  943.  
  944.  
  945. updateImageDisplay();
  946.  
  947. printAct->setEnabled(true);
  948. fitToWindowAct->setEnabled(true);
  949. updateActions();
  950.  
  951. if (!fitToWindowAct->isChecked())
  952. imageLabel->adjustSize();
  953.  
  954. setWindowFilePath(fileName);
  955. logFile << "geladen: " << fileName.toStdString().c_str() << std::endl;
  956. renewLogging();
  957. return true;
  958. }
  959.  
  960. bool ImageViewer::loadFileExt(const QString &fileName)
  961. {
  962. origImage = imageOld;
  963. loadFile(fileName);
  964. return true;
  965. }
  966.  
  967.  
  968. void ImageViewer::open()
  969. {
  970. QStringList mimeTypeFilters;
  971. foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
  972. mimeTypeFilters.append(mimeTypeName);
  973. mimeTypeFilters.sort();
  974. const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
  975. QFileDialog dialog(this, tr("Open File"),
  976. picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.first());
  977. dialog.setAcceptMode(QFileDialog::AcceptOpen);
  978. dialog.setMimeTypeFilters(mimeTypeFilters);
  979. dialog.selectMimeTypeFilter("image/jpeg");
  980.  
  981. while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
  982. }
  983.  
  984. void ImageViewer::print()
  985. {
  986. Q_ASSERT(imageLabel->pixmap());
  987. #if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
  988. QPrintDialog dialog(&printer, this);
  989. if (dialog.exec()) {
  990. QPainter painter(&printer);
  991. QRect rect = painter.viewport();
  992. QSize size = imageLabel->pixmap()->size();
  993. size.scale(rect.size(), Qt::KeepAspectRatio);
  994. painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
  995. painter.setWindow(imageLabel->pixmap()->rect());
  996. painter.drawPixmap(0, 0, *imageLabel->pixmap());
  997. }
  998. #endif
  999. }
  1000.  
  1001. void ImageViewer::zoomIn()
  1002. {
  1003. scaleImage(1.25);
  1004. }
  1005.  
  1006. void ImageViewer::zoomOut()
  1007. {
  1008. scaleImage(0.8);
  1009. }
  1010.  
  1011. void ImageViewer::normalSize()
  1012. {
  1013. imageLabel->adjustSize();
  1014. scaleFactor = 1.0;
  1015. }
  1016.  
  1017. void ImageViewer::fitToWindow()
  1018. {
  1019. bool fitToWindow = fitToWindowAct->isChecked();
  1020. scrollArea->setWidgetResizable(fitToWindow);
  1021. if (!fitToWindow) {
  1022. normalSize();
  1023. }
  1024. updateActions();
  1025. }
  1026.  
  1027. void ImageViewer::about()
  1028. {
  1029. QMessageBox::about(this, tr("About Image Viewer"),
  1030. tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
  1031. "and QScrollArea to display an image. QLabel is typically used "
  1032. "for displaying a text, but it can also display an image. "
  1033. "QScrollArea provides a scrolling view around another widget. "
  1034. "If the child widget exceeds the size of the frame, QScrollArea "
  1035. "automatically provides scroll bars. </p><p>The example "
  1036. "demonstrates how QLabel's ability to scale its contents "
  1037. "(QLabel::scaledContents), and QScrollArea's ability to "
  1038. "automatically resize its contents "
  1039. "(QScrollArea::widgetResizable), can be used to implement "
  1040. "zooming and scaling features. </p><p>In addition the example "
  1041. "shows how to use QPainter to print an image.</p>"));
  1042. }
  1043.  
  1044. void ImageViewer::createActions()
  1045. {
  1046. openAct = new QAction(tr("&Open..."), this);
  1047. openAct->setShortcut(tr("Ctrl+O"));
  1048. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  1049.  
  1050. printAct = new QAction(tr("&Print..."), this);
  1051. printAct->setShortcut(tr("Ctrl+P"));
  1052. printAct->setEnabled(false);
  1053. connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
  1054.  
  1055. exitAct = new QAction(tr("E&xit"), this);
  1056. exitAct->setShortcut(tr("Ctrl+Q"));
  1057. connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
  1058.  
  1059. zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
  1060. zoomInAct->setShortcut(tr("Ctrl++"));
  1061. zoomInAct->setEnabled(false);
  1062. connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
  1063.  
  1064. zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
  1065. zoomOutAct->setShortcut(tr("Ctrl+-"));
  1066. zoomOutAct->setEnabled(false);
  1067. connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
  1068.  
  1069. normalSizeAct = new QAction(tr("&Normal Size"), this);
  1070. normalSizeAct->setShortcut(tr("Ctrl+S"));
  1071. normalSizeAct->setEnabled(false);
  1072. connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
  1073.  
  1074. fitToWindowAct = new QAction(tr("&Fit to Window"), this);
  1075. fitToWindowAct->setEnabled(false);
  1076. fitToWindowAct->setCheckable(true);
  1077. fitToWindowAct->setShortcut(tr("Ctrl+F"));
  1078. connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
  1079.  
  1080. aboutAct = new QAction(tr("&About"), this);
  1081. connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  1082.  
  1083. aboutQtAct = new QAction(tr("About &Qt"), this);
  1084. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  1085. }
  1086.  
  1087. void ImageViewer::createMenus()
  1088. {
  1089. fileMenu = new QMenu(tr("&File"), this);
  1090. fileMenu->addAction(openAct);
  1091. fileMenu->addAction(printAct);
  1092. fileMenu->addSeparator();
  1093. fileMenu->addAction(exitAct);
  1094.  
  1095. viewMenu = new QMenu(tr("&View"), this);
  1096. viewMenu->addAction(zoomInAct);
  1097. viewMenu->addAction(zoomOutAct);
  1098. viewMenu->addAction(normalSizeAct);
  1099. viewMenu->addSeparator();
  1100. viewMenu->addAction(fitToWindowAct);
  1101.  
  1102. helpMenu = new QMenu(tr("&Help"), this);
  1103. helpMenu->addAction(aboutAct);
  1104. helpMenu->addAction(aboutQtAct);
  1105.  
  1106. menuBar()->addMenu(fileMenu);
  1107. menuBar()->addMenu(viewMenu);
  1108. menuBar()->addMenu(helpMenu);
  1109. }
  1110.  
  1111. void ImageViewer::updateActions()
  1112. {
  1113. zoomInAct->setEnabled(!fitToWindowAct->isChecked());
  1114. zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
  1115. normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
  1116. }
  1117.  
  1118. void ImageViewer::scaleImage(double factor)
  1119. {
  1120. Q_ASSERT(imageLabel->pixmap());
  1121. scaleFactor *= factor;
  1122. imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
  1123.  
  1124. adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
  1125. adjustScrollBar(scrollArea->verticalScrollBar(), factor);
  1126.  
  1127. zoomInAct->setEnabled(scaleFactor < 10.0);
  1128. zoomOutAct->setEnabled(scaleFactor > 0.05);
  1129. }
  1130.  
  1131. void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
  1132. {
  1133. scrollBar->setValue(int(factor * scrollBar->value()
  1134. + ((factor - 1) * scrollBar->pageStep()/2)));
  1135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement