From 25bc781e66af5d36f6862ea00d53cc8fa22ddbbc Mon Sep 17 00:00:00 2001 From: yonghye Date: Tue, 6 Nov 2018 20:27:12 +0900 Subject: [PATCH] Change Coordinate system: Relative to Absolute --- label_img.cpp | 130 +++++++++++++++++++++++++++++++++---------------- label_img.h | 29 ++++++----- mainwindow.cpp | 22 +++++---- 3 files changed, 117 insertions(+), 64 deletions(-) diff --git a/label_img.cpp b/label_img.cpp index 06568d4..86e2f2b 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -40,13 +40,13 @@ void label_img::mousePressEvent(QMouseEvent *ev) if(ev->button() == Qt::RightButton) { - removeFocusedObjectBox(m_mouse_pos_in_image_coordinate); + removeFocusedObjectBox(m_relative_mouse_pos_in_ui); showImage(); } else if(ev->button() == Qt::LeftButton) { - m_bMouseLeftButtonClicked = true; - m_leftButtonClickedPoint = m_mouse_pos_in_image_coordinate; + m_bMouseLeftButtonClicked = true; + m_relatvie_mouse_pos_LBtnClicked_in_ui = m_relative_mouse_pos_in_ui; } emit Mouse_Pressed(); @@ -63,18 +63,19 @@ void label_img::mouseReleaseEvent(QMouseEvent *ev) ObjectLabelingBox objBoundingbox; objBoundingbox.label = m_focusedObjectLabel; - objBoundingbox.box = getRectFromTwoPoints(m_mouse_pos_in_image_coordinate, m_leftButtonClickedPoint); + objBoundingbox.box = getRelativeRectFromTwoPoints(m_relative_mouse_pos_in_ui, + m_relatvie_mouse_pos_LBtnClicked_in_ui); - bool width_is_too_small = objBoundingbox.box.width() < m_inputImg.width() * 0.01; - bool height_is_too_small = objBoundingbox.box.height() < m_inputImg.height() * 0.01; + bool width_is_too_small = objBoundingbox.box.width() < 0.01; + bool height_is_too_small = objBoundingbox.box.height() < 0.01; if(!width_is_too_small && !height_is_too_small) m_objBoundingBoxes.push_back(objBoundingbox); - showImage(); - m_bMouseLeftButtonClicked = false; } + + showImage(); emit Mouse_Release(); } @@ -86,22 +87,17 @@ void label_img::setMousePosition(int x, int y) if(x > this->width()) x = this->width() - 1; if(y > this->height()) y = this->height() - 1; - m_aspectRatioWidth = static_cast(m_inputImg.width()) / this->width(); - m_aspectRatioHeight = static_cast(m_inputImg.height()) / this->height(); - - m_mouse_pos_in_image_coordinate = QPoint(static_cast(x * m_aspectRatioWidth + 0.5), - static_cast(y * m_aspectRatioHeight + 0.5)); + m_relative_mouse_pos_in_ui = cvtAbsoluteToRelativePoint(QPoint(x, y)); } void label_img::openImage(const QString &qstrImg) { m_inputImg = QImage(qstrImg); m_inputImg = m_inputImg.convertToFormat(QImage::Format_RGB888); - m_inputImgCopy = m_inputImg; m_objBoundingBoxes.clear(); - m_leftButtonClickedPoint = QPoint(); + m_relatvie_mouse_pos_LBtnClicked_in_ui = QPointF(); m_bMouseLeftButtonClicked = false; @@ -112,21 +108,17 @@ void label_img::openImage(const QString &qstrImg) { setMousePosition(mousePosInUi.x(), mousePosInUi.y()); } - else - { - m_mouse_pos_in_image_coordinate = QPoint(); - } } void label_img::showImage() { if(m_inputImg.isNull()) return; - m_inputImg = m_inputImgCopy; + QImage imageOnUi = m_inputImg.scaled(this->width(), this->height()); - QPainter painter(&m_inputImg); + QPainter painter(&imageOnUi); - int penThick = (m_inputImg.width() + m_inputImg.height())/400; + int penThick = 3; QColor crossLineColor(255, 187, 0); @@ -134,7 +126,7 @@ void label_img::showImage() drawFocusedObjectBox(painter, Qt::magenta, penThick); drawObjectBoxes(painter, penThick); - this->setPixmap(QPixmap::fromImage(m_inputImg)); + this->setPixmap(QPixmap::fromImage(imageOnUi)); } void label_img::loadLabelData(const QString& labelFilePath) @@ -153,22 +145,21 @@ void label_img::loadLabelData(const QString& labelFilePath) { try { ObjectLabelingBox objBox; + objBox.label = static_cast(inputFileValues.at(i)); - double midX = inputFileValues.at(i + 1) * m_inputImg.width(); - double midY = inputFileValues.at(i + 2) * m_inputImg.height(); - double width = inputFileValues.at(i + 3) * m_inputImg.width(); - double height = inputFileValues.at(i + 4) * m_inputImg.height(); + double midX = inputFileValues.at(i + 1); + double midY = inputFileValues.at(i + 2); + double width = inputFileValues.at(i + 3); + double height = inputFileValues.at(i + 4); + double leftX = midX - width/2.; + double topY = midY - height/2.; - std::cout << "midX: " << midX << std::endl; - std::cout << "midX: " << midY << std::endl; - - - objBox.box.setX(static_cast(midX - width/2 + 0.5)); - objBox.box.setY(static_cast(midY - height/2 + 0.5)); - objBox.box.setWidth(static_cast(width + 0.5)); - objBox.box.setHeight(static_cast(height + 0.5)); + objBox.box.setX(leftX); // translation: midX -> leftX + objBox.box.setY(topY); // translation: midY -> topY + objBox.box.setWidth(width); + objBox.box.setHeight(height); m_objBoundingBoxes.push_back(objBox); } @@ -189,9 +180,14 @@ void label_img::setFocusObjectName(QString qstrName) m_foucsedObjectName = qstrName; } +QImage label_img::crop(QRect rect) +{ + return m_inputImg.copy(rect); +} + void label_img::drawCrossLine(QPainter& painter, QColor color, int thickWidth) { - if(m_mouse_pos_in_image_coordinate == QPoint()) return; + if(m_relative_mouse_pos_in_ui == QPointF()) return; QPen pen; pen.setWidth(thickWidth); @@ -199,9 +195,12 @@ void label_img::drawCrossLine(QPainter& painter, QColor color, int thickWidth) pen.setColor(color); painter.setPen(pen); + QPoint absolutePoint = cvtRelativeToAbsolutePoint(m_relative_mouse_pos_in_ui); + + std::cout <<"absolutePoint.x() = "<< absolutePoint.x() << std::endl; //draw cross line - painter.drawLine(QPoint(m_mouse_pos_in_image_coordinate.x(),0), QPoint(m_mouse_pos_in_image_coordinate.x(),m_inputImg.height() - 1)); - painter.drawLine(QPoint(0,m_mouse_pos_in_image_coordinate.y()), QPoint(m_inputImg.width() - 1, m_mouse_pos_in_image_coordinate.y())); + painter.drawLine(QPoint(absolutePoint.x(),0), QPoint(absolutePoint.x(), this->height() - 1)); + painter.drawLine(QPoint(0,absolutePoint.y()), QPoint(this->width() - 1, absolutePoint.y())); std::cout << "draw Cross Line" << std::endl; } @@ -215,7 +214,13 @@ void label_img::drawFocusedObjectBox(QPainter& painter, Qt::GlobalColor color, i pen.setColor(color); painter.setPen(pen); - painter.drawRect(QRect(m_leftButtonClickedPoint, m_mouse_pos_in_image_coordinate)); + + //relative coord to absolute coord + + QPoint absolutePoint1 = cvtRelativeToAbsolutePoint(m_relatvie_mouse_pos_LBtnClicked_in_ui); + QPoint absolutePoint2 = cvtRelativeToAbsolutePoint(m_relative_mouse_pos_in_ui); + + painter.drawRect(QRect(absolutePoint1, absolutePoint2)); } } @@ -229,18 +234,18 @@ void label_img::drawObjectBoxes(QPainter& painter, int thickWidth) pen.setColor(m_drawObjectBoxColor.at(boundingbox.label)); painter.setPen(pen); - painter.drawRect(boundingbox.box); + painter.drawRect(cvtRelativeToAbsoluteRectInUi(boundingbox.box)); } } -void label_img::removeFocusedObjectBox(QPoint point) +void label_img::removeFocusedObjectBox(QPointF point) { int removeBoxIdx = -1; double nearestBoxDistance = 99999999999999.; for(int i = 0; i < m_objBoundingBoxes.size(); i++) { - QRect objBox = m_objBoundingBoxes.at(i).box; + QRectF objBox = m_objBoundingBoxes.at(i).box; if(objBox.contains(point)) { @@ -259,7 +264,7 @@ void label_img::removeFocusedObjectBox(QPoint point) } } -QRect label_img::getRectFromTwoPoints(QPoint p1, QPoint p2) +QRect label_img::getAbsoluteRectFromTwoPoints(QPoint p1, QPoint p2) { int midX = (p1.x() + p2.x()) / 2; int midY = (p1.y() + p2.y()) / 2; @@ -271,3 +276,42 @@ QRect label_img::getRectFromTwoPoints(QPoint p1, QPoint p2) return QRect(topLeftPoint, bottomRightPoint); } + +QRectF label_img::getRelativeRectFromTwoPoints(QPointF p1, QPointF p2) +{ + double midX = (p1.x() + p2.x()) / 2.; + double midY = (p1.y() + p2.y()) / 2.; + double width = fabs(p1.x() - p2.x()); + double height = fabs(p1.y() - p2.y()); + + QPointF topLeftPoint(midX - width/2., midY - height/2.); + QPointF bottomRightPoint(midX + width/2., midY + height/2.); + + return QRectF(topLeftPoint, bottomRightPoint); +} + +QRect label_img::cvtRelativeToAbsoluteRectInUi(QRectF rectF) +{ + return QRect(static_cast(rectF.x() * this->width() + 0.5), + static_cast(rectF.y() * this->height()+ 0.5), + static_cast(rectF.width() * this->width()+ 0.5), + static_cast(rectF.height()* this->height()+ 0.5)); +} + +QRect label_img::cvtRelativeToAbsoluteRectInImage(QRectF rectF) +{ + return QRect(static_cast(rectF.x() * m_inputImg.width()), + static_cast(rectF.y() * m_inputImg.height()), + static_cast(rectF.width() * m_inputImg.width()), + static_cast(rectF.height()* m_inputImg.height())); +} + +QPoint label_img::cvtRelativeToAbsolutePoint(QPointF p) +{ + return QPoint(static_cast(p.x() * this->width() + 0.5), static_cast(p.y() * this->height() + 0.5)); +} + +QPointF label_img::cvtAbsoluteToRelativePoint(QPoint p) +{ + return QPointF(static_cast(p.x()) / this->width(), static_cast(p.y()) / this->height()); +} diff --git a/label_img.h b/label_img.h index 8249250..a587617 100644 --- a/label_img.h +++ b/label_img.h @@ -11,7 +11,7 @@ struct ObjectLabelingBox { int label; - QRect box; + QRectF box; }; class label_img : public QLabel @@ -24,11 +24,6 @@ public: void mousePressEvent(QMouseEvent *ev); void mouseReleaseEvent(QMouseEvent *ev); - QImage m_inputImg; - QImage m_inputImgCopy;//for drawing - - QPoint m_mouse_pos_in_image_coordinate; - QVector m_drawObjectBoxColor; int m_uiX; @@ -51,6 +46,17 @@ public: void setFocusObjectLabel(int); void setFocusObjectName(QString); + QImage crop(QRect); + + QRect getAbsoluteRectFromTwoPoints(QPoint , QPoint); + QRectF getRelativeRectFromTwoPoints(QPointF , QPointF); + + QRect cvtRelativeToAbsoluteRectInUi(QRectF); + QRect cvtRelativeToAbsoluteRectInImage(QRectF); + + QPoint cvtRelativeToAbsolutePoint(QPointF); + QPointF cvtAbsoluteToRelativePoint(QPoint); + signals: void Mouse_Moved(); void Mouse_Pressed(); @@ -63,7 +69,10 @@ private: double m_aspectRatioWidth; double m_aspectRatioHeight; - QPoint m_leftButtonClickedPoint; + QImage m_inputImg; + + QPointF m_relative_mouse_pos_in_ui; + QPointF m_relatvie_mouse_pos_LBtnClicked_in_ui; void setMousePosition(int , int); @@ -71,11 +80,7 @@ private: void drawFocusedObjectBox(QPainter& , Qt::GlobalColor , int thickWidth = 3); void drawObjectBoxes(QPainter& , int thickWidth = 3); - void removeFocusedObjectBox(QPoint); - - QRect getRectFromTwoPoints(QPoint , QPoint); - - + void removeFocusedObjectBox(QPointF); }; #endif // LABEL_IMG_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 760c08c..abde9b8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -169,19 +169,22 @@ void MainWindow::save_label_data()const if(ui->checkBox_cropping->isChecked()) { - QImage cropped = ui->label_image->m_inputImgCopy.copy(objBox.box); + QImage cropped = ui->label_image->crop(ui->label_image->cvtRelativeToAbsoluteRectInImage(objBox.box)); - string strImgFile = m_fileList.at(m_fileIndex).toStdString(); - strImgFile = strImgFile.substr( strImgFile.find_last_of('/') + 1, - strImgFile.find_last_of('.') - strImgFile.find_last_of('/') - 1); + if(!cropped.isNull()) + { + string strImgFile = m_fileList.at(m_fileIndex).toStdString(); + strImgFile = strImgFile.substr( strImgFile.find_last_of('/') + 1, + strImgFile.find_last_of('.') - strImgFile.find_last_of('/') - 1); - cropped.save(QString().fromStdString(strImgFile) + "_cropped_" + QString::number(i) + ".png"); + cropped.save(QString().fromStdString(strImgFile) + "_cropped_" + QString::number(i) + ".png"); + } } - double midX = static_cast(objBox.box.x() + objBox.box.width() / 2.) / ui->label_image->m_inputImg.width(); - double midY = static_cast(objBox.box.y() + objBox.box.height() / 2.) / ui->label_image->m_inputImg.height(); - double width = static_cast(objBox.box.width()) / ui->label_image->m_inputImg.width(); - double height = static_cast(objBox.box.height()) / ui->label_image->m_inputImg.height(); + double midX = objBox.box.x() + objBox.box.width() / 2.; + double midY = objBox.box.y() + objBox.box.height() / 2.; + double width = objBox.box.width(); + double height = objBox.box.height(); fileOutputLabelData << objBox.label; fileOutputLabelData << " "; @@ -227,6 +230,7 @@ void MainWindow::load_label_list_data(QString qstrLabelListFile) ui->tableWidget_label->removeRow(ui->tableWidget_label->currentRow()); m_labelNameList.clear(); + ui->label_image->m_drawObjectBoxColor.clear(); string strLabel;