-
Notifications
You must be signed in to change notification settings - Fork 2
/
main-window.cpp
132 lines (118 loc) · 4.24 KB
/
main-window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <QFileDialog>
#include <QGraphicsScene>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <libgen.h>
#include <iostream>
#include <stdlib.h>
#include "main-window.hpp"
#include "ui_main-window.h"
#include "masks/ring-manager.hpp"
#include "masks/stadium-manager.hpp"
#include "util-opencv-qt.hpp"
static char TEMPORARY_FOLDER_TEMPLATE[] = "/tmp/mask-picker-XXXXXX";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow (parent),
ui (new Ui::MainWindow),
pixmap (new QGraphicsPixmapItem ()),
previous_lock (NULL),
working_folder ("./"),
temporary_folder (mkdtemp (TEMPORARY_FOLDER_TEMPLATE)),
first_image (true)
{
this->ui->setupUi (this);
QGraphicsScene *scene = new QGraphicsScene ();
this->ui->graphicsView->setScene (scene);
scene->addItem (this->pixmap);
this->add_manager (new RingManager (this));
this->add_manager (new MaskStadiumManager (this));
std::cout << "Temporary files are written to folder " << this->temporary_folder << '\n';
}
MainWindow::~MainWindow()
{
delete ui;
for (AbstractManager *m : this->managers) {
delete m;
}
}
void MainWindow::add_manager (AbstractManager *manager)
{
this->managers.push_back (manager);
this->ui->maskTypeComboBox->addItem (manager->label.c_str ());
}
void MainWindow::set_image (const std::string &image_filename)
{
this->image = cv::imread (image_filename, CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat displayed_image;
if (this->ui->enhanceImageCheckBox->isChecked ())
cv::equalizeHist (this->image, displayed_image);
else
displayed_image = this->image;
this->pixmap->setPixmap (QPixmap::fromImage (Mat2QImage (displayed_image)));
this->ui->graphicsView->update ();
// enable widgets
this->ui->maskTypeComboBox->setEnabled (true);
this->ui->enhanceImageCheckBox->setEnabled (true);
if (this->first_image && this->ui->maskTypeComboBox->count () == 1) {
this->managers [0]->setup_components (this->image.size ().width, this->image.size ().height);
// enable widgets
this->ui->createROIMasksPushButton->setEnabled (true);
this->ui->highlightROIsCheckBox->setEnabled (true);
this->ui->numberMasksSpinBox->setEnabled (true);
}
this->first_image = false;
}
void MainWindow::on_loadImagePushButton_clicked()
{
QFileDialog *file_dialog = new QFileDialog ();
file_dialog->setDirectory (this->working_folder.c_str ());
file_dialog->setViewMode (QFileDialog::List);
file_dialog->setOption (QFileDialog::ShowDirsOnly, false);
file_dialog->setFileMode (QFileDialog::ExistingFiles);
int return_code = file_dialog->exec ();
if (return_code == QDialog::Accepted) {
QStringList strings = file_dialog->selectedFiles ();
std::string filename = strings.at (0).toStdString ();
this->set_image (filename);
char *copy = new char [filename.size () + 1];
strcpy (copy, filename.c_str ());
this->working_folder = dirname (copy);
delete copy;
}
}
#pragma GCC diagnostic ignored "-Wunused-parameter"
void MainWindow::on_numberMasksSpinBox_valueChanged (int value)
{
this->managers [this->ui->maskTypeComboBox->currentIndex ()]->update_number_components (this->image.size ().width, this->image.size ().height);
}
void MainWindow::on_createROIMasksPushButton_clicked()
{
AbstractManager *manager = this->managers [this->ui->maskTypeComboBox->currentIndex ()];
manager->create_mask_files (this->working_folder, this->image.size ().width, this->image.size ().height, this->ui->useDefaultMaskFileNameCheckBox->isChecked ());
manager->create_property_files (this->working_folder);
}
void MainWindow::on_maskTypeComboBox_currentIndexChanged (int index)
{
if (this->ui->maskTypeComboBox->isEnabled ()) {
this->managers [index]->setup_components (this->image.size ().width, this->image.size ().height);
// enable widgets
this->ui->createROIMasksPushButton->setEnabled (true);
this->ui->highlightROIsCheckBox->setEnabled (true);
this->ui->numberMasksSpinBox->setEnabled (true);
}
}
void MainWindow::on_enhanceImageCheckBox_toggled (bool checked)
{
cv::Mat displayed_image;
if (checked) {
cv::equalizeHist (this->image, displayed_image);
}
else {
displayed_image = this->image;
}
this->pixmap->setPixmap (QPixmap::fromImage (Mat2QImage (displayed_image)));
this->ui->graphicsView->update ();
}
void MainWindow::on_highlightROIsCheckBox_toggled(bool checked)
{
}