Skip to content
Snippets Groups Projects
Commit a0b95b71 authored by Fabien Mottier's avatar Fabien Mottier
Browse files

generate border edition

parent d0979a40
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include <algorithm>
#include "borderedit.h"
Mat BorderEdit::frameImage;
Mat BorderEdit::drawSquare(Point *p, int n)
{
Mat imageCopy = frameImage.clone();
polylines(imageCopy, &p, &n, 1, true, Scalar(0, 255, 0), 1, LINE_AA);
//imshow(wndname, imageCopy);
return imageCopy;
}
int BorderEdit::findPoints(int x, int y, vector<Point> *posSandbox)
{
for (int i = 0; i < (int)posSandbox->size(); i++)
{
//check if the point is clicked
if ((x >= (posSandbox->at(i).x - margeClick)) && (x <= (posSandbox->at(i).x + margeClick)) && (y >= (posSandbox->at(i).y - margeClick)) && (y <= (posSandbox->at(i).y + margeClick)))
return i;
}
return -1; //not found
}
Mat BorderEdit::edit(int selectedPoint, int x, int y, vector<Point> *posSandbox)
{
posSandbox->at(selectedPoint).x = x;
posSandbox->at(selectedPoint).y = y;
return BorderEdit::drawSquare(&posSandbox->at(0), (int)posSandbox->size());
}
Mat BorderEdit::initBorder(Mat frame, vector<Point> *posSandbox)
{
frame.copyTo(frameImage);
return BorderEdit::drawSquare(&posSandbox->at(0), (int)posSandbox->size());
}
#ifndef BORDEREDIT_H
#define BORDEREDIT_H
#include <opencv2/opencv.hpp>
#include <QLabel>
using namespace cv;
using namespace std;
class BorderEdit
{
private:
static const int margeClick = 10;
static constexpr char *wndname = (char *)"Sandbox Border Finder";
static Mat frameImage;
// OPENCV - MANUEL RECT CHANGE
static Mat drawSquare(cv::Point *p, int n);
public:
static Mat initBorder(Mat frame, vector<Point> *posSandbox);
static Mat edit(int selectedPoint, int x, int y, vector<Point> *posSandbox);
static int findPoints(int x, int y, vector<Point> *posSandbox);
};
#endif
#include <algorithm>
#include "borderfinder.h"
double BorderFinder::angle(Point pt1, Point pt2, Point pt0)
{
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1 * dx2 + dy1 * dy2) / sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
// returns sequence of squares detected on the image.
bool BorderFinder::find(Rect &rect)
{
vector<vector<Point>> rectDetect;
Mat pyr, timg, gray0(frameImage.size(), CV_8U);
// down-scale and upscale the image to filter out the noise
pyrDown(frameImage, pyr, Size(frameImage.cols / 2, frameImage.rows / 2));
pyrUp(pyr, timg, frameImage.size());
vector<vector<Point>> contours;
int c = 1;
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
gray0 = gray0 > 10;
waitKey(0);
findContours(gray0, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
vector<Point> approx;
// test each contour
cout << "nb rect: " << contours.size() << endl;
for (size_t i = 0; i < contours.size(); i++)
{
// approximate contour with accuracy proportional
// to the contour perimeter
approxPolyDP(contours[i], approx, arcLength(contours[i], true) * 0.02, true);
if (approx.size() == 4 &&
fabs(contourArea(approx)) > 1000 &&
isContourConvex(approx))
{
double maxCosine = 0;
for (int j = 2; j < 5; j++)
{
// find the maximum cosine of the angle between joint edges
double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));
maxCosine = MAX(maxCosine, cosine);
}
// if cosines of all angles are small
// (all angles are ~90 degree) then write quandrange
// vertices to resultant sequence
// if (maxCosine < 0.3)
// {
rect = rotateRect(boundingRect(approx), 90, 90);
const Point *p = &approx[0];
int n = (int)approx.size();
polylines(frameImage, &p, &n, 1, true, Scalar(0, 0, 255), 3, LINE_AA);
//return true;
//}
}
}
//imshow("ff", frameImage);
//waitKey(0);
return true;
}
Rect BorderFinder::rotateRect(Rect rect, int heightPercentage, int widthPercetange)
{
int rwidth = rect.width;
int rheight = rect.height;
rect.width = round((rect.width * widthPercetange) / 100.0f);
rect.height = round((rect.height * heightPercentage) / 100.0f);
rect.x += (rwidth - rect.width) / 2;
rect.y += (rheight - rect.height) / 2;
return rect;
}
float BorderFinder::calculDistance(float point1[], float point2[])
{
return sqrt(pow(point1[0] - point2[0], 2) +
pow(point1[1] - point2[1], 2) +
pow(point1[2] - point2[2], 2));
}
BorderFinder::BorderFinder(Mat frame)
{
frame.copyTo(frameImage);
}
#ifndef BORDERFINDER_H
#define BORDERFINDER_H
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class BorderFinder
{
private:
// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
double angle(Point pt1, Point pt2, Point pt0);
Mat frameImage;
Rect rotateRect(Rect rect, int heightPercentage, int widthPercetange);
public:
float calculDistance(float point1[], float point2[]);
BorderFinder(Mat frame);
bool find(Rect &rect);
};
#endif
......@@ -82,36 +82,56 @@ string Sandbox::deserialize(istream& stream)
return "Problem to load";
}
/*!
* \brief Sandbox::configure
* \param lblImage
* Use a QLabel to configure the Matrices
* With the Intel RealSense camera and a beamer
*/
void Sandbox::configure(QLabel* lblImage) {
// Setup matrix for the label
Mat frameBeamer(Size(Beamer::width, Beamer::height), CV_8UC3, Scalar(255, 0, 0));
lblImage->setPixmap(QPixmap::fromImage(QImage(frameBeamer.data, frameBeamer.cols, frameBeamer.rows, frameBeamer.step, QImage::Format_RGB888)));
Mat Sandbox::generateBorder() {
// Start the Intel RealSense Camera
camera.startAlign(); // 1 seconde of warming up
//camera.captureFramesAlign();
camera.captureFramesAlign();
/*Mat frameData = camera.getDepthFrameAlign();
Mat frameData = camera.getDepthFrameAlign();
Mat coloredFrame = camera.getRGBFrameAlign();
Size s = frameData.size();
Point center(s.width / 2, s.height / 2);
center.x = s.width / 2;
center.y = s.height / 2;
float distancePlan = static_cast<float>(mean(frameData(Rect(center, Size(10, 10))))[0]); // get sandbox distance from center of camera
calibrate.setDistancePlan(distancePlan);
//destroyAllWindows();
vector<Point> rectPoints;
float y = coloredFrame.size().height;
float x = coloredFrame.size().width;
rectPoints.push_back(Point(1.0/4*x, 1.0/4*y));
rectPoints.push_back(Point(1.0/4*x, 3.0/4*y));
rectPoints.push_back(Point(3.0/4*x, 3.0/4*y));
rectPoints.push_back(Point(3.0/4*x, 1.0/4*y));*/
rectPoints.push_back(Point(3.0/4*x, 1.0/4*y));
coloredFrame = BorderEdit::initBorder(coloredFrame, &rectPoints); // edit projected frame
return coloredFrame;
}
void Sandbox::applyBorder() {
// adjust model matrixe to forme a rectangle in sandbox
int widthTop = rectPoints[3].x - rectPoints[0].x;
double angle1 = atan((double)(rectPoints[3].y - rectPoints[0].y) / widthTop);
Mat matRotation = getRotationMatrix2D(center, toDegrees(angle1), 1);
calibrate.setMatrixRotation(matRotation);
Size rectSize = Size(widthTop, cvRound(widthTop / 1.33333) + 5);
Point p = calibrate.rotatePixel(rectPoints[0]);
rectSandbox = Rect(p, rectSize);
}
double Sandbox::toDegrees(double radians)
{
return radians * (180.0 / M_PI);
}
int Sandbox::findEdgeBorder(int x, int y) {
return BorderEdit::findPoints(x, y, &rectPoints);
}
Mat Sandbox::editEdgeBorder(int selectedPoint, int x, int y) {
return BorderEdit::edit(selectedPoint, x, y, &rectPoints);
}
......@@ -12,6 +12,8 @@
#include "camera.h"
#include "beamer.h"
#include "calibrate.h"
#include "borderedit.h"
#include "borderfinder.h"
#include <QPixmap>
#include <QLabel>
#include <QImage>
......@@ -29,6 +31,9 @@ private:
char* realSenseDeviceSerialNumber;
Camera camera;
Calibrate calibrate;
Rect rectSandbox;
vector<Point> rectPoints;
Point center;
public:
Sandbox();
......@@ -39,7 +44,12 @@ public:
void saveConfiguration();
void serialize(ostream& stream);
string deserialize(istream& stream);
void configure(QLabel* lblImage);
Mat configure();
double toDegrees(double radians);
void applyBorder();
Mat generateBorder();
int findEdgeBorder(int x, int y);
Mat editEdgeBorder(int selectedPoint, int x, int y);
};
#endif // SANDBOX_H
......@@ -20,14 +20,18 @@ SOURCES += \
beamer.cpp \
calibrate.cpp \
camera.cpp \
sandbox.cpp
sandbox.cpp \
borderedit.cpp \
borderfinder.cpp
HEADERS += \
beamer.h \
calibrate.h \
camera.h \
sandbox.h \
serializable.h
serializable.h \
borderedit.h \
borderfinder.h
# Default rules for deployment.
unix {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment