Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zoom In/Out feature #66

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions label_img.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ 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)
.convertToFormat(QImage::Format_RGB888);
if(m_resized_inputImg.width() != this->width() || m_resized_inputImg.height() != this->height()){
m_resized_inputImg = m_inputImg.scaled(this->width(), this->height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation).convertToFormat(QImage::Format_RGB888);
}

QImage img = m_resized_inputImg;

gammaTransform(img);
zoomImage(img);

QPainter painter(&img);
QFont font = painter.font();
Expand Down Expand Up @@ -274,6 +273,21 @@ void label_img::drawFocusedObjectBox(QPainter& painter, Qt::GlobalColor color, i
}
}

// void label_img::drawObjectBoxes(QPainter& painter, int thickWidth)
// {
// QPen pen;
// pen.setWidth(thickWidth);

// for(ObjectLabelingBox boundingbox: m_objBoundingBoxes)
// {
// pen.setColor(m_drawObjectBoxColor.at(boundingbox.label));
// painter.setPen(pen);

// painter.drawRect(cvtRelativeToAbsoluteRectInUi(boundingbox.box));
// }
// }

// zoomed labels location-wise for visualize zoomed label location
void label_img::drawObjectBoxes(QPainter& painter, int thickWidth)
{
QPen pen;
Expand All @@ -284,10 +298,17 @@ void label_img::drawObjectBoxes(QPainter& painter, int thickWidth)
pen.setColor(m_drawObjectBoxColor.at(boundingbox.label));
painter.setPen(pen);

painter.drawRect(cvtRelativeToAbsoluteRectInUi(boundingbox.box));
QRectF relativeBox = boundingbox.box;
QRectF absoluteBox = cvtRelativeToAbsoluteRectInUi(relativeBox);

// Adjust box coordinates based on zoom factor
QRectF zoomedBox = zoomRect(absoluteBox);

painter.drawRect(zoomedBox);
}
}


void label_img::drawObjectLabels(QPainter& painter, int thickWidth, int fontPixelSize, int xMargin, int yMargin)
{
QFontMetrics fontMetrics = painter.fontMetrics();
Expand Down Expand Up @@ -418,3 +439,49 @@ void label_img::setContrastGamma(float gamma)
}
showImage();
}

void label_img::setZoomFactor(double factor)
{
if (factor <= 0) return;
m_zoomFactor = factor;
showImage();
}

void label_img::zoomImage(QImage &image)
{
int zoomWidth = static_cast<int>(image.width() / m_zoomFactor);
int zoomHeight = static_cast<int>(image.height() / m_zoomFactor);

int mouseX = m_relative_mouse_pos_in_ui.x() * image.width();
int mouseY = m_relative_mouse_pos_in_ui.y() * image.height();

int startX = std::max(0, mouseX - zoomWidth);
int startY = std::max(0, mouseY - zoomHeight);

int endX = std::min(image.width(), startX + zoomWidth);
int endY = std::min(image.height(), startY + zoomHeight);

QImage zoomedRegion = image.copy(startX, startY, endX - startX, endY - startY);

QImage scaledZoomedRegion = zoomedRegion.scaled(this->size(), Qt::KeepAspectRatio);

image.fill(Qt::white);
QPainter painter(&image);
painter.drawImage(0, 0, scaledZoomedRegion);
}

QRectF label_img::zoomRect(const QRectF& rect)
{
double zoomedX = rect.x() * m_zoomFactor;
double zoomedY = rect.y() * m_zoomFactor;

double zoomedWidth = rect.width() * m_zoomFactor;
double zoomedHeight = rect.height() * m_zoomFactor;

// Ensure the zoomed box stays within image boundaries
zoomedX = std::max(0.0, std::min(zoomedX, static_cast<double>(m_resized_inputImg.width() - zoomedWidth)));
zoomedY = std::max(0.0, std::min(zoomedY, static_cast<double>(m_resized_inputImg.height() - zoomedHeight)));

return QRectF(zoomedX, zoomedY, zoomedWidth, zoomedHeight);
}

8 changes: 8 additions & 0 deletions label_img.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ class label_img : public QLabel
void setFocusObjectLabel(int);
void setFocusObjectName(QString);
void setContrastGamma(float);

void setZoomFactor(double factor);

bool isOpened();
QImage crop(QRect);

QRectF getRelativeRectFromTwoPoints(QPointF , QPointF);
QRectF zoomRect(const QRectF& rect);

QRect cvtRelativeToAbsoluteRectInUi(QRectF);
QRect cvtRelativeToAbsoluteRectInImage(QRectF);
Expand All @@ -73,6 +76,8 @@ class label_img : public QLabel

double m_aspectRatioWidth;
double m_aspectRatioHeight;

double m_zoomFactor = 1.0;

QImage m_inputImg;
QImage m_resized_inputImg;
Expand All @@ -90,6 +95,9 @@ class label_img : public QLabel
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 zoomImage(QImage &image);

void removeFocusedObjectBox(QPointF);
};

Expand Down
16 changes: 16 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ void MainWindow::init_horizontal_slider()
ui->horizontalSlider_contrast->setValue(ui->horizontalSlider_contrast->maximum()/2);
ui->label_image->setContrastGamma(1.0);
ui->label_contrast->setText(QString("Contrast(%) ") + QString::number(50));

ui->horizontalSlider_zoomInout->setEnabled(true);
ui->horizontalSlider_zoomInout->setRange(0, 100);
ui->label_image->setZoomFactor(0);
ui->horizontalSlider_zoomInout->setValue(0);
ui->label_zoom->setText(QString("Zoom(%) ") + QString::number(0));
}

void MainWindow::init_table_widget()
Expand All @@ -476,6 +482,16 @@ void MainWindow::on_horizontalSlider_contrast_sliderMoved(int value)
ui->label_contrast->setText(QString("Contrast(%) ") + QString::number(int(valueToPercentage * 100.)));
}

void MainWindow::on_horizontalSlider_zoomInout_sliderMoved(int value)
{
double zoomFactor = 1.0 + (static_cast<double>(value) / 100.0);

ui->label_image->setZoomFactor(zoomFactor);

int zoomPercentage = static_cast<int>((zoomFactor - 1.0) * 100.0);
ui->label_zoom->setText(QString("Zoom(%) ") + QString::number(zoomPercentage));
}

void MainWindow::on_checkBox_visualize_class_name_clicked(bool checked)
{
ui->label_image->m_bVisualizeClassName = checked;
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private slots:

void on_horizontalSlider_contrast_sliderMoved(int value);

void on_horizontalSlider_zoomInout_sliderMoved(int zoomValue);

void on_checkBox_visualize_class_name_clicked(bool checked);

private:
Expand Down
126 changes: 115 additions & 11 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
<font>
<family>Arial</family>
<pointsize>18</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -124,7 +123,114 @@ border-color: rgb(0, 255, 255);}
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3"/>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QSlider" name="horizontalSlider_zoomInout">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>560</width>
<height>22</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>42</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="styleSheet">
<string notr="true">
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;
}
</string>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="pageStep">
<number>0</number>
</property>
<property name="tracking">
<bool>true</bool>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_zoom">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>23</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>42</height>
</size>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color : rgb(0, 0, 17);color : rgb(0, 255, 255);
border-style: outset;
border-width: 2px;
border-color: rgb(0, 255, 255);</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="text">
<string>Zoom In/Out</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
Expand Down Expand Up @@ -211,7 +317,6 @@ QSlider::handle:horizontal {
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -322,7 +427,6 @@ QSlider::handle:horizontal {
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -380,7 +484,6 @@ border-color: rgb(0, 255, 255);</string>
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
<kerning>true</kerning>
</font>
Expand Down Expand Up @@ -432,7 +535,6 @@ border-color: rgb(0, 255, 255);</string>
<font>
<family>Arial</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand All @@ -453,10 +555,13 @@ border-color: rgb(0, 255, 255);</string>
</property>
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Arial'; font-size:12pt; font-weight:600; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'Gulim'; font-size:9pt;&quot;&gt;Last Labeled Image:&lt;br /&gt;Current Image:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Arial'; font-size:12pt; font-weight:700; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'Gulim'; font-size:9pt; font-weight:600;&quot;&gt;Last Labeled Image:&lt;br /&gt;Current Image:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -487,7 +592,6 @@ p, li { white-space: pre-wrap; }
<property name="font">
<font>
<pointsize>10</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -613,7 +717,7 @@ QTableView {
<x>0</x>
<y>0</y>
<width>1180</width>
<height>21</height>
<height>25</height>
</rect>
</property>
</widget>
Expand Down