From 065ee3565634186dc5579a5068a836ea25921a62 Mon Sep 17 00:00:00 2001 From: Yu-Ren Zhang Date: Mon, 7 Nov 2022 16:12:24 +0800 Subject: [PATCH] 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); };