Aircraft Detection 1.0
Loading...
Searching...
No Matches
utils.cpp File Reference
#include "utils.h"

Functions

double degrees2rad (double degrees)
 Converts degrees to radians.
 
double rad2degrees (double radians)
 Converts radians to degrees.
 
bool sortByDescendingArea (const object &first, const object &second)
 Comparator function to sort contours by descending area.
 
cv::Mat rotate90 (cv::Mat img, int step)
 Rotates an image by multiples of 90 degrees clockwise.
 
std::filesystem::path createDirectory (const std::filesystem::path &folder_path, const std::string &directory_name)
 Creates a new directory if it does not already exist.
 
void globFiles (const std::string &directory, const std::string &pattern, std::vector< std::string > &file_paths)
 Retrieves a list of file paths that match a specified pattern in a directory.
 
void readImages (const std::vector< std::string > &img_paths, std::vector< cv::Mat > &images, int flags)
 Reads images from a list of file paths and stores them in a vector.
 
void processYoloLabels (const std::string &filePath, const cv::Mat &img, std::vector< cv::Rect > &yolo_boxes)
 Processes YOLO labels from a file and converts them to bounding boxes.
 
cv::Rect Yolo2BRect (const cv::Mat &img, double x_center, double y_center, double width, double height)
 Converts YOLO format bounding box coordinates to a cv::Rect.
 
bool isRoiInImage (const cv::Rect &roi, int width, int height)
 Checks if a region of interest (ROI) is completely within the image boundaries.
 
std::vector< cv::Rect > readYoloBoxes (const std::filesystem::path &file_path, const cv::Mat &img)
 Reads YOLO bounding boxes from a file and converts them to OpenCV cv::Rect format.
 
std::ofstream openFile (const std::string &filename)
 Opens a file for writing and returns the output file stream.
 
std::vector< cv::Rect > generateRoisFromPoints (const std::vector< cv::Point > &points, const std::vector< cv::Size > &roi_sizes)
 Generates regions of interest (ROIs) from a set of points and ROI sizes.
 
cv::Size calculateAvgDims (const std::filesystem::path &directory_path)
 Calculates the average dimensions of all images in a specified directory.
 
void reshape2sameDim (std::vector< cv::Mat > &clustered_imgs_by_intensity, const cv::Size &avg_dim)
 Resizes a vector of images to the same dimensions.
 
double euclidean_distance (const cv::Point &a, const cv::Point &b)
 Calculates the Euclidean distance between two points.
 
std::vector< cv::Point > filterPointsByMinDistance (std::vector< cv::Point > &points, double min_distance)
 Filters a set of points by a minimum distance criterion.
 
void listDirectories (const std::filesystem::path &directory_path, std::vector< std::string > &final_paths)
 Recursively lists all leaf directories within a specified directory.
 
void imshow (const std::string &win_name, cv::InputArray arr, bool wait, float scale)
 
int bitdepth (int ocv_depth)
 

Variables

constexpr double PI = 3.14159265358979323846
 

Function Documentation

◆ bitdepth()

int bitdepth ( int ocv_depth)

◆ calculateAvgDims()

cv::Size calculateAvgDims ( const std::filesystem::path & directory_path)

Calculates the average dimensions of all images in a specified directory.

This function reads all PNG images in the specified directory and calculates the average width and height of the images. It returns the average dimensions as a cv::Size object.

Parameters
[in]directory_pathThe path to the directory containing the images.
Returns
A cv::Size object representing the average width and height of the images.
Exceptions
std::runtime_errorIf no images are found in the directory or if any image fails to load.
Note
The function assumes that the images are in PNG format.
See also
cv::glob
cv::imread
cv::Size

◆ createDirectory()

std::filesystem::path createDirectory ( const std::filesystem::path & folder_path,
const std::string & directory_name )

Creates a new directory if it does not already exist.

This function takes a base folder path and a directory name, creates the directory if it does not already exist, and returns the path to the created directory.

Parameters
[in]folder_pathThe base path where the new directory will be created.
[in]directory_nameThe name of the directory to be created.
Returns
A std::filesystem::path object representing the path to the created directory.
Note
If the directory already exists, the function simply returns the path without creating a new directory.
See also
std::filesystem::exists
std::filesystem::create_directory

◆ degrees2rad()

double degrees2rad ( double degrees)

Converts degrees to radians.

This function converts an angle from degrees to radians.

Parameters
[in]degreesThe angle in degrees.
Returns
The angle in radians.

◆ euclidean_distance()

double euclidean_distance ( const cv::Point & a,
const cv::Point & b )

Calculates the Euclidean distance between two points.

This function computes the Euclidean distance between two points a and b using the OpenCV cv::norm function.

Parameters
[in]aThe first point.
[in]bThe second point.
Returns
The Euclidean distance between the two points.
See also
cv::Point
cv::norm

◆ filterPointsByMinDistance()

std::vector< cv::Point > filterPointsByMinDistance ( std::vector< cv::Point > & points,
double min_distance )

Filters a set of points by a minimum distance criterion.

This function filters a vector of points, selecting only those points that are not closer than a specified minimum distance to any previously selected points. The points are first sorted by their coordinates to ensure consistent results.

Parameters
[in,out]pointsA vector of cv::Point objects representing the points to be filtered. The vector is sorted by coordinates.
[in]min_distanceThe minimum distance required between any two selected points.
Returns
A vector of cv::Point objects representing the points that meet the minimum distance criterion.
See also
euclidean_distance
cv::Point

◆ generateRoisFromPoints()

std::vector< cv::Rect > generateRoisFromPoints ( const std::vector< cv::Point > & points,
const std::vector< cv::Size > & roi_sizes )

Generates regions of interest (ROIs) from a set of points and ROI sizes.

This function takes a vector of points and a vector of ROI sizes, and generates ROIs centered at each point with each of the given sizes. It ensures that the generated ROIs are within the image boundaries before adding them to the output vector.

Parameters
[in]pointsA vector of cv::Point objects representing the centers of the ROIs.
[in]roi_sizesA vector of cv::Size objects representing the sizes of the ROIs.
Returns
A vector of cv::Rect objects representing the valid ROIs.
Note
The function checks if each generated ROI is within the image boundaries before adding it to the output vector.
The function assumes that the isRoiInImage function is defined and checks if an ROI is within the image boundaries.
See also
isRoiInImage

◆ globFiles()

void globFiles ( const std::string & directory,
const std::string & pattern,
std::vector< std::string > & file_paths )

Retrieves a list of file paths that match a specified pattern in a directory.

This function uses the OpenCV cv::glob function to find all files in the specified directory that match the given pattern and stores the paths of these files in a vector.

Parameters
[in]directoryThe directory in which to search for files.
[in]patternThe pattern to match files against, such as "*.png".
[out]file_pathsA vector of strings where the matching file paths will be stored.
See also
cv::glob

◆ imshow()

void imshow ( const std::string & win_name,
cv::InputArray arr,
bool wait,
float scale )

◆ isRoiInImage()

bool isRoiInImage ( const cv::Rect & roi,
int width,
int height )

Checks if a region of interest (ROI) is completely within the image boundaries.

This function checks whether a given ROI is entirely contained within the boundaries of an image with the specified width and height.

Parameters
[in]roiThe region of interest represented as a cv::Rect.
[in]widthThe width of the image.
[in]heightThe height of the image.
Returns
true if the ROI is completely within the image boundaries, false otherwise.

◆ listDirectories()

void listDirectories ( const std::filesystem::path & directory_path,
std::vector< std::string > & final_paths )

Recursively lists all leaf directories within a specified directory.

This function traverses the directory tree starting from the specified directory path and collects the paths of all leaf directories (directories that do not contain any subdirectories).

Parameters
[in]directory_pathThe path to the directory to start the traversal from.
[out]final_pathsA vector of strings to store the paths of all leaf directories.
Note
A leaf directory is defined as a directory that does not contain any subdirectories.
The function uses recursion to traverse the directory tree.
See also
std::filesystem::directory_iterator
std::filesystem::path

◆ openFile()

std::ofstream openFile ( const std::string & filename)

Opens a file for writing and returns the output file stream.

This function opens a file with the specified filename for writing. If the file cannot be opened, it throws a std::runtime_error.

Parameters
[in]filenameThe name of the file to be opened.
Returns
A std::ofstream object representing the opened file stream.
Exceptions
std::runtime_errorIf the file cannot be opened.

◆ processYoloLabels()

void processYoloLabels ( const std::string & filePath,
const cv::Mat & img,
std::vector< cv::Rect > & yolo_boxes )

Processes YOLO labels from a file and converts them to bounding boxes.

This function reads a YOLO label file and converts the normalized bounding box coordinates to cv::Rect objects representing the bounding boxes in the image. The bounding boxes are then stored in a vector.

Parameters
[in]filePathThe path to the YOLO label file.
[in]imgThe image in which the bounding boxes are defined.
[out]yolo_boxesA vector of cv::Rect objects where the converted bounding boxes will be stored.
Note
The YOLO label file should have the following format for each line: <class_id> <center_x> <center_y> <width> <height> where center_x, center_y, width, and height are normalized coordinates.
The function skips lines that cannot be read or have bounding boxes that fall beyond the image boundaries.
See also
Yolo2BRect

◆ rad2degrees()

double rad2degrees ( double radians)

Converts radians to degrees.

This function converts an angle from radians to degrees.

Parameters
[in]radiansThe angle in radians.
Returns
The angle in degrees.

◆ readImages()

void readImages ( const std::vector< std::string > & img_paths,
std::vector< cv::Mat > & images,
int flags )

Reads images from a list of file paths and stores them in a vector.

This function iterates through a list of image file paths, reads each image using OpenCV's cv::imread function with the specified flags, and stores the successfully read images in a vector.

Parameters
[in]img_pathsA vector of strings representing the paths to the image files to be read.
[out]imagesA vector of cv::Mat objects where the successfully read images will be stored.
[in]flagsThe flag that specifies the way the image should be read. This is passed to cv::imread.
See also
cv::imread

◆ readYoloBoxes()

std::vector< cv::Rect > readYoloBoxes ( const std::filesystem::path & file_path,
const cv::Mat & img )

Reads YOLO bounding boxes from a file and converts them to OpenCV cv::Rect format.

This function reads a file containing YOLO format bounding box coordinates and converts them to OpenCV cv::Rect objects, which are stored in a vector. The YOLO format assumes normalized coordinates in the range [0, 1].

Parameters
[in]file_pathThe path to the file containing YOLO bounding box coordinates.
[in]imgThe image for which the bounding boxes are defined. Used to convert normalized coordinates to pixel values.
Returns
A vector of cv::Rect objects representing the bounding boxes.
Exceptions
std::runtime_errorIf the file cannot be opened.
Note
The expected format of each line in the file is: "class_id x_center y_center width height".
If a line cannot be parsed, an error message is printed and the line is skipped.

◆ reshape2sameDim()

void reshape2sameDim ( std::vector< cv::Mat > & clustered_imgs_by_intensity,
const cv::Size & avg_dim )

Resizes a vector of images to the same dimensions.

This function resizes each image in a vector of images to the specified average dimensions.

Parameters
[in,out]clustered_imgs_by_intensityA vector of cv::Mat objects representing the images to be resized.
[in]avg_dimThe target dimensions to which all images will be resized.
See also
cv::resize
cv::Mat
cv::Size

◆ rotate90()

cv::Mat rotate90 ( cv::Mat img,
int step )

Rotates an image by multiples of 90 degrees clockwise.

This function rotates the input image by a specified number of 90-degree steps clockwise. The valid step values are 0 (no rotation), 1 (90 degrees), 2 (180 degrees), and 3 (270 degrees).

Parameters
[in]imgThe input image to be rotated.
[in]stepThe number of 90-degree steps to rotate the image. A negative value is converted to its positive equivalent.
Returns
A cv::Mat object representing the rotated image.
Note
The function uses cv::transpose and cv::flip to perform the rotations.
If step is greater than 3, it is reduced modulo 4 to ensure a valid number of steps.
See also
cv::transpose
cv::flip

◆ sortByDescendingArea()

bool sortByDescendingArea ( const object & first,
const object & second )

Comparator function to sort contours by descending area.

This function compares the areas of two contours and returns true if the area of the first contour is greater than the area of the second contour.

Parameters
[in]firstThe first contour.
[in]secondThe second contour.
Returns
true if the area of the first contour is greater than the area of the second contour, false otherwise.
See also
cv::contourArea

◆ Yolo2BRect()

cv::Rect Yolo2BRect ( const cv::Mat & img,
double x_center,
double y_center,
double width,
double height )

Converts YOLO format bounding box coordinates to a cv::Rect.

This function converts normalized YOLO bounding box coordinates (center x, center y, width, height) to a cv::Rect with pixel coordinates, ensuring the bounding box is within the image boundaries.

Parameters
[in]imgThe image for which the bounding box is defined.
[in]x_centerThe normalized x coordinate of the bounding box center.
[in]y_centerThe normalized y coordinate of the bounding box center.
[in]widthThe normalized width of the bounding box.
[in]heightThe normalized height of the bounding box.
Returns
A cv::Rect representing the bounding box in pixel coordinates. If the bounding box falls outside the image boundaries, an empty cv::Rect is returned.
Note
Normalized coordinates are in the range [0, 1]. The function converts these to pixel values.
If the calculated bounding box exceeds the image boundaries, an empty cv::Rect is returned.

Variable Documentation

◆ PI

double PI = 3.14159265358979323846
constexpr