暫無描述

MergePanel.cpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "MergePanel.h"
  2. #include "ui_MergePanel.h"
  3. #include <QDebug>
  4. #include "ClickableLabel.h"
  5. /// \fn MainWindow::MainWindow(QWidget *parent)
  6. /// \~English
  7. /// \brief Default Constructor for the MainWindow.
  8. /// \~Spanish
  9. /// \brief Constructor por defecto del MainWindow.
  10. MainWindow::MainWindow(QWidget *parent) :
  11. QMainWindow(parent),
  12. ui(new Ui::MainWindow)
  13. {
  14. ui->setupUi(this);
  15. // Signal connections
  16. // Conexion de senales
  17. // Closes window with cmd + w
  18. // Cierra la ventana con cmd + w
  19. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(close()));
  20. // Opens a image with cmd + o
  21. // Abre una imagen con cmd + o
  22. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this, SLOT(on_btnSelectImage_clicked()));
  23. // Opens background image with cmd + n
  24. // Abre imagen de transfondo con cmd + n
  25. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_N), this, SLOT(on_btnSelectBackground_clicked()));
  26. // Saves an image with cmd + s
  27. // Guarda una imagen con cmd + s
  28. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this, SLOT(on_btnSaveImage_clicked()));
  29. // Merges two images with cmd + m
  30. // Fuciona dos imagenes con cmd + m
  31. new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_M), this, SLOT(on_btnMergeImages_clicked()));
  32. // Makes the label clickable.
  33. // Hace el labelWidget cliqueable.
  34. connect(ui->lblOriginalImage, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
  35. connect(ui->lblBackgroundImage, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_PressedBackground())) ;
  36. }
  37. /// \fn MainWindow::~MainWindow(QWidget *parent)
  38. /// \~English
  39. /// \brief Destructor for the MainWindow.
  40. /// \~Spanish
  41. /// \brief Destructor del MainWindow.
  42. MainWindow::~MainWindow(){
  43. delete ui;
  44. }
  45. /// \fn void MainWindow::on_btnSelectImage_clicked()
  46. /// \~English
  47. /// \brief Display the file browsing window to load an image. (Green Background)
  48. /// \~Spanish
  49. /// \brief Despliega la ventana de busqueda de archivos para subir una imagen. (Trasfondo verde)
  50. void MainWindow::on_btnSelectImage_clicked(){
  51. QString fname = QFileDialog::getOpenFileName(this, tr("Choose an image"), QDir::homePath());
  52. if (!fname.isEmpty()){
  53. QImage image(fname);
  54. if (image.isNull()){
  55. QMessageBox::information(this, tr("Choose an image"),tr("Cannot load %1.").arg(fname));
  56. return ;
  57. }
  58. originalImage=image;
  59. }
  60. ui->lblOriginalImage->setPixmap(QPixmap::fromImage(originalImage));
  61. ui->origSize->setText("Original Image Size: " + QString::number(originalImage.width()) + "x" +QString::number(originalImage.height()));
  62. }
  63. /// \fn void MainWindow::on_btnSelectBackground_clicked()
  64. /// \~English
  65. /// \brief Display the file browsing window to load a background image.
  66. /// \~Spanish
  67. /// \brief Despliega la ventana de busqueda de archivos para subir una imagen para el trasfondo.
  68. void MainWindow::on_btnSelectBackground_clicked(){
  69. QString fname = QFileDialog::getOpenFileName(this, tr("Choose an image"), QDir::homePath());
  70. if (!fname.isEmpty()){
  71. QImage image(fname);
  72. if (image.isNull()){
  73. QMessageBox::information(this, tr("Choose an image"),tr("Cannot load %1.").arg(fname));
  74. return ;
  75. }
  76. backgroundImage=image;
  77. }
  78. ui->lblBackgroundImage->setPixmap(QPixmap::fromImage(backgroundImage));
  79. ui->bgSize->setText("Original Image Size: " + QString::number(backgroundImage.width()) + "x" +QString::number(backgroundImage.height()));
  80. }
  81. /// \fn void MainWindow::on_btnMergeImages_clicked()
  82. /// \~English
  83. /// \brief Invokes the function that merges the green screen image with the background image,
  84. /// then displays the merged image.
  85. /// \~Spanish
  86. /// \brief Invoca la funcion que fusional la imagen con transfondo verde con la imagen de transfondo,
  87. /// entonces displiega la imagen fusionada.
  88. void MainWindow::on_btnMergeImages_clicked(){
  89. if (originalImage.width() > backgroundImage.width() || originalImage.height() > backgroundImage.height()){
  90. originalImage = originalImage.scaled(backgroundImage.width(), backgroundImage.height(), Qt::KeepAspectRatio, Qt::FastTransformation) ;
  91. }
  92. mergedImage = backgroundImage;
  93. MergeImages(originalImage, backgroundImage, mergedImage, ui->thresholdSlider->value(),ui->ghostBox->isChecked(), ui->lblOriginalImage->x, ui->lblOriginalImage->y, ui->lblBackgroundImage->x, ui->lblBackgroundImage->y);
  94. ui->lblMergedImage->setPixmap(QPixmap::fromImage(mergedImage));
  95. }
  96. /// \fn void MainWindow::on_btnSaveImage_clicked()
  97. /// \~English
  98. /// \brief Displays the file browse window to choose the path to save
  99. /// the merged image in the file system.
  100. /// \~Spanish
  101. /// \brief Despliega la ventana de busqueda de archivos para escoger un camino
  102. /// guardar la imagen fusionada en el sistema de archivos.
  103. void MainWindow::on_btnSaveImage_clicked(){
  104. if(!mergedImage.isNull()){
  105. QPixmap out = QPixmap::grabWidget(this,361,10,481,481);
  106. QString fname = QFileDialog::getSaveFileName(this, tr("Save Merged Image"), (""), tr("PNG (*.png)" ));
  107. mergedImage.save(fname, "PNG");
  108. }
  109. }
  110. /// \fn void MainWindow::Mouse_Pressed()
  111. /// \~English
  112. /// \brief Function that gets the coordinates of the image with the greenscreen.
  113. /// \~Spanish
  114. /// \brief Funcion que obtiene las coordenada de la imagen con el trasfondo verde.
  115. void MainWindow::Mouse_Pressed(){
  116. if (!originalImage.isNull()){
  117. if (originalImage.height() < ui->lblOriginalImage->height())
  118. ui->lblOriginalImage->y = ui->lblOriginalImage->y * ui->lblOriginalImage->height()/originalImage.height();
  119. else
  120. ui->lblOriginalImage->y = ui->lblOriginalImage->y * originalImage.height()/ui->lblOriginalImage->height();
  121. if(originalImage.width() < ui->lblOriginalImage->width())
  122. ui->lblOriginalImage->x = ui->lblOriginalImage->x * ui->lblOriginalImage->width()/originalImage.width();
  123. else
  124. ui->lblOriginalImage->x = ui->lblOriginalImage->x * originalImage.width()/ui->lblOriginalImage->width();
  125. ui->origCoord->setText("Selected Coord: (" + QString::number(ui->lblOriginalImage->x) + ","+QString::number(ui->lblOriginalImage->y) + ")" );
  126. }
  127. }
  128. /// \fn void MainWindow::Mouse_Pressed()
  129. /// \~English
  130. /// \brief Function that gets the coordinaates of the image with the final background.
  131. /// \~Spanish
  132. /// \brief Funcion que obtiene las coordenada de la imagen con el trasfondo final.
  133. void MainWindow::Mouse_PressedBackground(){
  134. if (!backgroundImage.isNull()){
  135. if (backgroundImage.height() < ui->lblBackgroundImage->height())
  136. ui->lblBackgroundImage->y = ui->lblBackgroundImage->y * ui->lblBackgroundImage->height()/backgroundImage.height();
  137. else
  138. ui->lblBackgroundImage->y = ui->lblBackgroundImage->y * backgroundImage.height()/ui->lblBackgroundImage->height();
  139. if(backgroundImage.width() < ui->lblBackgroundImage->width())
  140. ui->lblBackgroundImage->x = ui->lblBackgroundImage->x * ui->lblBackgroundImage->width()/backgroundImage.width();
  141. else
  142. ui->lblBackgroundImage->x = ui->lblBackgroundImage->x * backgroundImage.width()/ui->lblBackgroundImage->width();
  143. ui->backCoord->setText("Selected Coord: (" + QString::number(ui->lblBackgroundImage->x) + ","+QString::number(ui->lblBackgroundImage->y) + ")" );
  144. }
  145. }
  146. /// \fn void MainWindow::on_thresholdSlider_actionTriggered()
  147. /// \~English
  148. /// \brief Invokes the function that merges the green screen image with the background image.
  149. /// \~Spanish
  150. /// \brief Invoca la funcion que fusiona la imagen de trasfondo verde con la imagen del trasfondo final.
  151. void MainWindow::on_thresholdSlider_actionTriggered(){
  152. if (originalImage.width() > backgroundImage.width() || originalImage.height() > backgroundImage.height())
  153. originalImage = originalImage.scaled(backgroundImage.width(), backgroundImage.height(), Qt::KeepAspectRatio, Qt::FastTransformation) ;
  154. mergedImage = backgroundImage;
  155. MergeImages(originalImage, backgroundImage, mergedImage, ui->thresholdSlider->value(), ui->ghostBox->isChecked(), ui->lblOriginalImage->x, ui->lblOriginalImage->y, ui->lblBackgroundImage->x, ui->lblBackgroundImage->y);
  156. ui->lblMergedImage->setPixmap(QPixmap::fromImage(mergedImage));
  157. }
  158. /// \fn void MainWindow::on_ghostBox_clicked()
  159. /// \~English
  160. /// \brief Funtion that invokes the thresholdSlider function and creates a
  161. /// ghost version of the image with greenscreen background.
  162. /// \~Spanish
  163. /// \brief Funcion que invoca la funcion thresholdSlider y crea una version
  164. /// fantasma de la imagen con el trasfondo verde.
  165. void MainWindow::on_ghostBox_clicked(){
  166. on_thresholdSlider_actionTriggered();
  167. }