-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagelabel.cpp
132 lines (81 loc) · 2.87 KB
/
imagelabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "imagelabel.h"
#include "ui_imagelabel.h"
ImageLabel::ImageLabel(QWidget *parent) :
QLabel(parent),
ui(new Ui::imageLabel)
{
ui->setupUi(this);
this->setAlignment(Qt::AlignTop);
}
ImageLabel::~ImageLabel()
{
delete ui;
}
void ImageLabel::mousePressEvent(QMouseEvent *e){
if(m_enabledClick && e->button() == Qt::LeftButton){
if(e->pos().x() < m_backgroundImage.cols*m_scaleFactor && e->pos().y() < m_backgroundImage.rows*m_scaleFactor){
m_imagePoint.x = (e->pos().x()/m_scaleFactor);
m_imagePoint.y = (e->pos().y()/m_scaleFactor);
emit signalImagePoint();
}
}else if(e->button() == Qt::RightButton){
emit signalClearImagePoint();
}
}
void ImageLabel::loadBackgroundImage(std::string path){
m_backgroundImage = cv::imread(path);
if(m_backgroundImage.empty()){
return;
}
setBackgroundImage(m_backgroundImage);
}
void ImageLabel::changeDefaultBackgroundImage(cv::Mat inputImage){
m_backgroundImage = inputImage.clone();
if(m_backgroundImage.empty()){
return;
}
setBackgroundImage(m_backgroundImage);
}
void ImageLabel::setBackgroundImage(cv::Mat inputImage){
cv::Mat image = inputImage.clone();
if(image.empty()){
return;
}
cv::cvtColor(image,image,cv::COLOR_BGR2RGB);
m_newPixmap = QPixmap::fromImage(QImage(image.data, image.cols, image.rows, image.step, QImage::Format_RGB888));
this->setPixmap(m_newPixmap.scaled(this->size(),Qt::KeepAspectRatio, Qt::SmoothTransformation));
this->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
m_aspectRationImage = static_cast<double>(image.cols)/image.rows;
m_aspectRationLabel = static_cast<double>(this->size().width())/this->size().height();
if(m_aspectRationImage < m_aspectRationLabel){
m_scaleFactor = static_cast<double>(this->size().height())/image.rows;
}else if(m_aspectRationImage > m_aspectRationLabel)
{
m_scaleFactor = static_cast<double>(this->size().width())/image.cols;
}else{m_scaleFactor = 1.0;}
}
void ImageLabel::activateImageClick(){
m_enabledClick = true;
}
void ImageLabel::resetImagePoint(){
m_imagePoint = cv::Point2f();
}
cv::Point2f ImageLabel::getImagePoint(){
return m_imagePoint;
}
void ImageLabel::deactivateImageClick(){
m_enabledClick = false;
}
cv::Mat ImageLabel::getBackgroundImage(){
return m_backgroundImage;
}
void ImageLabel::displayPoints(std::vector<cv::Point2f> pairedImagePoints){
cv::Mat temp = m_backgroundImage.clone();
cv::Point2i tempPoint;
for(uint x = 0; x < pairedImagePoints.size(); x++ ){
tempPoint.x = std::round(pairedImagePoints[x].x);
tempPoint.y = std::round(pairedImagePoints[x].y);
cv::circle(temp,tempPoint,5,{255,0,0},-1,cv::LINE_AA);
}
setBackgroundImage(temp);
}