From 037276bc01b341bed91686ade0ef37ca19db40a8 Mon Sep 17 00:00:00 2001 From: Yu-Ren Zhang Date: Thu, 3 Nov 2022 17:09:48 +0800 Subject: [PATCH 1/5] add drawing object box with label Signed-off-by: Yu-Ren Zhang --- label_img.cpp | 8 +++++++- label_img.h | 1 + mainwindow.cpp | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/label_img.cpp b/label_img.cpp index e968c22..119178d 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -269,13 +269,19 @@ void label_img::drawObjectBoxes(QPainter& painter, int thickWidth) { QPen pen; pen.setWidth(thickWidth); + QFont font = painter.font(); + font.setPixelSize(12); + font.setBold(true); + painter.setFont(font); for(ObjectLabelingBox boundingbox: m_objBoundingBoxes) { pen.setColor(m_drawObjectBoxColor.at(boundingbox.label)); painter.setPen(pen); - painter.drawRect(cvtRelativeToAbsoluteRectInUi(boundingbox.box)); + QRect rectUi = cvtRelativeToAbsoluteRectInUi(boundingbox.box); + painter.drawRect(rectUi); + painter.drawText(rectUi.topLeft() + QPoint(5, 14 + 14 * boundingbox.label), m_objList.at(boundingbox.label)); } } diff --git a/label_img.h b/label_img.h index 1badeda..b862217 100644 --- a/label_img.h +++ b/label_img.h @@ -25,6 +25,7 @@ public: void mouseReleaseEvent(QMouseEvent *ev); QVector m_drawObjectBoxColor; + QStringList m_objList; int m_uiX; int m_uiY; diff --git a/mainwindow.cpp b/mainwindow.cpp index 4c867bf..4b5dc37 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -250,6 +250,7 @@ void MainWindow::load_label_list_data(QString qstrLabelListFile) ui->label_image->m_drawObjectBoxColor.push_back(labelColor); } + ui->label_image->m_objList = m_objList; } } From 065ee3565634186dc5579a5068a836ea25921a62 Mon Sep 17 00:00:00 2001 From: Yu-Ren Zhang Date: Mon, 7 Nov 2022 16:12:24 +0800 Subject: [PATCH 2/5] Ensure resized image has the same format with the input image. Add `drawObjectLabels()` to draw all the labels Signed-off-by: Yu-Ren Zhang --- label_img.cpp | 48 ++++++++++++++++++++++++++++++++++++++++-------- label_img.h | 1 + 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/label_img.cpp b/label_img.cpp index 119178d..1c1f41e 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -125,7 +125,8 @@ void label_img::openImage(const QString &qstrImg, bool &ret) m_inputImg = img.copy(); m_inputImg = m_inputImg.convertToFormat(QImage::Format_RGB888); - m_resized_inputImg = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + m_resized_inputImg = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation) + .convertToFormat(QImage::Format_RGB888); m_bLabelingStarted = false; @@ -148,7 +149,8 @@ void label_img::showImage() if(m_inputImg.isNull()) return; if(m_resized_inputImg.width() != this->width() or m_resized_inputImg.height() != this->height()) { - m_resized_inputImg = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + m_resized_inputImg = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation) + .convertToFormat(QImage::Format_RGB888); } QImage img = m_resized_inputImg; @@ -156,6 +158,11 @@ void label_img::showImage() gammaTransform(img); QPainter painter(&img); + QFont font = painter.font(); + int fontSize = 14, xMargin = 5, yMargin = 2; + font.setPixelSize(fontSize); + font.setBold(true); + painter.setFont(font); int penThick = 3; @@ -164,6 +171,7 @@ void label_img::showImage() drawCrossLine(painter, crossLineColor, penThick); drawFocusedObjectBox(painter, Qt::magenta, penThick); drawObjectBoxes(painter, penThick); + drawObjectLabels(painter, penThick, fontSize, xMargin, yMargin); this->setPixmap(QPixmap::fromImage(img)); } @@ -269,19 +277,43 @@ void label_img::drawObjectBoxes(QPainter& painter, int thickWidth) { QPen pen; pen.setWidth(thickWidth); - QFont font = painter.font(); - font.setPixelSize(12); - font.setBold(true); - painter.setFont(font); for(ObjectLabelingBox boundingbox: m_objBoundingBoxes) { pen.setColor(m_drawObjectBoxColor.at(boundingbox.label)); painter.setPen(pen); + painter.drawRect(cvtRelativeToAbsoluteRectInUi(boundingbox.box)); + } +} + +void label_img::drawObjectLabels(QPainter& painter, int thickWidth, int fontPixelSize, int xMargin, int yMargin) +{ + QFontMetrics fontMetrics = painter.fontMetrics(); + QPen blackPen; + + for(ObjectLabelingBox boundingbox: m_objBoundingBoxes) + { + QColor labelColor = m_drawObjectBoxColor.at(boundingbox.label); QRect rectUi = cvtRelativeToAbsoluteRectInUi(boundingbox.box); - painter.drawRect(rectUi); - painter.drawText(rectUi.topLeft() + QPoint(5, 14 + 14 * boundingbox.label), m_objList.at(boundingbox.label)); + + QRect labelRect = fontMetrics.boundingRect(m_objList.at(boundingbox.label)); + if (rectUi.top() > fontPixelSize + yMargin * 2 + thickWidth + 1) { + labelRect.moveTo(rectUi.topLeft() + QPoint(-thickWidth / 2, -(fontPixelSize + yMargin * 2 + thickWidth + 1))); + labelRect.adjust(0, 0, xMargin * 2, yMargin * 2); + } else { + labelRect.moveTo(rectUi.topLeft() + QPoint(-thickWidth / 2, 0)); + labelRect.adjust(0, 0, xMargin * 2, yMargin * 2); + } + painter.fillRect(labelRect, labelColor); + + if (qGray(m_drawObjectBoxColor.at(boundingbox.label).rgb()) > 120) { + blackPen.setColor(QColorConstants::Black); + } else { + blackPen.setColor(QColorConstants::White); + } + painter.setPen(blackPen); + painter.drawText(labelRect.topLeft() + QPoint(xMargin, yMargin + fontPixelSize), m_objList.at(boundingbox.label)); } } diff --git a/label_img.h b/label_img.h index b862217..0a628a9 100644 --- a/label_img.h +++ b/label_img.h @@ -87,6 +87,7 @@ private: void drawCrossLine(QPainter& , QColor , int thickWidth = 3); void drawFocusedObjectBox(QPainter& , Qt::GlobalColor , int thickWidth = 3); void drawObjectBoxes(QPainter& , int thickWidth = 3); + void drawObjectLabels(QPainter& , int thickWidth = 3, int fontPixelSize = 14, int xMargin = 5, int yMargin = 2); void gammaTransform(QImage& image); void removeFocusedObjectBox(QPointF); }; From 588bd80e8ac2c8adb9e01276ef4dfb257781d6b1 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Tue, 10 Jan 2023 20:54:53 +0900 Subject: [PATCH 3/5] add visualize class names checkbox --- mainwindow.ui | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/mainwindow.ui b/mainwindow.ui index b1b676f..b93882a 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -57,7 +57,7 @@ - + @@ -77,6 +77,7 @@ + Arial 18 75 true @@ -106,7 +107,7 @@ border-color: rgb(0, 255, 255);} 3 - <html><head/><body><p>Open a Dataset Directory...</p><p>D: Next Image</p><p>A: Prev Image</p><p><br/>Ctrl + S: Save</p><p>Ctrl + D: Delete an Image<br/></p></body></html> + <html><head/><body><p>Open a Dataset Directory...</p><p>D: Next Image</p><p>A: Prev Image</p><p>S: Next Class</p><p>W: Prev Class</p><p>V: Visualize Class Name<br/>Ctrl + S: Save</p><p>Ctrl + D: Delete an Image<br/></p></body></html> true @@ -575,6 +576,32 @@ QTableView { + + + + + + + Arial + 12 + false + false + + + + background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); + + + + Visualize Class Names + + + V + + + + + From 7796b8606d8df8b6a952063c7115f8aa7450011c Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Tue, 10 Jan 2023 21:31:04 +0900 Subject: [PATCH 4/5] add shortcut for open file button and description for other functions --- mainwindow.ui | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mainwindow.ui b/mainwindow.ui index b93882a..18c7baa 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -107,7 +107,7 @@ border-color: rgb(0, 255, 255);} 3 - <html><head/><body><p>Open a Dataset Directory...</p><p>D: Next Image</p><p>A: Prev Image</p><p>S: Next Class</p><p>W: Prev Class</p><p>V: Visualize Class Name<br/>Ctrl + S: Save</p><p>Ctrl + D: Delete an Image<br/></p></body></html> + <html><head/><body><p>Open a Dataset Directory...</p><p>D: Next Image</p><p>A: Prev Image</p><p>S: Next Class</p><p>W: Prev Class</p><p>V: Visualize Class Name</p><p>O: Open Files<br/>Ctrl + S: Save</p><p>Ctrl + D: Delete an Image<br/></p></body></html> true @@ -397,6 +397,9 @@ border-color: rgb(0, 255, 255); Open Files + + O + false @@ -579,7 +582,7 @@ QTableView { - + Arial @@ -593,7 +596,7 @@ QTableView { - Visualize Class Names + Visualize Class Name V From cc3a1882755f389e9bf7c0a09dcda88ccb6e5072 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Tue, 10 Jan 2023 21:32:09 +0900 Subject: [PATCH 5/5] turn on or off visualization function with a check box --- label_img.cpp | 6 ++++-- label_img.h | 1 + mainwindow.cpp | 13 +++++++------ mainwindow.h | 8 ++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/label_img.cpp b/label_img.cpp index 1c1f41e..76c400a 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -80,6 +80,7 @@ void label_img::init() { m_objBoundingBoxes.clear(); m_bLabelingStarted = false; + m_bVisualizeClassName = false; m_focusedObjectLabel = 0; QPoint mousePosInUi = this->mapFromGlobal(QCursor::pos()); @@ -159,7 +160,7 @@ void label_img::showImage() QPainter painter(&img); QFont font = painter.font(); - int fontSize = 14, xMargin = 5, yMargin = 2; + int fontSize = 16, xMargin = 5, yMargin = 2; font.setPixelSize(fontSize); font.setBold(true); painter.setFont(font); @@ -171,7 +172,8 @@ void label_img::showImage() drawCrossLine(painter, crossLineColor, penThick); drawFocusedObjectBox(painter, Qt::magenta, penThick); drawObjectBoxes(painter, penThick); - drawObjectLabels(painter, penThick, fontSize, xMargin, yMargin); + if(m_bVisualizeClassName) + drawObjectLabels(painter, penThick, fontSize, xMargin, yMargin); this->setPixmap(QPixmap::fromImage(img)); } diff --git a/label_img.h b/label_img.h index 0a628a9..157f8bc 100644 --- a/label_img.h +++ b/label_img.h @@ -34,6 +34,7 @@ public: int m_imgY; bool m_bLabelingStarted; + bool m_bVisualizeClassName; static QColor BOX_COLORS[10]; diff --git a/mainwindow.cpp b/mainwindow.cpp index 4b5dc37..34cccc4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -198,7 +198,7 @@ void MainWindow::remove_img() } else if( m_imgIndex == m_imgList.size()) { - m_imgIndex --; + m_imgIndex--; } goto_img(m_imgIndex); @@ -361,11 +361,6 @@ void MainWindow::open_obj_file(bool& ret) } } -void MainWindow::reupdate_img_list() -{ - -} - void MainWindow::wheelEvent(QWheelEvent *ev) { if(ev->angleDelta().y() > 0) // up Wheel @@ -480,3 +475,9 @@ void MainWindow::on_horizontalSlider_contrast_sliderMoved(int value) ui->label_image->setContrastGamma(percentageToGamma); ui->label_contrast->setText(QString("Contrast(%) ") + QString::number(int(valueToPercentage * 100.))); } + +void MainWindow::on_checkBox_visualize_class_name_clicked(bool checked) +{ + ui->label_image->m_bVisualizeClassName = checked; + ui->label_image->showImage(); +} diff --git a/mainwindow.h b/mainwindow.h index 9312377..f958522 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -23,10 +23,6 @@ public: private slots: void on_pushButton_open_files_clicked(); -// void on_pushButton_change_dir_clicked(); -// void on_pushButton_save_clicked(); -// void on_pushButton_remove_clicked(); - void on_pushButton_prev_clicked(); void on_pushButton_next_clicked(); @@ -48,6 +44,8 @@ private slots: void on_horizontalSlider_contrast_sliderMoved(int value); + void on_checkBox_visualize_class_name_clicked(bool checked); + private: void init(); void init_table_widget(); @@ -71,8 +69,6 @@ private: void open_img_dir(bool&); void open_obj_file(bool&); - void reupdate_img_list(); - Ui::MainWindow *ui; QString m_imgDir;