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

Functions

void selectAirplanes (const cv::Mat &img, const std::vector< cv::Rect > &yolo_boxes, std::vector< cv::Rect > &selected_airplanes_yolo_boxes, int &count, const std::string &img_filename, const std::filesystem::path &straight_airplanes_folder)
 Selects and processes airplane images based on user input.
 
void binarizeAirplanes (const cv::Mat &channel, const std::vector< cv::Rect > &selected_airplanes_yolo_boxes, std::vector< cv::Mat > &bin_airplanes)
 Binarizes regions of interest (ROIs) in an image using adaptive thresholding.
 
void calculateGeometricMoments (const std::vector< cv::Mat > &bin_airplanes, const std::vector< cv::Rect > &yolo_boxes, std::vector< std::pair< cv::Point2f, double > > &geometric_moments_descriptors)
 Calculates geometric moments for a set of binary airplane images and corresponding YOLO bounding boxes.
 
void saveStraightAirplanes (const std::vector< cv::Mat > &airplanes, int &count, const std::string &img_filename, const std::filesystem::path &straight_airplanes_folder)
 Displays and optionally saves a list of airplane images after user confirmation and optional rotation.
 
void saveAirplane (const cv::Mat &airplane, int &count, const std::string &img_filename, const std::filesystem::path &straight_airplane_folder)
 Saves an airplane image to a specified directory with a unique filename.
 
void extractRotatedAirplanes (const cv::Mat &img, const std::vector< cv::Mat > &bin_airplanes, const std::vector< std::pair< cv::Point2f, double > > &geometric_moments_descriptors, const std::vector< cv::Rect > &yolo_boxes, std::vector< cv::Mat > &airplanes_vector)
 Extracts and rotates airplane images from the input image based on geometric moments and YOLO bounding boxes.
 
std::string getValidInput (const std::string &prompt, const std::vector< std::string > &valid_inputs)
 Prompts the user for input until a valid input is provided.
 
cv::Mat getBoundarySafeROI (const cv::Mat &img, cv::Rect &roi)
 Extracts a region of interest (ROI) from an image, ensuring it stays within image boundaries.
 
void extractStraightAirplanes ()
 Extracts, processes, and saves straightened airplane images from a dataset.
 
double correctAngle (double angle)
 Corrects the orientation angle of an airplane image.
 
cv::Rect calculateCenteredROI (const cv::Point2f &center, const cv::Size &size, float scale_factor)
 Calculates a centered region of interest (ROI) around a given point with a specified size and scale factor.
 

Function Documentation

◆ binarizeAirplanes()

void binarizeAirplanes ( const cv::Mat & channel,
const std::vector< cv::Rect > & selected_airplanes_yolo_boxes,
std::vector< cv::Mat > & bin_airplanes )

Binarizes regions of interest (ROIs) in an image using adaptive thresholding.

This function extracts specified ROIs from an image channel, applies adaptive thresholding to binarize them, and performs morphological dilation to enhance the binary images. The resulting binary images are stored in an output vector.

Parameters
[in]channelThe input image channel from which the ROIs are extracted.
[in]selected_airplanes_yolo_boxesA vector of cv::Rect objects representing the ROIs to be binarized.
[out]bin_airplanesA vector of cv::Mat objects to store the resulting binary images.
Note
The function adjusts the block size for adaptive thresholding to ensure it is odd.
A constant C is used to fine-tune the thresholding.
Morphological dilation is applied using an elliptical structuring element.
See also
cv::adaptiveThreshold
cv::morphologyEx
cv::getStructuringElement
cv::THRESH_BINARY

◆ calculateCenteredROI()

cv::Rect calculateCenteredROI ( const cv::Point2f & center,
const cv::Size & size,
float scale_factor )

Calculates a centered region of interest (ROI) around a given point with a specified size and scale factor.

This function computes a rectangular ROI centered at the given point, with dimensions scaled by the specified scale factor.

Parameters
[in]centerThe center point around which the ROI is calculated.
[in]sizeThe size of the ROI before scaling.
[in]scale_factorThe factor by which the size of the ROI is scaled.
Returns
A cv::Rect representing the calculated ROI.
Note
The resulting ROI may need to be adjusted to ensure it stays within the image boundaries when used.

◆ calculateGeometricMoments()

void calculateGeometricMoments ( const std::vector< cv::Mat > & bin_airplanes,
const std::vector< cv::Rect > & yolo_boxes,
std::vector< std::pair< cv::Point2f, double > > & geometric_moments_descriptors )

Calculates geometric moments for a set of binary airplane images and corresponding YOLO bounding boxes.

This function computes the geometric moments for each binary airplane image, calculates the center and orientation angle, and adjusts the center based on the corresponding YOLO bounding box. The results are stored in a vector of pairs, where each pair contains the corrected center point and the orientation angle in degrees.

Parameters
[in]bin_airplanesA vector of binary cv::Mat objects representing the segmented airplanes.
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes for each airplane.
[out]geometric_moments_descriptorsA vector of pairs where each pair contains the corrected center point (cv::Point2f) and the orientation angle (double) in degrees for each airplane.
Note
The function assumes that the contours are sorted by area in descending order to select the largest contour for moment calculation.
The orientation angle is corrected to degrees from radians.
See also
cv::findContours
cv::moments
std::atan2
rad2degrees
sortByDescendingArea

◆ correctAngle()

double correctAngle ( double angle)

Corrects the orientation angle of an airplane image.

This function adjusts the orientation angle of an airplane image to ensure it is within a specific range. If the angle is between 0 and 90 degrees, it adds 90 degrees to the angle. If the angle is negative, it adjusts the angle to be within the 0-180 degree range.

Parameters
[in]angleThe original orientation angle of the airplane in degrees.
Returns
The corrected orientation angle in degrees.

◆ extractRotatedAirplanes()

void extractRotatedAirplanes ( const cv::Mat & img,
const std::vector< cv::Mat > & bin_airplanes,
const std::vector< std::pair< cv::Point2f, double > > & geometric_moments_descriptors,
const std::vector< cv::Rect > & yolo_boxes,
std::vector< cv::Mat > & airplanes_vector )

Extracts and rotates airplane images from the input image based on geometric moments and YOLO bounding boxes.

This function processes binary airplane images and their corresponding geometric moments and YOLO bounding boxes to extract, rotate, and resize the airplanes. The resulting straightened airplane images are stored in the output vector.

The steps are as follows:

  1. Iterate through each binary airplane image.
  2. Check if the current index is valid.
  3. Calculate the center and correct the angle using geometric moments.
  4. Define the region of interest (ROI) and get a safe ROI within image boundaries.
  5. Rotate the ROI to align the airplane.
  6. Define the final ROI and extract the straightened airplane image.
  7. Store the straightened airplane image in the output vector if the final ROI is valid.
Parameters
[in]imgThe input image from which the airplanes are extracted.
[in]bin_airplanesA vector of binary cv::Mat objects representing the segmented airplanes.
[in]geometric_moments_descriptorsA vector of pairs containing the center points and angles (in degrees) for each airplane.
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes for each airplane.
[out]airplanes_vectorA vector of cv::Mat objects to store the extracted and rotated airplane images.
Note
The function uses scaling factors to adjust the size of the ROI for better extraction.
If the final ROI is out of image boundaries, the airplane is skipped.
See also
correctAngle
calculateCenteredROI
getBoundarySafeROI
cv::getRotationMatrix2D
cv::warpAffine

◆ extractStraightAirplanes()

void extractStraightAirplanes ( )

Extracts, processes, and saves straightened airplane images from a dataset.

This function processes a dataset of images and their corresponding YOLO label files to extract, straighten, and save airplane images. It performs the following steps:

  1. Reads the paths of the dataset images and YOLO label files.
  2. Creates a directory to save the straightened airplane images.
  3. Iterates through each dataset image: a. Reads the image and converts it to HSV color space. b. Processes the YOLO labels to obtain bounding boxes for the airplanes. c. Prompts the user to select and optionally rotate the airplanes. d. Binarizes the selected airplane regions. e. Calculates geometric moments to determine the orientation of the airplanes. f. Extracts and rotates the airplane images to be upright. g. Saves the processed airplane images to the specified directory.
Note
This function assumes that the dataset images and YOLO label files are in the same directory.
The processed images are saved in the straight_airplanes directory within the source directory.
See also
globFiles
createDirectory
processYoloLabels
selectAirplanes
binarizeAirplanes
calculateGeometricMoments
extractRotatedAirplanes
saveStraightAirplanes

◆ getBoundarySafeROI()

cv::Mat getBoundarySafeROI ( const cv::Mat & img,
cv::Rect & roi )

Extracts a region of interest (ROI) from an image, ensuring it stays within image boundaries.

This function checks if the specified ROI is within the boundaries of the given image. If the ROI is within boundaries, it returns the corresponding sub-image. If the ROI exceeds the image boundaries, it pads the image using reflection border, adjusts the ROI, and then extracts the sub-image.

Parameters
[in]imgThe input image from which the ROI is extracted.
[in,out]roiThe region of interest specified as a cv::Rect. If the ROI exceeds the image boundaries, it is adjusted to fit within the padded image.
Returns
A cv::Mat containing the extracted sub-image corresponding to the ROI.
See also
cv::copyMakeBorder
cv::Rect

◆ getValidInput()

std::string getValidInput ( const std::string & prompt,
const std::vector< std::string > & valid_inputs )

Prompts the user for input until a valid input is provided.

This function displays a prompt to the user and reads the user's input. It continues to prompt the user until a valid input from the provided list is entered.

Parameters
[in]promptThe message displayed to the user when asking for input.
[in]valid_inputsA vector of strings representing the valid inputs.
Returns
A string containing the valid input entered by the user.
Note
The function uses an unordered set to store the valid inputs for efficient lookup.
See also
std::unordered_set
std::vector

◆ saveAirplane()

void saveAirplane ( const cv::Mat & airplane,
int & count,
const std::string & img_filename,
const std::filesystem::path & straight_airplane_folder )

Saves an airplane image to a specified directory with a unique filename.

This function saves the provided airplane image to a specified directory. The filename is generated using a count and the original image filename.

Parameters
[in]airplaneThe cv::Mat object representing the airplane image to be saved.
[in,out]countAn integer counter used to generate unique filenames for the saved images. It is incremented with each call to the function.
[in]img_filenameThe original filename of the image, used as part of the new filename.
[in]straight_airplane_folderThe path to the directory where the image will be saved.
See also
cv::imwrite
std::filesystem::path

◆ saveStraightAirplanes()

void saveStraightAirplanes ( const std::vector< cv::Mat > & airplanes,
int & count,
const std::string & img_filename,
const std::filesystem::path & straight_airplanes_folder )

Displays and optionally saves a list of airplane images after user confirmation and optional rotation.

This function iterates through a list of airplane images, displaying each one to the user. The user can choose to save each image, optionally rotating it before saving. The saved images are stored in a specified directory with unique filenames.

Parameters
[in]airplanesA vector of cv::Mat objects representing the airplane images to be processed.
[in,out]countAn integer counter used to generate unique filenames for the saved images. It is incremented with each saved image.
[in]img_filenameThe original filename of the image, used as part of the new filename.
[in]straight_airplanes_folderThe path to the directory where the images will be saved.
Note
The function displays each image and prompts the user to decide whether to keep it. If the user chooses to keep an image, they can also choose to rotate it before saving.
See also
getValidInput
rotate90
cv::imshow
cv::waitKey
cv::imwrite

◆ selectAirplanes()

void selectAirplanes ( const cv::Mat & img,
const std::vector< cv::Rect > & yolo_boxes,
std::vector< cv::Rect > & selected_airplanes_yolo_boxes,
int & count,
const std::string & img_filename,
const std::filesystem::path & straight_airplanes_folder )

Selects and processes airplane images based on user input.

This function iterates through a list of YOLO bounding boxes, displays each corresponding airplane image to the user, and prompts the user to decide whether to process and select the airplane. The user can also choose to rotate and save the airplane image.

Parameters
[in]imgThe input image containing the airplanes.
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes for each airplane.
[out]selected_airplanes_yolo_boxesA vector of cv::Rect objects to store the selected YOLO bounding boxes for further processing.
[in,out]countAn integer counter used to generate unique filenames for the saved images. It is incremented with each saved image.
[in]img_filenameThe original filename of the image, used as part of the new filename.
[in]straight_airplanes_folderThe path to the directory where the processed images will be saved.
Note
The function displays each airplane image and prompts the user to decide whether to process it.
If the user chooses to process an airplane, it is added to the selected_airplanes_yolo_boxes vector.
If the user chooses to rotate and save an airplane, it is rotated by a multiple of 90 degrees clockwise and saved.
See also
getValidInput
rotate90
saveAirplane
cv::imshow
cv::waitKey
cv::Rect