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) :
ui->setupUi(this);
maskEdit = new MaskEdit(ui->frame->geometry());
ui->vLayout->addWidget(maskEdit, 1);
// TODO : try to load cropping mask from file
}
......
......@@ -5,7 +5,9 @@ MaskEdit::MaskEdit(const QRect geo, QWidget *parent) :
QFrame(parent),
ui(new Ui::MaskEdit)
{
this->installEventFilter(this);
ui->setupUi(this);
// set size like the parent widget's size
setGeometry(0,0, geo.width(), geo.height());
ui->lblFrame->setGeometry(0,0, width(), height());
}
......@@ -19,21 +21,22 @@ MaskEdit::~MaskEdit()
void MaskEdit::updateFrame(cv::Mat *frame, std::vector<cv::Point> *points){
capture = frame;
rectPoints = points;
if(frame != nullptr && points != nullptr){
ratioX = (float)capture->size().width / ui->lblFrame->width();
ratioY = (float)capture->size().height / ui->lblFrame->height();
}
update();
}
void MaskEdit::updateFrame(cv::Mat *frame){
capture = frame;
update();
updateFrame(frame, rectPoints);
}
void MaskEdit::updateFrame(std::vector<cv::Point> *points){
rectPoints = points;
update();
updateFrame(capture, points);
}
void MaskEdit::paintEvent(QPaintEvent *){
if(capture != nullptr){
......@@ -64,16 +67,77 @@ void MaskEdit::paintEvent(QPaintEvent *){
}
}
/*
void CroppingMask::scaleMaskWindowToFrame(){
float xCoeff = cameraRGBFrame.size().width / ui->vLayout->width();
float yCoeff = cameraRGBFrame.size().height / ui->vLayout->height();
bool MaskEdit::eventFilter(QObject *object, QEvent *ev)
{
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++){
rectPoints[i].x = rectPoints[i].x * xCoeff;
rectPoints[i].y = rectPoints[i].y * yCoeff;
int MaskEdit::selectCorner(QMouseEvent* eMouse){
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 @@
#include <QPen>
#include <QPainter>
#include <QFrame>
#include <QMouseEvent>
#include <opencv2/opencv.hpp>
namespace Ui {
......@@ -20,11 +21,20 @@ public:
void updateFrame(cv::Mat *frame, std::vector<cv::Point> *points);
void updateFrame(cv::Mat *frame);
void updateFrame(std::vector<cv::Point> *points);
virtual bool eventFilter(QObject *object, QEvent *ev) override;
private:
Ui::MaskEdit *ui;
cv::Mat *capture = 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:
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