Skip to content
Snippets Groups Projects
Commit e371e2ea authored by simon.fanetti's avatar simon.fanetti
Browse files

add edit mask on UI

parent fafe110c
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ CroppingMask::CroppingMask(SandboxSetup *sandbox, QWidget *parent) : ...@@ -10,6 +10,7 @@ CroppingMask::CroppingMask(SandboxSetup *sandbox, QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
maskEdit = new MaskEdit(ui->frame->geometry()); maskEdit = new MaskEdit(ui->frame->geometry());
ui->vLayout->addWidget(maskEdit, 1); ui->vLayout->addWidget(maskEdit, 1);
// TODO : try to load cropping mask from file // TODO : try to load cropping mask from file
} }
......
...@@ -5,7 +5,9 @@ MaskEdit::MaskEdit(const QRect geo, QWidget *parent) : ...@@ -5,7 +5,9 @@ MaskEdit::MaskEdit(const QRect geo, QWidget *parent) :
QFrame(parent), QFrame(parent),
ui(new Ui::MaskEdit) ui(new Ui::MaskEdit)
{ {
this->installEventFilter(this);
ui->setupUi(this); ui->setupUi(this);
// set size like the parent widget's size
setGeometry(0,0, geo.width(), geo.height()); setGeometry(0,0, geo.width(), geo.height());
ui->lblFrame->setGeometry(0,0, width(), height()); ui->lblFrame->setGeometry(0,0, width(), height());
} }
...@@ -19,21 +21,22 @@ MaskEdit::~MaskEdit() ...@@ -19,21 +21,22 @@ MaskEdit::~MaskEdit()
void MaskEdit::updateFrame(cv::Mat *frame, std::vector<cv::Point> *points){ void MaskEdit::updateFrame(cv::Mat *frame, std::vector<cv::Point> *points){
capture = frame; capture = frame;
rectPoints = points; rectPoints = points;
if(frame != nullptr && points != nullptr){
ratioX = (float)capture->size().width / ui->lblFrame->width();
ratioY = (float)capture->size().height / ui->lblFrame->height();
}
update(); update();
} }
void MaskEdit::updateFrame(cv::Mat *frame){ void MaskEdit::updateFrame(cv::Mat *frame){
capture = frame; updateFrame(frame, rectPoints);
update();
} }
void MaskEdit::updateFrame(std::vector<cv::Point> *points){ void MaskEdit::updateFrame(std::vector<cv::Point> *points){
rectPoints = points; updateFrame(capture, points);
update();
} }
void MaskEdit::paintEvent(QPaintEvent *){ void MaskEdit::paintEvent(QPaintEvent *){
if(capture != nullptr){ if(capture != nullptr){
...@@ -64,16 +67,77 @@ void MaskEdit::paintEvent(QPaintEvent *){ ...@@ -64,16 +67,77 @@ void MaskEdit::paintEvent(QPaintEvent *){
} }
} }
/*
void CroppingMask::scaleMaskWindowToFrame(){
float xCoeff = cameraRGBFrame.size().width / ui->vLayout->width(); bool MaskEdit::eventFilter(QObject *object, QEvent *ev)
float yCoeff = cameraRGBFrame.size().height / ui->vLayout->height(); {
if (ev->type() == QEvent::MouseButtonPress){
selectedCornerIndex = selectCorner((QMouseEvent*)ev);
}
else if (ev->type() == QEvent::MouseButtonRelease){
selectedCornerIndex = -1;
}
else if (ev->type() == QEvent::MouseMove){
QMouseEvent* eMouse = (QMouseEvent*)ev;
if (eMouse->type() == QMouseEvent::MouseMove){
UpdateSelectedCornerPosition(eMouse);
}
}
(void)object; // var unused
return false;
}
for(uint i=0; i<rectPoints.size(); i++){ int MaskEdit::selectCorner(QMouseEvent* eMouse){
rectPoints[i].x = rectPoints[i].x * xCoeff;
rectPoints[i].y = rectPoints[i].y * yCoeff; if(capture != nullptr && rectPoints != nullptr){
int margin = 20;
// x and y relative to the captured frame, not the UI
int mouseX = (int)( (float)eMouse->x() * ratioX );
int mouseY = (int)( (float)eMouse->y() * ratioY );
int marginX = (int)( margin * ratioX );
int marginY = (int)( margin * ratioY );
for(uint i=0; i<rectPoints->size(); i++){
int x = rectPoints->at(i).x;
int y = rectPoints->at(i).y;
if( (x-marginX <= mouseX && mouseX <= x+marginX) &&
(y-marginY <= mouseY && mouseY <= y+marginY) )
{
return (int)i;
}
}
} }
return -1;
} }
*/
void MaskEdit::UpdateSelectedCornerPosition(QMouseEvent* eMouse){
if(capture != nullptr && rectPoints != nullptr){
if(selectedCornerIndex != -1){
if( (0 <= eMouse->x() && eMouse->x() < ui->lblFrame->width()) &&
(0 <= eMouse->y() && eMouse->y() < ui->lblFrame->height()) )
{
int mouseX = (int)( (float)eMouse->x() * ratioX );
int mouseY = (int)( (float)eMouse->y() * ratioY );
rectPoints->at(selectedCornerIndex).x = mouseX;
rectPoints->at(selectedCornerIndex).y = mouseY;
update();
}
}
}
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <QPen> #include <QPen>
#include <QPainter> #include <QPainter>
#include <QFrame> #include <QFrame>
#include <QMouseEvent>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
namespace Ui { namespace Ui {
...@@ -20,11 +21,20 @@ public: ...@@ -20,11 +21,20 @@ public:
void updateFrame(cv::Mat *frame, std::vector<cv::Point> *points); void updateFrame(cv::Mat *frame, std::vector<cv::Point> *points);
void updateFrame(cv::Mat *frame); void updateFrame(cv::Mat *frame);
void updateFrame(std::vector<cv::Point> *points); void updateFrame(std::vector<cv::Point> *points);
virtual bool eventFilter(QObject *object, QEvent *ev) override;
private: private:
Ui::MaskEdit *ui; Ui::MaskEdit *ui;
cv::Mat *capture = nullptr; cv::Mat *capture = nullptr;
std::vector<cv::Point> *rectPoints = nullptr; std::vector<cv::Point> *rectPoints = nullptr;
int selectedCornerIndex = -1;
// relative to the captured frame, not the UI
float ratioX = 1.0f;
float ratioY = 1.0f;
int selectCorner(QMouseEvent* eMouse);
void UpdateSelectedCornerPosition(QMouseEvent* eMouse);
protected: protected:
void paintEvent(QPaintEvent* event); void paintEvent(QPaintEvent* event);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment