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

started refactoring

parents
No related branches found
No related tags found
No related merge requests found
#ifndef BORDEREDIT_H
#define BORDEREDIT_H
#include <opencv2/opencv.hpp>
class BorderEdit
{
private:
static const int margeClick = 10;
static constexpr char *wndname = (char *)"Sandbox Border Finder";
static cv::Mat frameImage;
// OPENCV - MANUEL RECT CHANGE
static void drawSquare(cv::Point *p, int n);
static int findPoints(int x, int y, std::vector<cv::Point> &posSandbox);
static void mouseHandler(int event, int x, int y, int, void *param);
public:
static void edit(cv::Mat frame, std::vector<cv::Point> *posSandbox);
};
#endif
\ No newline at end of file
#include <algorithm>
#include "borderfinder.h"
using namespace std;
using namespace cv;
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>
class BorderFinder
{
private:
// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
double angle(cv::Point pt1, cv::Point pt2, cv::Point pt0);
cv::Mat frameImage;
cv::Rect rotateRect(cv::Rect rect, int heightPercentage, int widthPercetange);
public:
float calculDistance(float point1[], float point2[]);
BorderFinder(cv::Mat frame);
bool find(cv::Rect &rect);
};
#endif
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <stdarg.h>
#include "log.h"
int log_level= 5;
/* [INFO] msg ... */
/* [DEBUG] msg ... */
/**
* Inform the user that an error occured and stop the execution.
* @param: char *msg - The message of the error
* @return
*/
void error(char*msg) {
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
void mylog(int log_level_value, const char *format,...){
/* Variadic argument in C : */
/* https://www.cprogramming.com/tutorial/c/lesson17.html */
va_list argList;
va_start(argList,format);
if (log_level & log_level_value) {
FILE *output=stderr;
switch(log_level_value ) {
case LOG_WARNING:
output=stderr;
fprintf(output,"[WARNING] ");
break;
case LOG_DEBUG:
output=stdout;
fprintf(output,"[DEBUG] ");
break;
case LOG_ERROR:
output=stderr;
fprintf(output,"[ERROR] ");
break;
default:
break;
}
vfprintf(output,format,argList);
}
va_end(argList);
}
\ No newline at end of file
#ifndef LOG_H
#define LOG_H
#define LOG_NONE 0x0
#define LOG_DEBUG 0x1
#define LOG_WARNING 0x2
#define LOG_ERROR 0x4
extern int log_level;
void error(char*msg);
void mylog(int log_level, const char *format,...);
#endif /* LOG_H */
\ No newline at end of file
#include "./sandbox.h"
Sandbox::Sandbox(){
//controller = Controller();
}
cv::Mat Sandbox::getRGBFrame(){
return controller.getDepthFrame();
}
cv::Mat Sandbox::getDepthFrame(){
return controller.getDepthFrame();
}
void Sandbox::showImage(cv::Mat* image){
controller.showImage(image);
}
/*
void sendAnswer(request_t *req, matrixPayload_t *payload, int socket)
{
answer_t ans;
matrixPayload_t payloadAnswer;
ans.id = req->id;
cv::Mat matrix;
switch (req->idAction)
{
case 0: //show
mylog(LOG_DEBUG, "Request: show image\n");
if (req->lengthPayload)
{
payloadToMatrix(&matrix, *payload);
sandbox->showImage(matrix);
waitKey(1);
}
else
{
mylog(LOG_ERROR, "No matrix found\n");
ans.error = -1;
}
break;
case 1:
mylog(LOG_DEBUG, "Request: get RGB frame\n");
sandbox->getRGBFrame().copyTo(matrix);
matrixToPayload(matrix, &payloadAnswer);
ans.lengthPayload = calculateSizeMatrix(payloadAnswer);
break;
case 2:
mylog(LOG_DEBUG, "Request: get DEPTH frame\n");
sandbox->getDepthFrame().copyTo(matrix);
matrixToPayload(matrix, &payloadAnswer);
ans.lengthPayload = calculateSizeMatrix(payloadAnswer);
break;
}
write(socket, &ans, sizeof(answer_t));
//send the matrix without the pointer
write(socket, &payloadAnswer, matrixSize);
if (ans.lengthPayload)
write(socket, payloadAnswer.data, ans.lengthPayload);
mylog(LOG_DEBUG, "Answer sent\n");
}
*/
\ No newline at end of file
#ifndef SANDBOX_H
#define SANDBOX_H
#include "./lib/controller.h"
class Sandbox{
public:
Sandbox();
cv::Mat getRGBFrame();
cv::Mat getDepthFrame();
void showImage(cv::Mat* image);
private:
Controller controller;
};
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment