2018-12-14 17:04:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 dimercur
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __IMG_H__
|
|
|
|
#define __IMG_H__
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
2018-12-19 09:15:42 +01:00
|
|
|
#include <string>
|
2018-12-14 17:04:42 +01:00
|
|
|
|
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
#include <opencv2/imgproc/imgproc.hpp>
|
|
|
|
#include <opencv2/core.hpp>
|
|
|
|
|
|
|
|
#ifdef __WITH_ARUCO__
|
|
|
|
#include <opencv2/aruco/dictionary.hpp>
|
|
|
|
#include <opencv2/aruco/charuco.hpp>
|
|
|
|
#include <opencv2/core/mat.hpp>
|
|
|
|
#endif // __WITH_ARUCO__
|
|
|
|
|
|
|
|
#define ARENA_NOT_DETECTED -1
|
|
|
|
|
2018-12-19 09:15:42 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Redefinition of cv::Mat type
|
|
|
|
*/
|
2018-12-14 17:04:42 +01:00
|
|
|
typedef cv::Mat ImageMat;
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Declaration of Jpg type
|
|
|
|
*/
|
2018-12-19 09:15:42 +01:00
|
|
|
typedef vector<unsigned char> Jpg;
|
2018-12-14 17:04:42 +01:00
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
2019-01-17 15:18:19 +01:00
|
|
|
* Class position, used for store robot coordinates
|
2019-01-15 16:25:48 +01:00
|
|
|
*
|
2019-01-17 15:18:19 +01:00
|
|
|
* @brief Class position, used for store robot coordinates
|
2019-01-15 16:25:48 +01:00
|
|
|
*/
|
2019-01-17 15:18:19 +01:00
|
|
|
class Position {
|
|
|
|
public:
|
|
|
|
int robotId;
|
|
|
|
float angle;
|
2019-01-15 16:25:48 +01:00
|
|
|
cv::Point2f center;
|
2018-12-14 17:04:42 +01:00
|
|
|
cv::Point2f direction;
|
2019-01-17 15:18:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of Position object
|
|
|
|
*/
|
|
|
|
Position() {
|
|
|
|
robotId = -1;
|
|
|
|
angle = 0.0;
|
|
|
|
center=cv::Point2f(0.0,0.0);
|
|
|
|
direction=cv::Point2f(0.0,0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a string representation of the object
|
|
|
|
* @return String containing object information
|
|
|
|
*/
|
|
|
|
string ToString() {
|
|
|
|
return "Id=" + to_string(this->robotId) + ", Angle=" + to_string(this->angle) + ", center=(" + to_string(this->center.x) + ";" + to_string(this->center.y) + ")";
|
|
|
|
}
|
|
|
|
};
|
2018-12-14 17:04:42 +01:00
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Class arena, used for holding outline of arena on image and cropping image to only usefull area
|
|
|
|
*
|
|
|
|
* @brief Class arena, used for holding outline of arena on image and cropping image to only usefull area
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
class Arena {
|
2018-12-14 17:04:42 +01:00
|
|
|
public:
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
2019-01-17 15:18:19 +01:00
|
|
|
* Coordinate of arena, empty if no arena found
|
2019-01-15 16:25:48 +01:00
|
|
|
*/
|
2019-01-17 15:18:19 +01:00
|
|
|
cv::Rect arena;
|
2018-12-14 17:04:42 +01:00
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
2019-01-17 15:18:19 +01:00
|
|
|
* Constructor of Arena object
|
2019-01-15 16:25:48 +01:00
|
|
|
*/
|
2019-01-17 15:18:19 +01:00
|
|
|
Arena() {}
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tell if arena is empty (not found) or not
|
|
|
|
* @return true if no arena found, false otherwise
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
bool IsEmpty();
|
2019-01-17 15:18:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a string representation of the object
|
|
|
|
* @return String containing object information
|
|
|
|
*/
|
|
|
|
string ToString() {
|
|
|
|
if (IsEmpty()) return "Arena empty";
|
|
|
|
else
|
|
|
|
return "Arena: (x;y)=(" + to_string(this->arena.x) + ";" + to_string(this->arena.x) + " (w;h)=(" + to_string(this->arena.width) + ";" + to_string(this->arena.height) + ")";
|
|
|
|
}
|
2018-12-14 17:04:42 +01:00
|
|
|
};
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Class for image storage and treatment
|
|
|
|
*
|
|
|
|
* @brief Class for image storage and treatment
|
|
|
|
*/
|
2018-12-14 17:04:42 +01:00
|
|
|
class Img {
|
|
|
|
public:
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Image data
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
ImageMat img;
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Create new Img object based on image data
|
|
|
|
*
|
|
|
|
* @param imgMatrice Image data to be stored (raw data)
|
|
|
|
*/
|
2018-12-14 17:04:42 +01:00
|
|
|
Img(ImageMat imgMatrice);
|
2018-12-19 09:15:42 +01:00
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Convert object to a string representation
|
|
|
|
*
|
|
|
|
* @return String containing information on contained image (size and number of channel)
|
|
|
|
*/
|
2018-12-19 09:15:42 +01:00
|
|
|
string ToString();
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a copy of current object
|
|
|
|
*
|
|
|
|
* @return New Img object, copy of current
|
|
|
|
*/
|
2018-12-19 09:15:42 +01:00
|
|
|
Img* Copy();
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Compress current image to JPEG
|
|
|
|
* @return Image compressed as JPEG
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
Jpg ToJpg();
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search arena outline in current image
|
|
|
|
* @return Arena object with coordinate of outline, empty if no arena found
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
Arena SearchArena();
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw an oriented arrow at robot position
|
|
|
|
* @param robot Position of robot
|
|
|
|
*/
|
|
|
|
void DrawRobot(Position robot);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw an oriented arrow for each position provided
|
|
|
|
* @param robots List of robot positions
|
|
|
|
* @return Number of position drawn
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
int DrawAllRobots(std::list<Position> robots);
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw arena outline
|
|
|
|
* @param arenaToDraw Arena position
|
|
|
|
*/
|
|
|
|
void DrawArena(Arena arenaToDraw);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search available robots in an image
|
|
|
|
* @param arena Arena position for cropping image
|
|
|
|
* @return list of position, empty if no robot found
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
std::list<Position> SearchRobot(Arena arena);
|
|
|
|
|
2018-12-14 17:04:42 +01:00
|
|
|
#ifdef __WITH_ARUCO__
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Dictionary to be used for aruco recognition
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
cv::Ptr<cv::aruco::Dictionary> dictionary;
|
2018-12-14 17:04:42 +01:00
|
|
|
#endif // __WITH_ARUCO__
|
|
|
|
private:
|
|
|
|
#ifdef __WITH_ARUCO__
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Find center point of given aruco
|
|
|
|
* @param aruco Aruco coordinates
|
|
|
|
* @return Center point coordinate
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
cv::Point2f FindArucoCenter(std::vector<cv::Point2f> aruco);
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find direction of given aruco
|
|
|
|
* @param aruco Aruco coordinates
|
|
|
|
* @return Orientation of aruco
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
cv::Point2f FindArucoDirection(std::vector<cv::Point2f> aruco);
|
2018-12-14 17:04:42 +01:00
|
|
|
#endif // __WITH_ARUCO__
|
|
|
|
|
2019-01-15 16:25:48 +01:00
|
|
|
/**
|
|
|
|
* Function for computing angle
|
|
|
|
* @param robots Position of robot
|
|
|
|
* @return Angle
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
float CalculAngle(Position robots);
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for computing angle
|
|
|
|
* @param pt1 ???
|
|
|
|
* @param pt2 ???
|
|
|
|
* @return Angle
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
float CalculAngle2(cv::Point2f pt1, cv::Point2f pt2);
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for computing distance
|
|
|
|
* @param p ???
|
|
|
|
* @param q ???
|
|
|
|
* @return Distance
|
|
|
|
*/
|
2019-01-04 16:55:48 +01:00
|
|
|
float EuclideanDistance(cv::Point2f p, cv::Point2f q);
|
2019-01-15 16:25:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Crop image around detected arena
|
|
|
|
* @param arena Coordinate of arena
|
|
|
|
* @return Reduced image, focused on arena
|
|
|
|
*/
|
2019-01-09 16:32:16 +01:00
|
|
|
ImageMat CropArena(Arena arena);
|
2018-12-14 17:04:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //__IMG_H__
|