rmcv  v0.1.0
A library for RoboMaster computer vision based on OpenCV.
Namespaces | Classes | Typedefs | Enumerations | Functions | Variables
rm Namespace Reference

Main modules. More...

Namespaces

 debug
 Debug utilities.
 

Classes

class  Armour
 
union  BitFloat
 
class  DahengCamera
 
class  LightBlob
 
class  Package
 
class  ParallelQueue
 
class  SerialPort
 

Typedefs

typedef std::vector< cv::Point > Contour
 
typedef enum rm::CompensateMode CompensateMode
 Define witch method is used to calculate the compensation of gravity.
 

Enumerations

enum  CampType { CAMP_RED = 0, CAMP_BLUE = 1, CAMP_GUIDELIGHT = 2, CAMP_NEUTRAL = -1 }
 
enum  AimMode { AIM_COMBAT = 0, AIM_BUFF = 1, AIM_SNIPE = 2 }
 
enum  RectType { RECT_TALL = 0, RECT_SIDE = 1 }
 
enum  FileType {
  FILETYPE_REGULAR_FILE = 0, FILETYPE_DIRECTORY = 1, FILETYPE_SYMBOLIC_LINK = 2, FILETYPE_SOCKET = 3,
  FILETYPE_UNKNOWN = -1
}
 
enum  CompensateMode { COMPENSATE_NONE = 0, COMPENSATE_CLASSIC = 1, COMPENSATE_NI = 2 }
 Define witch method is used to calculate the compensation of gravity. More...
 

Functions

rm::FileType GetFileType (const char *filename)
 
bool ListFiles (const char *path, std::vector< std::string > &filenames)
 
cv::Rect GetROI (cv::Point2f *imagePoints, int pointsCount, float scaleFactor=1.0f, const cv::Size &frameSize={-1, -1}, const cv::Rect &previous={0, 0, 0, 0})
 
cv::Rect GetROI (cv::Point2f *imagePoints, int pointsCount, const cv::Size2f &scaleFactor={1, 1}, const cv::Size &frameSize={-1, -1}, const cv::Rect &previous={0, 0, 0, 0})
 
void VerticesRectify (cv::RotatedRect &input, cv::Point2f *output, RectType type)
 
void CalcPerspective (cv::Point2f input[4], cv::Point2f output[4], float outRatio=1.0f)
 
double NewtonIteration (double(*fd)(double), double x0=0, double error=0.0001, int cycle=1024)
 
double NewtonIteration (double(*fd)(double, std::vector< double >), const std::vector< double > &literals, double x0=0, double error=0.0001, int cycle=1024)
 
double ProjectileMotionFD (double theta, std::vector< double > literals)
 
float PointDistance (const cv::Point2f &pt1, const cv::Point2f &pt2)
 
float PointDistance (const cv::Point2i &pt1, const cv::Point2i &pt2)
 
cv::Point2f LineCenter (const cv::Point2f &pt1, const cv::Point2f &pt2)
 
void ExtendCord (const cv::Point2f &pt1, const cv::Point2f &pt2, float deltaLen, cv::Point2f &dst1, cv::Point2f &dst2)
 
std::string PathCombine (const std::string &path1, const std::string &path2)
 
std::string int2str (int number)
 
void CalcRatio (cv::Mat &source, cv::Mat &calibration, cv::Point2f vertices[4], cv::Size outSize)
 
void CalcGamma (cv::Mat &source, cv::Mat &calibration, float gamma=0.5f)
 
void ExtractColor (cv::InputArray image, cv::OutputArray binary, rm::CampType enemy, int lowerBound, bool overExposed=false, cv::Size kernelSize={0, 0})
 
void AutoEnhance (cv::Mat &frame, float maxGainFactor=100.0, float minGainFactor=50.0)
 
void AutoBinarize (cv::Mat &image, cv::Mat &binary)
 
void AxisRotateX (double y, double z, double thetaX, double &outY, double &outZ)
 Rotate the vector around the x-axis. More...
 
void AxisRotateY (double x, double z, double thetaY, double &outX, double &outZ)
 Rotate the vector around the y-axis. More...
 
void AxisRotateZ (double x, double y, double thetaZ, double &outX, double &outY)
 Rotate the vector around the z-axis. More...
 
double DeltaHeight (cv::InputArray translationVector, double motorAngle, const cv::Point2f &offset={0, 0}, double angleOffset=0)
 Solve height difference between barrel and target. More...
 
double Distance (cv::InputArray translationVector)
 Solve the distance between camera and target. More...
 
double ProjectileAngle (double v0, double g, double d, double h)
 Solve initial angle required for a projectile motion. More...
 
bool SolveCameraPose (cv::InputArray rotationVector, cv::InputArray translationVector, cv::OutputArray pose)
 Solve camera pose in pitch, yaw, and roll relative to a target. More...
 
double SolveGEA (cv::InputArray translationVector, cv::OutputArray gimbalErrorAngle, double g, double v0, double h, const cv::Point2f &offset={0, 0}, double angleOffset=0, rm::CompensateMode mode=rm::COMPENSATE_NONE)
 Solve gimbal error angle to target by given method. More...
 
bool SolvePNP (const std::vector< cv::Point2f > &imagePoints, cv::InputArray cameraMatrix, cv::InputArray distortionFactor, const cv::Size2f &exactSize, cv::OutputArray translationVector, cv::OutputArray rotationVector, const cv::Rect &ROI={0, 0, 0, 0})
 Solve the rotation & translation vector using cv::solvePnP & cv::SOLVEPNP_IPPE_SQUARE. More...
 
bool MatchLightBlob (const rm::Contour &contour, float minRatio, float maxRatio, float tiltAngle, float minArea, float maxArea, cv::RotatedRect &lightBlobBox, bool fitEllipse=true)
 
void FindLightBlobs (std::vector< rm::Contour > &contours, std::vector< rm::LightBlob > &lightBlobs, float minRatio, float maxRatio, float tiltAngle, float minArea, float maxArea, const cv::Mat &source, bool fitEllipse=true)
 
void FindLightBlobs (std::vector< rm::Contour > &contours, std::vector< rm::LightBlob > &lightBlobs, float minRatio, float maxRatio, float tiltAngle, float minArea, float maxArea, rm::CampType camp, bool fitEllipse=true)
 
int FindGuidLight (const std::vector< rm::LightBlob > &lightBlobs, const cv::Mat &source)
 
bool LightBlobOverlap (std::vector< rm::LightBlob > &lightBlobs, int leftIndex, int rightIndex)
 
void FindArmour (std::vector< rm::LightBlob > &lightBlobs, std::vector< rm::Armour > &armours, float maxAngleDif, float errAngle, float minBoxRatio, float maxBoxRatio, float lenRatio, rm::CampType enemy, bool filter=true)
 
unsigned char LookupCRC (unsigned char *data, unsigned char dataLength, const unsigned char *crcTable=rm::CRC8)
 

Variables

const unsigned char CRC8 [256]
 

Detailed Description

Main modules.

Enumeration Type Documentation

◆ CompensateMode

Define witch method is used to calculate the compensation of gravity.

Enumerator
COMPENSATE_NONE 

No compensation.

COMPENSATE_CLASSIC 

Use newtonian's theorem of mechanics.

COMPENSATE_NI 

Use newton iteration method.

Function Documentation

◆ AutoBinarize()

void rm::AutoBinarize ( cv::Mat &  image,
cv::Mat &  binary 
)

Use the mean value of each channels to binarize given image and normalize to CV_32FC1.

Parameters
imageSource image.
binaryDestine image.

◆ AutoEnhance()

void rm::AutoEnhance ( cv::Mat &  frame,
float  maxGainFactor = 100.0,
float  minGainFactor = 50.0 
)

Auto enhance image by the given benchmarks.

Parameters
frameSource image & destine image.
maxGainFactorThe mean value of pixel values where gain should be maximize.
minGainFactorThe mean value of pixel values where gain should be minimize.

◆ AxisRotateX()

void rm::AxisRotateX ( double  y,
double  z,
double  thetaX,
double &  outY,
double &  outZ 
)

Rotate the vector around the x-axis.

Parameters
yy of the vector.
zz of the vector.
thetaXangle to be rotate.
outYy of the vector after rotation.
outZz of the vector after rotation.

◆ AxisRotateY()

void rm::AxisRotateY ( double  x,
double  z,
double  thetaY,
double &  outX,
double &  outZ 
)

Rotate the vector around the y-axis.

Parameters
xx of the vector.
zz of the vector.
thetaYangle to be rotate.
outXx of the vector after rotation.
outZz of the vector after rotation.

◆ AxisRotateZ()

void rm::AxisRotateZ ( double  x,
double  y,
double  thetaZ,
double &  outX,
double &  outY 
)

Rotate the vector around the z-axis.

Parameters
xx of the vector.
yy of the vector.
thetaZangle to be rotate.
outXx of the vector after rotation.
outYy of the vector after rotation.

◆ CalcGamma()

void rm::CalcGamma ( cv::Mat &  source,
cv::Mat &  calibration,
float  gamma = 0.5f 
)

Perform a gamma transform on the input image.

Parameters
sourceSource image.
calibrationOutput calibrated image.
gammaGamma factor.

◆ CalcPerspective()

void rm::CalcPerspective ( cv::Point2f  input[4],
cv::Point2f  output[4],
float  outRatio = 1.0f 
)

Calibrate given point sets in to specified aspect ratio.

Parameters
inputOrigin point sets.
outputPoints sets in specified aspect ratio.
outRatioAspect ratio.

◆ CalcRatio()

void rm::CalcRatio ( cv::Mat &  source,
cv::Mat &  calibration,
cv::Point2f  vertices[4],
cv::Size  outSize 
)

Calibrate a portion of the source frame to the destine rect.

Parameters
sourceSource image.
calibrationOutput calibrated image.
verticesVertices of the portion on the source frame.
outSizeDestine size.

◆ DeltaHeight()

double rm::DeltaHeight ( cv::InputArray  translationVector,
double  motorAngle,
const cv::Point2f &  offset = {0, 0},
double  angleOffset = 0 
)

Solve height difference between barrel and target.

Parameters
translationVectorThe translation vector of target.
motorAngleThe motor angle of gimbal, positive upwards. (RAD)
offsetOffset between camera and barrel. (cm)
angleOffsetAngle offset between camera and barrel. (RAD)
Returns
Height difference between barrel and target in cm, NAN if translationVector is not in cv::Mat format.

◆ Distance()

double rm::Distance ( cv::InputArray  translationVector)

Solve the distance between camera and target.

Parameters
translationVectorThe translation vector of target.
Returns
Distance in cm, NAN if translationVector is not in cv::Mat format.

◆ ExtendCord()

void rm::ExtendCord ( const cv::Point2f &  pt1,
const cv::Point2f &  pt2,
float  deltaLen,
cv::Point2f &  dst1,
cv::Point2f &  dst2 
)

Expand the cord by the given length while center of the cord remain still.

Parameters
pt1First point of the cord.
pt2Second point of the cord.
deltaLenThe length to be expanded.
dst1The first point after expanding.
dst2The second point after expanding.

◆ ExtractColor()

void rm::ExtractColor ( cv::InputArray  image,
cv::OutputArray  binary,
rm::CampType  enemy,
int  lowerBound,
bool  overExposed = false,
cv::Size  kernelSize = {0, 0} 
)

Extract specified color from source image.

Parameters
imageSource image.
binaryOutput binary image.
ownCampOwn camp.
overExposedSpecify if the image has been over exposed. If true, the color of own camp would be extracted somehow, consider pass source image to rm::FindLightBlobs function when finding light blobs.
lowerBoundLower bound when performing binarization.
kernelSizeKernel size when performing dilate.

◆ FindArmour()

void rm::FindArmour ( std::vector< rm::LightBlob > &  lightBlobs,
std::vector< rm::Armour > &  armours,
float  maxAngleDif,
float  errAngle,
float  minBoxRatio,
float  maxBoxRatio,
float  lenRatio,
rm::CampType  enemy,
bool  filter = true 
)

Fit armours from a set of light blobs.

Parameters
lightBlobsThe set of light blobs
armoursOutput armours
maxAngleDifMaximum angle difference between two light blobs.
errAngleMaximum angle between the over all rect and the two light blobs.
minBoxRatioMinimum ratio of the armour box.
maxBoxRatioMaximum ratio of the armour box.
lenRatioMaximum length ratio between two light blobs.
ownCampOwn camp.
attentionAttention point. When this parameter is specified, the distance between armour and the attention point would be used to sort the output.
filterExclude one if two armours sharing a same light blob, exclude the armour witch minimum height along two light blobs is smaller.

◆ FindGuidLight()

int rm::FindGuidLight ( const std::vector< rm::LightBlob > &  lightBlobs,
const cv::Mat &  source 
)

Find guid light

Parameters
lightBlobs
source
Returns

◆ FindLightBlobs() [1/2]

void rm::FindLightBlobs ( std::vector< rm::Contour > &  contours,
std::vector< rm::LightBlob > &  lightBlobs,
float  minRatio,
float  maxRatio,
float  tiltAngle,
float  minArea,
float  maxArea,
const cv::Mat &  source,
bool  fitEllipse = true 
)

Find light blobs from a set of contours. Auto detect light blob camp.

Parameters
contoursInput contour (more than 6 points).
lightBlobsOutput light blobs.
minRatioMinimal aspect ratio.
maxRatioMaximal aspect ratio.
tiltAngleMaximal tilt angle.
minAreaMinimal contour area.
maxAreaMaximal contour area.
sourceSource image (3 channels RGB).
fitEllipseUse cv::fitEllipseDirect to box light blob instead of cv::minAreaRect when True.

◆ FindLightBlobs() [2/2]

void rm::FindLightBlobs ( std::vector< rm::Contour > &  contours,
std::vector< rm::LightBlob > &  lightBlobs,
float  minRatio,
float  maxRatio,
float  tiltAngle,
float  minArea,
float  maxArea,
rm::CampType  camp,
bool  fitEllipse = true 
)

Find light blobs from a set of contours.

Parameters
contoursInput contour (more than 6 points).
lightBlobsOutput light blobs.
minRatioMinimal aspect ratio.
maxRatioMaximal aspect ratio.
tiltAngleMaximal tilt angle.
minAreaMinimal contour area.
maxAreaMaximal contour area.
campLight blobs camp.
fitEllipseUse cv::fitEllipseDirect to box light blob instead of cv::minAreaRect when True.

◆ LightBlobOverlap()

bool rm::LightBlobOverlap ( std::vector< rm::LightBlob > &  lightBlobs,
int  leftIndex,
int  rightIndex 
)

Detect if there is a overlap over all light blobs at the given pair of light blobs.

Parameters
lightBlobsAll light blobs on the frame (must be sorted ascending by x).
leftIndexIndex of the left light blob.
rightIndexIndex of the right light blob.
Returns
True if there is a overlap;

◆ LineCenter()

cv::Point2f rm::LineCenter ( const cv::Point2f &  pt1,
const cv::Point2f &  pt2 
)

Return the midpoint between two given points.

Parameters
pt1First point.
pt2Second point.
Returns
Midpoint.

◆ MatchLightBlob()

bool rm::MatchLightBlob ( const rm::Contour &  contour,
float  minRatio,
float  maxRatio,
float  tiltAngle,
float  minArea,
float  maxArea,
cv::RotatedRect &  lightBlobBox,
bool  fitEllipse = true 
)

Match a contour if it's a light blob with the given condition.

Parameters
contourInput contour (more than 6 points).
minRatioMinimal aspect ratio.
maxRatioMaximal aspect ratio.
tiltAngleMaximal tilt angle.
minAreaMinimal contour area.
maxAreaMaximal contour area.
fitEllipseUse cv::fitEllipseDirect to define light blob outlines instead of cv::minAreaRect when set to true. Usually recommended when source frame was over exposed.
Returns
True if condition matches.

◆ NewtonIteration() [1/2]

double rm::NewtonIteration ( double(*)(double)  fd,
double  x0 = 0,
double  error = 0.0001,
int  cycle = 1024 
)

Use newton's iteration to approach the approx solve of function.

Parameters
fdThe f(x)/f'(x) function of goal function.
x0x to start the iteration.
errorMaximum error before iteration stops.
cycleMaximum cycle the iteration could run.
Returns
The solve.

◆ NewtonIteration() [2/2]

double rm::NewtonIteration ( double(*)(double, std::vector< double >)  fd,
const std::vector< double > &  literals,
double  x0 = 0,
double  error = 0.0001,
int  cycle = 1024 
)

Use newton's iteration to approach the approx solve of function.

Parameters
fdThe f(x)/f'(x) function of goal function.
literalsThe literals that all f(x) needed.
x0x to start the iteration.
errorMaximum error before iteration stops.
cycleMaximum cycle the iteration could run.
Returns
The solve.

◆ PointDistance() [1/2]

float rm::PointDistance ( const cv::Point2f &  pt1,
const cv::Point2f &  pt2 
)

Return the distance between two given points.

Parameters
pt1First point.
pt2Second point.
Returns
Distance.

◆ PointDistance() [2/2]

float rm::PointDistance ( const cv::Point2i &  pt1,
const cv::Point2i &  pt2 
)

Return the distance between two given points.

Parameters
pt1First point.
pt2Second point.
Returns
Distance.

◆ ProjectileAngle()

double rm::ProjectileAngle ( double  v0,
double  g,
double  d,
double  h 
)

Solve initial angle required for a projectile motion.

This function would be done by solving \( 0 = \frac{g \cdot d^{2}}{2 \cdot v_{0}^{2}} \cdot tan\theta^{2} + d \cdot tan\theta + (\frac{g \cdot d^{2}}{2 \cdot v_{0}^{2}} - h) \) for \(tan\theta\) using the binary first-order root formula, witch can be deduced from Newtonian's theorem of mechanics.

Parameters
v0Initial launch speed. (m/s)
gAcceleration of gravity. (m/s^2)
dHorizontal distance. (m)
hHeight difference. (m)
Returns
Estimated launch angle in radians, NAN if equations have no real solutions.

◆ ProjectileMotionFD()

double rm::ProjectileMotionFD ( double  theta,
std::vector< double >  literals 
)

The f(x)/f'(x) function of projectile motion.

Parameters
thetaThe angle of the initial shot of the oblique throwing motion, in radians.
literalsThe independent variables, in the order of g, d, h, v0.
Returns
f(x)/f'(x)

◆ SolveCameraPose()

bool rm::SolveCameraPose ( cv::InputArray  rotationVector,
cv::InputArray  translationVector,
cv::OutputArray  pose 
)

Solve camera pose in pitch, yaw, and roll relative to a target.

Parameters
rotationVectorThe rotation vector of target.
translationVectorThe translation vector of target.
pose[OUT] Camera pose.
Returns
False if translationVector or rotationVector is not in cv::Mat format.

◆ SolveGEA()

double rm::SolveGEA ( cv::InputArray  translationVector,
cv::OutputArray  gimbalErrorAngle,
double  g,
double  v0,
double  h,
const cv::Point2f &  offset = {0, 0},
double  angleOffset = 0,
rm::CompensateMode  mode = rm::COMPENSATE_NONE 
)

Solve gimbal error angle to target by given method.

Parameters
translationVectorTranslation vector of target.
gimbalErrorAngle[OUT] Estimation error angle. Format: [pitch, yaw].
gAcceleration of gravity. (m/s^2)
v0The initial speed of bullet. (m/s)
hHeight between barrel and target. (m)
offsetOffset between camera and barrel. (cm)
angleOffsetAngle offset between camera and barrel. (RAD)
modeMethod to be used to calculate the compensation of gravity.
Returns
Estimation air time, NAN if translationVector is not in cv::Mat format.

◆ SolvePNP()

bool rm::SolvePNP ( const std::vector< cv::Point2f > &  imagePoints,
cv::InputArray  cameraMatrix,
cv::InputArray  distortionFactor,
const cv::Size2f &  exactSize,
cv::OutputArray  translationVector,
cv::OutputArray  rotationVector,
const cv::Rect &  ROI = {0, 0, 0, 0} 
)

Solve the rotation & translation vector using cv::solvePnP & cv::SOLVEPNP_IPPE_SQUARE.

Parameters
imagePointsObject points on image (Quantity must be four).
cameraMatrixCamera matrix.
distortionFactorCamera distortion factor.
exactSizeExact size of the coordinate object (cm).
translationVector[OUT] Translation vector.
rotationVector[OUT] Rotation vector.
Returns
False if imagePoints.size() is not 4, or there might be errors in Camera-parameters or Points-given, such as wrong There is no one-to-one correspondence between the points on the image and the actual points.

◆ VerticesRectify()

void rm::VerticesRectify ( cv::RotatedRect &  input,
cv::Point2f *  output,
RectType  type = RECT_TALL 
)

Reorder vertices of a rectangle to match the point order in left down, left up, right up, right down.

Parameters
inputThe original rectangle.
outputReordered vertices.
typeRectangle type.

Variable Documentation

◆ CRC8

const unsigned char rm::CRC8[256]
Initial value:
{0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f,
0x2e, 0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f,
0x5c, 0x6d, 0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73, 0x20, 0x11, 0x3f, 0x0e, 0x5d, 0x6c, 0xfb,
0xca, 0x99, 0xa8, 0xc5, 0xf4, 0xa7, 0x96, 0x01, 0x30, 0x63, 0x52, 0x7c, 0x4d, 0x1e, 0x2f,
0xb8, 0x89, 0xda, 0xeb, 0x3d, 0x0c, 0x5f, 0x6e, 0xf9, 0xc8, 0x9b, 0xaa, 0x84, 0xb5, 0xe6,
0xd7, 0x40, 0x71, 0x22, 0x13, 0x7e, 0x4f, 0x1c, 0x2d, 0xba, 0x8b, 0xd8, 0xe9, 0xc7, 0xf6,
0xa5, 0x94, 0x03, 0x32, 0x61, 0x50, 0xbb, 0x8a, 0xd9, 0xe8, 0x7f, 0x4e, 0x1d, 0x2c, 0x02,
0x33, 0x60, 0x51, 0xc6, 0xf7, 0xa4, 0x95, 0xf8, 0xc9, 0x9a, 0xab, 0x3c, 0x0d, 0x5e, 0x6f,
0x41, 0x70, 0x23, 0x12, 0x85, 0xb4, 0xe7, 0xd6, 0x7a, 0x4b, 0x18, 0x29, 0xbe, 0x8f, 0xdc,
0xed, 0xc3, 0xf2, 0xa1, 0x90, 0x07, 0x36, 0x65, 0x54, 0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc,
0x9f, 0xae, 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17, 0xfc, 0xcd, 0x9e, 0xaf, 0x38,
0x09, 0x5a, 0x6b, 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3, 0xd2, 0xbf, 0x8e, 0xdd, 0xec,
0x7b, 0x4a, 0x19, 0x28, 0x06, 0x37, 0x64, 0x55, 0xc2, 0xf3, 0xa0, 0x91, 0x47, 0x76, 0x25,
0x14, 0x83, 0xb2, 0xe1, 0xd0, 0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69, 0x04, 0x35,
0x66, 0x57, 0xc0, 0xf1, 0xa2, 0x93, 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a, 0xc1,
0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef,
0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d,
0xac}