Skip to content

Commit dc13e15

Browse files
v2 - several bugs gone + improvements
1 parent e251e14 commit dc13e15

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

mainwindow.cpp

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ MainWindow::MainWindow(QWidget *parent) :
4848
ui->comboBox_grid_color->addItem(tr("White"));
4949
ui->comboBox_grid_color->blockSignals(false);
5050

51-
ui->comboBox_algorithm->setCurrentIndex(4);
51+
ui->comboBox_algorithm->setCurrentIndex(4); // SLIC
5252

53-
InitializeValues();
53+
InitializeValues(); // init all indicators and zoom etc
5454

55-
basedir = "/media/DataI5/Photos/";
55+
basedir = "/media/DataI5/Photos/"; // base path and file
5656
basefile = "";
5757
}
5858

@@ -61,15 +61,15 @@ MainWindow::~MainWindow()
6161
delete ui;
6262
}
6363

64-
/////////////////// UI //////////////////////
64+
/////////////////// GUI //////////////////////
6565

6666
void MainWindow::on_Tabs_currentChanged(int) // when a tab is clicked
6767
{
6868
if(ui->Tabs->currentIndex()==2) ui->label_segmentation->setCursor(Qt::PointingHandCursor); // labels tab ? => labels view cursor
6969
else ui->label_segmentation->setCursor(Qt::ArrowCursor); // default cursor
7070
}
7171

72-
void MainWindow::InitializeValues()
72+
void MainWindow::InitializeValues() // initialize all sorts of indicators, zoom, etc
7373
{
7474
// Hide tabs
7575
ui->Tabs->setTabEnabled(1, false);
@@ -111,13 +111,13 @@ void MainWindow::DeleteAllLabels(bool newLabel) // add a label to the list
111111

112112
void MainWindow::UnselectAllLabels() // unselect all labels in list
113113
{
114-
for (int n = 0; n < ui->listWidget_labels->count(); n++)
115-
ui->listWidget_labels->item(n)->setSelected(false);
114+
for (int n = 0; n < ui->listWidget_labels->count(); n++) // for each label
115+
ui->listWidget_labels->item(n)->setSelected(false); // unselect it
116116
}
117117

118118
QListWidgetItem* MainWindow::AddNewLabel(QString newLabel) // add a label to the list
119119
{
120-
UnselectAllLabels();
120+
UnselectAllLabels(); // the new one will be the only selected
121121

122122
QListWidgetItem *item = new QListWidgetItem ();
123123
if (newLabel == "") item->setText("Rename me!"); // default text of the label
@@ -137,16 +137,16 @@ QListWidgetItem* MainWindow::AddNewLabel(QString newLabel) // add a label to the
137137

138138
void MainWindow::on_listWidget_labels_currentItemChanged(QListWidgetItem *currentItem) // show current label color
139139
{
140-
QBrush brush = currentItem->background();
141-
ShowCurrentColor(brush.color().red(), brush.color().green(), brush.color().blue());
140+
QBrush brush = currentItem->background(); // the label color is stored in the background color of the list
141+
ShowCurrentColor(brush.color().red(), brush.color().green(), brush.color().blue()); // show current color used
142142
}
143143

144144
void MainWindow::on_listWidget_labels_itemChanged(QListWidgetItem *currentItem) // show current label text
145145
{
146-
ui->label_color->setText(currentItem->text());
146+
ui->label_color->setText(currentItem->text()); // show label name
147147
}
148148

149-
void MainWindow::on_pushButton_label_add_clicked() // add a new default label
149+
void MainWindow::on_pushButton_label_add_clicked() // add new default label
150150
{
151151
if (!computed) { // if image not computed yet get out
152152
QMessageBox::warning(this, "No cells found",
@@ -229,7 +229,7 @@ void MainWindow::on_pushButton_label_join_clicked() // join 2 or more labels, re
229229
}
230230
items[0]->setText(items[0]->text() + " (joined)"); // add "join" to the first label name containing the result
231231

232-
ShowSegmentation(); // show the result
232+
ShowSegmentation(); // show result
233233
}
234234

235235
void MainWindow::on_pushButton_label_create_clicked() // special mode to modify the cells (superpixels)
@@ -249,7 +249,7 @@ void MainWindow::on_pushButton_label_create_clicked() // special mode to modify
249249

250250
ui->label_segmentation->setCursor(Qt::CrossCursor); // cursor change to show this is a special mode
251251

252-
ui->frame_pick_colors->setVisible(false); // disable all the tabs/elements to keep the user in the special mode
252+
ui->frame_pick_colors->setVisible(false); // disable all tabs/elements to keep the user in the special mode
253253
ui->pushButton_label_add->setVisible(false);
254254
ui->pushButton_label_delete->setVisible(false);
255255
ui->pushButton_label_hide->setVisible(false);
@@ -436,14 +436,13 @@ void MainWindow::on_button_image_clicked() // Load main image
436436
return;
437437
}
438438

439-
ui->label_thumbnail->setPixmap(QPixmap());
439+
ui->label_thumbnail->setPixmap(QPixmap()); // unset all images in GUI
440440
ui->label_segmentation->setPixmap(QPixmap());
441441
ui->label_thumbnail->setPixmap(Mat2QPixmapResized(mat, ui->label_thumbnail->width(), ui->label_thumbnail->height(), true)); // Show thumbnail
442442

443443
image = mat; // store the image for further use
444444
mat.copyTo(image_backup); // for undo
445-
image.copyTo(selection); // selection mask
446-
selection = 0;
445+
selection = Mat::zeros(image.rows, image.cols, CV_8UC3); // init selection mask
447446

448447
ui->label_segmentation->setPixmap(QPixmap()); // Delete the depthmap image
449448
ui->label_segmentation->setText("Segmentation not computed"); // Text in the segmentation area
@@ -458,7 +457,7 @@ void MainWindow::on_button_image_clicked() // Load main image
458457
ui->label_image_width->setText(QString::number(image.cols)); // display image dimensions
459458
ui->label_image_height->setText(QString::number(image.rows));
460459

461-
InitializeValues();
460+
InitializeValues(); // reset all variables
462461

463462
double zoomX = double(ui->label_segmentation->width()) / image.cols; // try vertical and horizontal ratios
464463
double zoomY = double(ui->label_segmentation->height()) / image.rows;
@@ -592,8 +591,10 @@ void MainWindow::on_button_load_session_clicked() // load previous session
592591
size_t pos = filesession.find("-segmentation-data.xml");
593592
if (pos != std::string::npos) filesession.erase(pos, filesession.length());
594593

595-
InitializeValues();
594+
InitializeValues(); // reinit all variables
596595
image.release();
596+
mask.release();
597+
grid.release();
597598

598599
mask = cv::imread(filesession + "-segmentation-mask.png", CV_LOAD_IMAGE_COLOR); // load mask
599600
if (mask.empty()) {
@@ -674,7 +675,7 @@ void MainWindow::on_button_load_session_clicked() // load previous session
674675

675676
fs.release(); // close file
676677

677-
ui->label_thumbnail->setPixmap(QPixmap());
678+
ui->label_thumbnail->setPixmap(QPixmap()); // reinit all images in GUI
678679
ui->label_segmentation->setPixmap(QPixmap());
679680
ui->label_thumbnail->setPixmap(Mat2QPixmapResized(image, ui->label_thumbnail->width(), ui->label_thumbnail->height(), true)); // Show thumbnail
680681

@@ -694,8 +695,7 @@ void MainWindow::on_button_load_session_clicked() // load previous session
694695
selection.release();
695696

696697
image.copyTo(image_backup); // for undo
697-
image.copyTo(selection); // selection mask
698-
selection = 0;
698+
selection = Mat::zeros(image.rows, image.cols, CV_8UC3);
699699

700700
double min, max;
701701
minMaxIdx(labels, &min, &max); // find the highest superpixel number
@@ -705,11 +705,11 @@ void MainWindow::on_button_load_session_clicked() // load previous session
705705
ui->label_image_height->setText(QString::number(image.rows));
706706

707707
SaveUndo(); // for undo
708-
loaded = true;
708+
loaded = true; // done !
709709
computed = true; // not really computed but necessary
710710

711711
ui->comboBox_grid_color->setCurrentIndex(gridValue); // update grid color in ui
712-
UnselectAllLabels();
712+
UnselectAllLabels(); // only the first label must be selected
713713
ui->listWidget_labels->setCurrentRow(0);
714714

715715
//QMessageBox::information(this, "Load segmentation session", "Session loaded with base name:\n" + QString::fromStdString(filesession));
@@ -1293,7 +1293,7 @@ void MainWindow::on_pushButton_color_laurel_clicked() // Laurel
12931293

12941294
/////////////////// Key events //////////////////////
12951295

1296-
void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent) // draw cell mode : when spacebar is released
1296+
void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent) // draw cell mode and move view
12971297
{
12981298
if (keyEvent->key() == Qt::Key_Space) { // spacebar = move the view
12991299
QPoint mouse_pos = QCursor::pos(); // current mouse position
@@ -1303,7 +1303,7 @@ void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent) // draw cell mode : when s
13031303

13041304
SetViewportXY(viewport.x - double(decX) / zoom, viewport.y - double(decY) / zoom); // update viewport
13051305

1306-
ShowSegmentation(); // display the result
1306+
ShowSegmentation(); // display result
13071307

13081308
QApplication::restoreOverrideCursor(); // Restore cursor
13091309
}
@@ -1320,9 +1320,6 @@ void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent) // draw cell mode : when s
13201320
QPoint mouse_pos = ui->label_segmentation->mapFromGlobal(QCursor::pos()); // current mouse position
13211321
cv::Point pos = Viewport2Image(cv::Point(mouse_pos.x(), mouse_pos.y())); // convert position from viewport to image coordinates
13221322

1323-
if ((pos.x >= 0) & (pos.x < image.cols)
1324-
& (pos.y >= 0) & (pos.y < image.rows))
1325-
13261323
mask_line_save.copyTo(mask); // erase line (mouse position can have changed a bit
13271324
SaveUndo(); // for undo
13281325

@@ -1331,6 +1328,7 @@ void MainWindow::keyReleaseEvent(QKeyEvent *keyEvent) // draw cell mode : when s
13311328

13321329
mask.copyTo(mask_line_save); // save result for next line
13331330
pos_save = pos; // the last pixel is the new line origin
1331+
13341332
}
13351333
}
13361334
}
@@ -1368,7 +1366,7 @@ void MainWindow::mousePressEvent(QMouseEvent *eventPress) // event triggered by
13681366
{
13691367
if (!loaded) return; // no image = get out
13701368

1371-
if (ui->label_segmentation->underMouse()) { // if the mouse is over the viewport
1369+
if (ui->label_segmentation->underMouse()) { // mouse over the viewport ?
13721370
bool key_control = QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ControlModifier); // modifier keys pressed ? (shift control etc)
13731371
bool key_alt = QGuiApplication::queryKeyboardModifiers().testFlag(Qt::AltModifier);
13741372

@@ -1381,7 +1379,7 @@ void MainWindow::mousePressEvent(QMouseEvent *eventPress) // event triggered by
13811379
return;
13821380
}
13831381

1384-
if ((computed) & (ui->Tabs->currentIndex() == 2)) { // in labels tab ?
1382+
if ((computed) & (ui->Tabs->currentIndex() == 2)) { // mouse in labels tab ?
13851383
if ((key_alt) & (mouseButton == Qt::LeftButton) & (pos.x >= 0) & (pos.x < image.cols)
13861384
& (pos.y >= 0) & (pos.y < image.rows)) { // <alt> pressed at the same time = label selection
13871385
int value = labels_mask.at<int>(pos.y, pos.x); // get label mask value
@@ -1495,7 +1493,7 @@ void MainWindow::mouseMoveEvent(QMouseEvent *eventMove) // first mouse click has
14951493

14961494
mouse_origin = mouse_pos; // save mouse position
14971495
}
1498-
else if ((computed) & (ui->Tabs->currentIndex() == 2)) {
1496+
else if ((computed) & (ui->Tabs->currentIndex() == 2)) { // mouse over tab 2 ?
14991497
cv::Point pos = Viewport2Image(cv::Point(mouse_pos.x(), mouse_pos.y())); // convert position from viewport to image coordinates
15001498

15011499
if (ui->pushButton_label_create->isChecked()) { // direct draw pixel for new label
@@ -1516,7 +1514,7 @@ void MainWindow::mouseMoveEvent(QMouseEvent *eventMove) // first mouse click has
15161514

15171515
ShowSegmentation(); // show result
15181516
}
1519-
else if ((pos.x >= 0) & (pos.x < image.cols)
1517+
else if ((pos.x >= 0) & (pos.x < image.cols) // define cell color
15201518
& (pos.y >= 0) & (pos.y < image.rows)){
15211519
// Color already set before ? saves CPU time
15221520
//Vec3b pixel = mask.at<Vec3b>(pos.y,pos.x); // find mask pixel color
@@ -1574,7 +1572,7 @@ void MainWindow::DisplayThumbnail() // image thumnail with transparent viewport
15741572
p1x = 0;
15751573
p2x = ui->label_thumbnail->pixmap()->width();
15761574
}
1577-
else {
1575+
else { // partial view
15781576
p1x = double(viewport.x) / image.cols;
15791577
p2x = p1x + double(viewport.width) / image.cols;
15801578
p1x *= double(ui->label_thumbnail->pixmap()->width());
@@ -1585,16 +1583,15 @@ void MainWindow::DisplayThumbnail() // image thumnail with transparent viewport
15851583
p1y = 0;
15861584
p2y= ui->label_thumbnail->pixmap()->height();
15871585
}
1588-
else {
1586+
else { // partial view
15891587
p1y = double(viewport.y) / image.rows;
15901588
p2y = p1y + double(viewport.height) / image.rows;
15911589
p1y *= double(ui->label_thumbnail->pixmap()->height());
15921590
p2y *= double(ui->label_thumbnail->pixmap()->height());
15931591
}
15941592

15951593
Mat thumbnail_temp;
1596-
thumbnail.copyTo(thumbnail_temp);
1597-
thumbnail_temp = 0;
1594+
Mat thumbnail_temp = Mat::zeros(thumbnail.rows, thumbnail.cols, CV_8UC3);
15981595
rectangle(thumbnail_temp, cv::Point(p1x, p1y), cv::Point(p2x-1, p2y-1), Scalar(92,92,92), -1); // draw filled rectangle representing the viewport position
15991596
rectangle(thumbnail_temp, cv::Point(p1x, p1y), cv::Point(p2x-1, p2y-1), Scalar(255,255,255), 2); // draw rectangle with thick border
16001597
cv::addWeighted(thumbnail, 1, thumbnail_temp, 0.25, 0, thumbnail_temp, -1); // with transparency
@@ -1603,23 +1600,23 @@ void MainWindow::DisplayThumbnail() // image thumnail with transparent viewport
16031600

16041601
void MainWindow::UpdateViewportDimensions() // recomputes the viewport width and height
16051602
{
1606-
viewport.width = double(ui->label_segmentation->width()) / zoom;
1603+
viewport.width = double(ui->label_segmentation->width()) / zoom; // viewport width
16071604
if (viewport.width > image.cols) {
16081605
viewport.width = image.cols;
16091606
viewport.x = 0;
16101607
}
1611-
viewport.height = double(ui->label_segmentation->height()) / zoom;
1608+
viewport.height = double(ui->label_segmentation->height()) / zoom; // viewport height
16121609
if (viewport.height > image.rows) {
16131610
viewport.height = image.rows;
16141611
viewport.y = 0;
16151612
}
1616-
if (viewport.x > image.cols - viewport.width) viewport.x = image.cols - viewport.width;
1613+
if (viewport.x > image.cols - viewport.width) viewport.x = image.cols - viewport.width; // stay within image
16171614
if (viewport.y > image.rows - viewport.height) viewport.y = image.rows - viewport.height;
16181615
}
16191616

16201617
void MainWindow::SetViewportXY(int x, int y) // set viewport top-left to (x,y) new coordinates
16211618
{
1622-
viewport.x = x;
1619+
viewport.x = x; // coordinates
16231620
viewport.y = y;
16241621
if (viewport.x < 0) viewport.x = 0;
16251622
if (viewport.x > image.cols - viewport.width) viewport.x = image.cols - viewport.width;
@@ -1683,11 +1680,11 @@ void MainWindow::ShowSegmentation() // show image + mask + grid
16831680
if (ui->checkBox_holes->isChecked() & (!mask.empty())) {// show holes in mask
16841681
Mat holes = cv::Mat::zeros(mask.rows, mask.cols, CV_8UC3); // to draw the holes
16851682
Mat1b holes_mask;
1686-
cv::inRange(mask, Vec3b(0,0,0), Vec3b(0,0,0), holes_mask); // extract mask = 0
1683+
cv::inRange(mask, Vec3b(0,0,0), Vec3b(0,0,0), holes_mask); // extract mask = unset label zones
16871684
holes.setTo(Vec3b(255,255,255), holes_mask); // draw holes in white
16881685
cv::addWeighted(disp_color, 1,
16891686
CopyFromImage(holes, viewport), 1,
1690-
0, disp_color, -1);
1687+
0, disp_color, -1); // show the mask over the whole display
16911688
}
16921689

16931690
// display the viewport
@@ -1849,8 +1846,7 @@ void MainWindow::on_button_compute_clicked() // compute segmentation
18491846
seeds->getLabelContourMask(maskTemp, !ui->checkBox_thick->isChecked()); // contours
18501847
}
18511848

1852-
labels.copyTo(labels_mask);
1853-
labels_mask = 0;
1849+
labels_mask = Mat::zeros(labels.rows, labels.cols, CV_8UC3); // new labels mask
18541850

18551851
// contours for displaying : grid & mask initialization
18561852
cv::cvtColor(maskTemp, mask, CV_GRAY2RGB); // at first the mask contains the grid, need to convert labels from gray to RGB
@@ -1866,7 +1862,7 @@ void MainWindow::on_button_compute_clicked() // compute segmentation
18661862
// convert the image to a QPixmap and display it
18671863
ShowSegmentation(); // update display
18681864

1869-
ui->Tabs->setTabEnabled(2, true);
1865+
ui->Tabs->setTabEnabled(2, true); // computed = we can edit labels
18701866

18711867
QApplication::restoreOverrideCursor(); // Restore cursor
18721868
}

mainwindow.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public slots:
4848
private slots:
4949

5050
//// UI
51-
void InitializeValues();
51+
void InitializeValues(); // well named !
5252

5353
//// Tabs
5454
void on_Tabs_currentChanged(int); // tabs handling
@@ -134,7 +134,7 @@ private slots:
134134
void on_button_compute_clicked(); // compute segmentation mask and grid
135135

136136
//// Keyboard & mouse events
137-
void keyPressEvent(QKeyEvent *keyEvent); // for the create cell mode
137+
void keyPressEvent(QKeyEvent *keyEvent); // for the create cell mode and move with <SPACE>
138138
void keyReleaseEvent(QKeyEvent *keyEvent);
139139

140140
void mouseReleaseEvent(QMouseEvent *eventRelease); // when the mouse button is released
@@ -143,7 +143,11 @@ private slots:
143143
void wheelEvent(QWheelEvent *wheelEvent);
144144

145145
private:
146+
// the UI object, to access the UI elements created with Qt Designer
147+
Ui::MainWindow *ui;
148+
146149
//// Display
150+
147151
void ShowSegmentation(); // display image in viewport with grid and mask
148152
void DisplayThumbnail(); // display thumbnail view
149153

@@ -157,13 +161,14 @@ private slots:
157161
void UpdateViewportDimensions(); // calculate width and height of the viewport
158162
cv::Point Viewport2Image(cv::Point p); // calculate coordinates in the image from the viewport
159163

160-
// the UI object, to access the UI elements created with Qt Designer
161-
Ui::MainWindow *ui;
164+
//// superpixels
162165

163166
Ptr<SuperpixelSLIC> slic; // SLIC Segmentation pointer
164167
Ptr<SuperpixelLSC> lsc; // LSC Segmentation pointer
165168
Ptr<SuperpixelSEEDS> seeds; // SEEDS Segmentation pointer
166169

170+
//// masks & display
171+
167172
cv::Mat labels, labels_mask, undo_labels; // Segmentation cells and labels
168173
int maxLabels; // max number of labels
169174
int nb_cells; // number of labels in segmentation
@@ -180,18 +185,26 @@ private slots:
180185

181186
Vec3b color, gridColor; // current colors used
182187

188+
//// files
189+
183190
std::string basefile, basedir; // main image filename: directory and filename without extension
184191

192+
//// mouse
193+
185194
Qt::MouseButton mouseButton; // save mouse button value when holding down a mouse button
186195
QPoint mouse_origin; // save the mouse position when holding down a mouse button
187196
cv::Point pos_save; // save the mouse position in image coordinates
188197

198+
//// zoom
199+
189200
int num_zooms = 22; // number of zoom values
190201
double zooms[23] = {0, 0.05, 0.0625, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, // reduced view
191202
1, 1.25, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10000}; // zoomed view
192203
double zoom, oldZoom; // zoom factor for image display
193204
QString zoom_type; // if zoom changes indicates where it came from: button clic or mouse wheel
194205

206+
//// indicators
207+
195208
bool loaded, computed; // indicators: image loaded & segmentation computed
196209

197210
};

0 commit comments

Comments
 (0)