From 748109d66194a815856d691a0395297cf8c8d9b4 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Tue, 30 Nov 2021 00:04:33 +0900 Subject: [PATCH] all new yolo label --- label_img.cpp | 51 +++- label_img.h | 9 +- mainwindow.cpp | 97 +++---- mainwindow.h | 12 +- mainwindow.ui | 708 ++++++++++++++++++------------------------------- 5 files changed, 367 insertions(+), 510 deletions(-) diff --git a/label_img.cpp b/label_img.cpp index ed96a3d..e968c22 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -2,6 +2,8 @@ #include #include #include /* fabs */ +#include +//#include using std::ifstream; @@ -123,6 +125,7 @@ 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_bLabelingStarted = false; @@ -143,10 +146,16 @@ void label_img::openImage(const QString &qstrImg, bool &ret) 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); + } - QImage imageOnUi = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation); + QImage img = m_resized_inputImg; - QPainter painter(&imageOnUi); + gammaTransform(img); + + QPainter painter(&img); int penThick = 3; @@ -156,7 +165,7 @@ void label_img::showImage() drawFocusedObjectBox(painter, Qt::magenta, penThick); drawObjectBoxes(painter, penThick); - this->setPixmap(QPixmap::fromImage(imageOnUi)); + this->setPixmap(QPixmap::fromImage(img)); } void label_img::loadLabelData(const QString& labelFilePath) @@ -270,6 +279,31 @@ void label_img::drawObjectBoxes(QPainter& painter, int thickWidth) } } +void label_img::gammaTransform(QImage &image) +{ + uchar* bits = image.bits(); + + int h = image.height(); + int w = image.width(); + + //#pragma omp parallel for collapse(2) + for(int y = 0 ; y < h; ++y) + { + for(int x = 0; x < w; ++x) + { + int index_pixel = (y*w+x)*3; + + unsigned char r = bits[index_pixel + 0]; + unsigned char g = bits[index_pixel + 1]; + unsigned char b = bits[index_pixel + 2]; + + bits[index_pixel + 0] = m_gammatransform_lut[r]; + bits[index_pixel + 1] = m_gammatransform_lut[g]; + bits[index_pixel + 2] = m_gammatransform_lut[b]; + } + } +} + void label_img::removeFocusedObjectBox(QPointF point) { int removeBoxIdx = -1; @@ -334,3 +368,14 @@ QPointF label_img::cvtAbsoluteToRelativePoint(QPoint p) { return QPointF(static_cast(p.x()) / this->width(), static_cast(p.y()) / this->height()); } + +void label_img::setContrastGamma(float gamma) +{ + for(int i=0; i < 256; i++) + { + int s = (int)(pow((float)i/255., gamma) * 255.); + s = std::clamp(s, 0, 255); + m_gammatransform_lut[i] = (unsigned char)s; + } + showImage(); +} diff --git a/label_img.h b/label_img.h index 34b5c91..1badeda 100644 --- a/label_img.h +++ b/label_img.h @@ -46,7 +46,7 @@ public: void setFocusObjectLabel(int); void setFocusObjectName(QString); - + void setContrastGamma(float); bool isOpened(); QImage crop(QRect); @@ -59,6 +59,7 @@ public: QPoint cvtRelativeToAbsolutePoint(QPointF); QPointF cvtAbsoluteToRelativePoint(QPoint); + signals: void Mouse_Moved(); void Mouse_Pressed(); @@ -72,16 +73,20 @@ private: double m_aspectRatioHeight; QImage m_inputImg; + QImage m_resized_inputImg; QPointF m_relative_mouse_pos_in_ui; QPointF m_relatvie_mouse_pos_LBtnClicked_in_ui; + unsigned char m_gammatransform_lut[256]; + QVector colorTable; + void setMousePosition(int , int); void drawCrossLine(QPainter& , QColor , int thickWidth = 3); void drawFocusedObjectBox(QPainter& , Qt::GlobalColor , int thickWidth = 3); void drawObjectBoxes(QPainter& , int thickWidth = 3); - + void gammaTransform(QImage& image); void removeFocusedObjectBox(QPointF); }; diff --git a/mainwindow.cpp b/mainwindow.cpp index fd43e8e..4e1e2c6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -43,33 +43,36 @@ MainWindow::~MainWindow() void MainWindow::on_pushButton_open_files_clicked() { bool bRetImgDir = false; - bool bRetObjFile = false; - open_img_dir(bRetImgDir); if (!bRetImgDir) return ; - open_obj_file(bRetObjFile); - - if (!bRetObjFile) return ; + if (m_objList.empty()) + { + bool bRetObjFile = false; + open_obj_file(bRetObjFile); + if (!bRetObjFile) return ; + } init(); } -void MainWindow::on_pushButton_change_dir_clicked() -{ - bool bRetImgDir = false; +//void MainWindow::on_pushButton_change_dir_clicked() +//{ +// bool bRetImgDir = false; - open_img_dir(bRetImgDir); +// open_img_dir(bRetImgDir); - if (!bRetImgDir) return ; +// if (!bRetImgDir) return ; - init(); -} +// init(); +//} void MainWindow::init() { + m_lastLabeledImgIndex = -1; + ui->label_image->init(); init_button_event(); @@ -89,7 +92,17 @@ void MainWindow::set_label_progress(const int fileIndex) void MainWindow::set_focused_file(const int fileIndex) { - ui->label_file->setText("File: " + m_imgList.at(fileIndex)); + QString str = ""; + + if(m_lastLabeledImgIndex >= 0) + { + str += "Last Labeled Image: " + m_imgList.at(m_lastLabeledImgIndex); + str += '\n'; + } + + str += "Current Image: " + m_imgList.at(fileIndex); + + ui->textEdit_log->setText(str); } void MainWindow::goto_img(const int fileIndex) @@ -126,7 +139,7 @@ void MainWindow::prev_img(bool bSavePrev) goto_img(m_imgIndex - 1); } -void MainWindow::save_label_data()const +void MainWindow::save_label_data() { if(m_imgList.size() == 0) return; @@ -139,21 +152,6 @@ void MainWindow::save_label_data()const { ObjectLabelingBox objBox = ui->label_image->m_objBoundingBoxes[i]; - if(ui->checkBox_cropping->isChecked()) - { - QImage cropped = ui->label_image->crop(ui->label_image->cvtRelativeToAbsoluteRectInImage(objBox.box)); - - if(!cropped.isNull()) - { - string strImgFile = m_imgList.at(m_imgIndex).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"); - } - } - double midX = objBox.box.x() + objBox.box.width() / 2.; double midY = objBox.box.y() + objBox.box.height() / 2.; double width = objBox.box.width(); @@ -169,10 +167,8 @@ void MainWindow::save_label_data()const fileOutputLabelData << " "; fileOutputLabelData << std::fixed << std::setprecision(6) << height << std::endl; } - + m_lastLabeledImgIndex = m_imgIndex; fileOutputLabelData.close(); - - ui->textEdit_log->setText(qstrOutputLabelData + " saved."); } } @@ -408,15 +404,15 @@ void MainWindow::keyPressEvent(QKeyEvent * event) } } -void MainWindow::on_pushButton_save_clicked() -{ - save_label_data(); -} +//void MainWindow::on_pushButton_save_clicked() +//{ +// save_label_data(); +//} -void MainWindow::on_pushButton_remove_clicked() -{ - remove_img(); -} +//void MainWindow::on_pushButton_remove_clicked() +//{ +// remove_img(); +//} void MainWindow::on_tableWidget_label_cellDoubleClicked(int row, int column) { @@ -447,11 +443,7 @@ void MainWindow::on_horizontalSlider_images_sliderMoved(int position) void MainWindow::init_button_event() { - ui->pushButton_change_dir->setEnabled(true); - ui->pushButton_next->setEnabled(true); - ui->pushButton_prev->setEnabled(true); - ui->pushButton_save->setEnabled(true); - ui->pushButton_remove->setEnabled(true); +// ui->pushButton_change_dir->setEnabled(true); } void MainWindow::init_horizontal_slider() @@ -461,6 +453,12 @@ void MainWindow::init_horizontal_slider() ui->horizontalSlider_images->blockSignals(true); ui->horizontalSlider_images->setValue(0); ui->horizontalSlider_images->blockSignals(false); + + ui->horizontalSlider_contrast->setEnabled(true); + ui->horizontalSlider_contrast->setRange(0, 1000); + ui->horizontalSlider_contrast->setValue(ui->horizontalSlider_contrast->maximum()/2); + ui->label_image->setContrastGamma(1.0); + ui->label_contrast->setText(QString("Contrast(%) ") + QString::number(50)); } void MainWindow::init_table_widget() @@ -471,3 +469,12 @@ void MainWindow::init_table_widget() disconnect(ui->tableWidget_label->horizontalHeader(), SIGNAL(sectionPressed(int)),ui->tableWidget_label, SLOT(selectColumn(int))); } + +void MainWindow::on_horizontalSlider_contrast_sliderMoved(int value) +{ + float valueToPercentage = float(value)/ui->horizontalSlider_contrast->maximum(); //[0, 1.0] + float percentageToGamma = pow(1/(valueToPercentage + 0.5), 7.); + + ui->label_image->setContrastGamma(percentageToGamma); + ui->label_contrast->setText(QString("Contrast(%) ") + QString::number(int(valueToPercentage * 100.))); +} diff --git a/mainwindow.h b/mainwindow.h index 4124c21..9312377 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -23,9 +23,9 @@ 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_change_dir_clicked(); +// void on_pushButton_save_clicked(); +// void on_pushButton_remove_clicked(); void on_pushButton_prev_clicked(); void on_pushButton_next_clicked(); @@ -34,7 +34,7 @@ private slots: void next_img(bool bSavePrev = true); void prev_img(bool bSavePrev = true); - void save_label_data() const; + void save_label_data(); void clear_label_data(); void remove_img(); @@ -46,6 +46,8 @@ private slots: void on_horizontalSlider_images_sliderMoved(int ); + void on_horizontalSlider_contrast_sliderMoved(int value); + private: void init(); void init_table_widget(); @@ -79,6 +81,8 @@ private: QStringList m_objList; int m_objIndex; + int m_lastDeletedImgIndex; + int m_lastLabeledImgIndex; protected: void wheelEvent(QWheelEvent *); diff --git a/mainwindow.ui b/mainwindow.ui index 841610a..b1b676f 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,20 +6,20 @@ 0 0 - 1528 - 953 + 1180 + 693 - + 0 0 - 1400 - 920 + 1180 + 640 @@ -39,206 +39,205 @@ - + 0 0 - 1363 - 867 + 1180 + 610 - 16777215 - 16777215 + 165555 + 165555 - - - + + + - - - - 1275 - 20 - - - - - 16777215 - 20 - - - - - 13 - 75 - true - - - - color : rgb(255, 187, 0); - - - File: - - - - - - - - 0 - 0 - - - - - 1280 - 720 - - - - - 18 - 75 - true - - - - CrossCursor - - - true - - - QLabel { background-color : rgb(0, 0, 17); color : rgb(187, 255, 254); + + + + + + 0 + 0 + + + + + 540 + 360 + + + + + 18 + 75 + true + + + + CrossCursor + + + true + + + QLabel { background-color : rgb(0, 0, 17); color : rgb(187, 255, 254); border-style: outset; border-width: 2px; border-color: rgb(0, 255, 255);} - - - QFrame::StyledPanel - - - QFrame::Plain - - - 3 - - - Open Data Set Directory... - - - true - - - Qt::AlignCenter - - - -1 - - - label_image - - - - - + + + QFrame::StyledPanel + + + QFrame::Plain + + + 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> + + + true + + + Qt::AlignCenter + + + -1 + + + label_image + + + + + + + + + + + + false + + + + 1 + 0 + + + + + 560 + 22 + + + + + 16777215 + 42 + + + + Qt::NoFocus + + + +QSlider::groove:horizontal { + border: 1px solid #999999; + height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */ + background: rgb(0, 255, 255); + margin: 2px 0; +} + + +QSlider::handle:horizontal { + background: rgb(255, 187, 0); + border: 1px solid #5c5c5c; + width: 18px; + margin: -10px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */ + border-radius: 3px; +} + + + + 0 + + + 0 + + + true + + + Qt::Horizontal + + + + + + + + 0 + 0 + + + + + 160 + 23 + + + + + 16777215 + 42 + + + + + Arial + 12 + 75 + true + + + + background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); +border-style: outset; +border-width: 2px; +border-color: rgb(0, 255, 255); + + + QFrame::StyledPanel + + + 2 + + + Contrast + + + Qt::AlignCenter + + + + + - - - - false - - - - 75 - 23 - - - - - 75 - true - - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - Prev - - - - - - false - - - false - - - false - - - - - - - false - - - - 75 - 23 - - - - - 75 - true - - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - Next - - - Del - - - false - - - false - - - false - - - @@ -252,12 +251,18 @@ border-color: rgb(0, 255, 255); - 881 + 560 22 + + + 16777215 + 42 + + - Qt::ClickFocus + Qt::NoFocus @@ -302,12 +307,20 @@ QSlider::handle:horizontal { - 211 - 21 + 160 + 23 + + + + + 16777215 + 42 + Arial + 12 75 true @@ -335,23 +348,37 @@ border-color: rgb(0, 255, 255); - + + + + 0 + 0 + + - 200 - 91 + 160 + 25 - 16777215 - 91 + 200 + 60 + + + + + 0 + 0 + Arial + 12 75 true true @@ -378,112 +405,29 @@ border-color: rgb(0, 255, 255); - - - false + + + + 0 + 0 + - 200 - 91 + 560 + 25 16777215 - 91 - - - - - 75 - true - true - - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - Change Directory - - - false - - - false - - - - - - - false - - - - 200 - 91 - - - - - 16777215 - 91 - - - - - 75 - true - true - - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - Save - - - - - - false - - - false - - - false - - - - - - - false - - - - 200 - 91 + 60 + Arial + 12 75 true @@ -497,177 +441,29 @@ border-style: outset; border-width: 2px; border-color: rgb(0, 255, 255); - - Remove + + false + + + true + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Arial'; font-size:12pt; font-weight:600; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Gulim'; font-size:9pt;">Last Labeled Image:<br />Current Image:</span></p></body></html> - - - - - 100 - 21 - - - - - 100 - 16777215 - - - - - 75 - true - - - - ArrowCursor - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - with crop - - - - - - - - - true - - - - 0 - 0 - - - - - 41 - 15 - - - - - 16777215 - 15 - - - - - 75 - false - true - - - - color : rgb(0, 255, 255); - - - Log - - - - - - - - 0 - 0 - - - - - 545 - 70 - - - - - 16777215 - 70 - - - - - 75 - true - - - - Qt::NoFocus - - - background-color : rgb(0, 0, 17);color : rgb(0, 255, 255); -border-style: outset; -border-width: 2px; -border-color: rgb(0, 255, 255); - - - false - - - true - - - - - - - - - - - - - - 0 - 0 - - - - - 220 - 20 - - - - - 220 - 20 - - - - - 13 - 75 - true - - - - - - - - + 0 0 @@ -675,12 +471,12 @@ border-color: rgb(0, 255, 255); 220 - 851 + 360 - 220 + 330 16777215 @@ -744,12 +540,12 @@ QTableView { false - - false - 40 + + false + true @@ -786,8 +582,8 @@ QTableView { 0 0 - 1528 - 23 + 1180 + 21