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

show frame to edit cropping mask

parent d729aaa2
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#------------------------------------------------- #-------------------------------------------------
QT += core gui QT += core gui
QT += uitools
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
...@@ -27,17 +28,20 @@ SOURCES += \ ...@@ -27,17 +28,20 @@ SOURCES += \
main.cpp \ main.cpp \
monitorgui.cpp \ monitorgui.cpp \
camerafocus.cpp \ camerafocus.cpp \
croppingmask.cpp croppingmask.cpp \
maskedit.cpp
HEADERS += \ HEADERS += \
monitorgui.h \ monitorgui.h \
camerafocus.h \ camerafocus.h \
croppingmask.h croppingmask.h \
maskedit.h
FORMS += \ FORMS += \
monitorgui.ui \ monitorgui.ui \
camerafocus.ui \ camerafocus.ui \
croppingmask.ui croppingmask.ui \
maskedit.ui
......
...@@ -5,8 +5,11 @@ CroppingMask::CroppingMask(SandboxSetup *sandbox, QWidget *parent) : ...@@ -5,8 +5,11 @@ CroppingMask::CroppingMask(SandboxSetup *sandbox, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::CroppingMask) ui(new Ui::CroppingMask)
{ {
ui->setupUi(this);
setup = sandbox; setup = sandbox;
ui->setupUi(this);
maskEdit = new MaskEdit(ui->frame->geometry());
ui->vLayout->addWidget(maskEdit, 1);
// TODO : try to load cropping mask from file // TODO : try to load cropping mask from file
} }
...@@ -16,40 +19,30 @@ CroppingMask::~CroppingMask() ...@@ -16,40 +19,30 @@ CroppingMask::~CroppingMask()
delete ui; delete ui;
} }
void CroppingMask::paintEvent(QPaintEvent *){
QPainter p;
p.begin(this);
p.end();
}
void CroppingMask::on_btnTakePicture_clicked() void CroppingMask::on_btnTakePicture_clicked()
{ {
setup->captureBlueScreen(); setup->captureBlueScreen();
cv::Mat rgb = setup->camera.getRGBFrame(); cv::Mat rgb = setup->camera.getRGBFrame();
cameraRGBFrame = rgb; cameraRGBFrame = rgb;
QImage img = QImage((uchar *)rgb.data, (int)rgb.cols, (int)rgb.rows, static_cast<int>(rgb.step.buf[0]), QImage::Format_RGB888);
QPixmap image = QPixmap::fromImage(img);
//ui->lblFrame->setPixmap(image);
// no config found // no config found
if(!maskValideInFrame(rgb)){ if(!maskValideInFrame(&rgb)){
float y = ui->lblMask->height(); float y = rgb.size().height;
float x = ui->lblMask->width(); float x = rgb.size().width;
rectPoints = std::vector<cv::Point>{ cv::Point(1.0/4*x, 1.0/4*y), cv::Point(1.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 1.0/4*y) }; rectPoints = std::vector<cv::Point>{ cv::Point(1.0/4*x, 1.0/4*y), cv::Point(1.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 3.0/4*y), cv::Point(3.0/4*x, 1.0/4*y) };
} }
drawMask(rectPoints);
maskEdit->updateFrame(&cameraRGBFrame, &rectPoints);
} }
bool CroppingMask::maskValideInFrame(cv::Mat rgb){ bool CroppingMask::maskValideInFrame(cv::Mat *rgb){
if(rectPoints.empty()) if(rectPoints.empty())
return false; return false;
for(uint i=0; i<rectPoints.size(); i++){ for(uint i=0; i<rectPoints.size(); i++){
if( !(( 0<=rectPoints[i].x && rectPoints[i].x<rgb.size().width ) && if( !(( 0<=rectPoints[i].x && rectPoints[i].x<rgb->size().width ) &&
( 0<=rectPoints[i].y && rectPoints[i].y<rgb.size().height ) )) ( 0<=rectPoints[i].y && rectPoints[i].y<rgb->size().height ) ))
{ {
return false; return false;
} }
...@@ -58,47 +51,7 @@ bool CroppingMask::maskValideInFrame(cv::Mat rgb){ ...@@ -58,47 +51,7 @@ bool CroppingMask::maskValideInFrame(cv::Mat rgb){
return true; return true;
} }
void CroppingMask::drawMask(std::vector<cv::Point> rectPoints){
QPixmap img;
QPainter maskPainter(&img);
QPolygon poly;
for(uint i=0; i<rectPoints.size(); i++){
poly << QPoint(rectPoints[i].x, rectPoints[i].y);
}
// QPen: style(), width(), brush(), capStyle() and joinStyle().
QPen pen(Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
maskPainter.setPen(pen);
maskPainter.drawPolygon(poly);
maskPainter.drawRect(15,15,100,100);
QPixmap px;
QPainter p(&px);
p.setPen(Qt::blue);
p.drawLine(5,5, 40, 40);
p.end();
ui->lblMask->setPixmap(px);
}
void CroppingMask::scaleMaskWindowToFrame(){
float xCoeff = cameraRGBFrame.size().width / ui->lblMask->width();
float yCoeff = cameraRGBFrame.size().height / ui->lblMask->height();
for(uint i=0; i<rectPoints.size(); i++){
rectPoints[i].x = rectPoints[i].x * xCoeff;
rectPoints[i].y = rectPoints[i].y * yCoeff;
}
}
void CroppingMask::on_btnbxEnd_accepted() void CroppingMask::on_btnbxEnd_accepted()
{ {
state = true; state = true;
// Needed because the image shown is stretched
scaleMaskWindowToFrame();
} }
...@@ -3,10 +3,7 @@ ...@@ -3,10 +3,7 @@
#include <QDialog> #include <QDialog>
#include <sandboxSetup.h> #include <sandboxSetup.h>
#include "maskedit.h"
#include <QPainter>
#include <QPolygon>
//#include <QPainterPath>
namespace Ui { namespace Ui {
class CroppingMask; class CroppingMask;
...@@ -32,13 +29,9 @@ private: ...@@ -32,13 +29,9 @@ private:
std::vector<cv::Point> rectPoints; std::vector<cv::Point> rectPoints;
bool state = false; bool state = false;
cv::Mat cameraRGBFrame; cv::Mat cameraRGBFrame;
MaskEdit *maskEdit;
bool maskValideInFrame(cv::Mat rgb); bool maskValideInFrame(cv::Mat *rgb);
void drawMask(std::vector<cv::Point> rectPoints);
void scaleMaskWindowToFrame();
protected:
void paintEvent(QPaintEvent* event);
}; };
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>635</width> <width>635</width>
<height>422</height> <height>442</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>270</x> <x>270</x>
<y>370</y> <y>380</y>
<width>341</width> <width>341</width>
<height>32</height> <height>32</height>
</rect> </rect>
...@@ -32,35 +32,10 @@ ...@@ -32,35 +32,10 @@
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="lblFrame">
<property name="geometry">
<rect>
<x>40</x>
<y>30</y>
<width>441</width>
<height>281</height>
</rect>
</property>
<property name="lineWidth">
<number>3</number>
</property>
<property name="text">
<string>Press &quot;Take Picture&quot; when your beamer is ready</string>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="wordWrap">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="btnTakePicture"> <widget class="QPushButton" name="btnTakePicture">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>520</x> <x>510</x>
<y>60</y> <y>60</y>
<width>101</width> <width>101</width>
<height>25</height> <height>25</height>
...@@ -70,31 +45,40 @@ ...@@ -70,31 +45,40 @@
<string>Take Picture</string> <string>Take Picture</string>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="lblMask"> <widget class="QFrame" name="frame">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>40</x> <x>40</x>
<y>30</y> <y>40</y>
<width>441</width> <width>441</width>
<height>281</height> <height>301</height>
</rect> </rect>
</property> </property>
<property name="lineWidth"> <property name="frameShape">
<number>3</number> <enum>QFrame::StyledPanel</enum>
</property> </property>
<property name="text"> <property name="frameShadow">
<string/> <enum>QFrame::Raised</enum>
</property> </property>
<property name="scaledContents"> <widget class="QWidget" name="verticalLayoutWidget">
<bool>true</bool> <property name="geometry">
</property> <rect>
<property name="alignment"> <x>0</x>
<set>Qt::AlignCenter</set> <y>0</y>
</property> <width>441</width>
<property name="wordWrap"> <height>301</height>
<bool>false</bool> </rect>
</property> </property>
<layout class="QVBoxLayout" name="vLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</widget>
</widget> </widget>
<zorder>frame</zorder>
<zorder>btnbxEnd</zorder>
<zorder>btnTakePicture</zorder>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>
......
#include "maskedit.h"
#include "ui_maskedit.h"
MaskEdit::MaskEdit(const QRect geo, QWidget *parent) :
QFrame(parent),
ui(new Ui::MaskEdit)
{
ui->setupUi(this);
setGeometry(0,0, geo.width(), geo.height());
ui->lblFrame->setGeometry(0,0, width(), height());
}
MaskEdit::~MaskEdit()
{
delete ui;
}
void MaskEdit::updateFrame(cv::Mat *frame, std::vector<cv::Point> *points){
capture = frame;
rectPoints = points;
update();
}
void MaskEdit::updateFrame(cv::Mat *frame){
capture = frame;
update();
}
void MaskEdit::updateFrame(std::vector<cv::Point> *points){
rectPoints = points;
update();
}
void MaskEdit::paintEvent(QPaintEvent *){
if(capture != nullptr){
QImage img = QImage((uchar *)capture->data, (int)capture->cols, (int)capture->rows, static_cast<int>(capture->step.buf[0]), QImage::Format_RGB888);
QPixmap px = QPixmap::fromImage(img);
QPainter maskPainter(&px);
// QPen: style(), width(), brush(), capStyle() and joinStyle().
QPen pen(Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
maskPainter.setPen(pen);
QPolygon poly;
int size = 10;
for(uint i=0; i<rectPoints->size(); i++){
int x = rectPoints->at(i).x;
int y = rectPoints->at(i).y;
poly << QPoint(x, y);
maskPainter.drawEllipse(x-(size/2), y-(size/2), size, size);
}
maskPainter.drawPolygon(poly);
maskPainter.end();
ui->lblFrame->setPixmap(px);
}
}
/*
void CroppingMask::scaleMaskWindowToFrame(){
float xCoeff = cameraRGBFrame.size().width / ui->vLayout->width();
float yCoeff = cameraRGBFrame.size().height / ui->vLayout->height();
for(uint i=0; i<rectPoints.size(); i++){
rectPoints[i].x = rectPoints[i].x * xCoeff;
rectPoints[i].y = rectPoints[i].y * yCoeff;
}
}
*/
#ifndef MASKEDIT_H
#define MASKEDIT_H
#include <QPen>
#include <QPainter>
#include <QFrame>
#include <opencv2/opencv.hpp>
namespace Ui {
class MaskEdit;
}
class MaskEdit : public QFrame
{
Q_OBJECT
public:
explicit MaskEdit(const QRect geo, QWidget *parent = 0);
~MaskEdit();
void updateFrame(cv::Mat *frame, std::vector<cv::Point> *points);
void updateFrame(cv::Mat *frame);
void updateFrame(std::vector<cv::Point> *points);
private:
Ui::MaskEdit *ui;
cv::Mat *capture = nullptr;
std::vector<cv::Point> *rectPoints = nullptr;
protected:
void paintEvent(QPaintEvent* event);
};
#endif // MASKEDIT_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MaskEdit</class>
<widget class="QFrame" name="MaskEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>488</width>
<height>349</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLabel" name="lblFrame">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>441</width>
<height>281</height>
</rect>
</property>
<property name="lineWidth">
<number>3</number>
</property>
<property name="text">
<string>Press &quot;Take Picture&quot; when your beamer is ready</string>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="wordWrap">
<bool>false</bool>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment