Ensure resized image has the same format with the input image. Add drawObjectLabels() to draw all the labels

Signed-off-by: Yu-Ren Zhang <e8035669@gmail.com>
This commit is contained in:
Yu-Ren Zhang 2022-11-07 16:12:24 +08:00
parent 037276bc01
commit 065ee35656
2 changed files with 41 additions and 8 deletions

View File

@ -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));
}
}

View File

@ -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);
};