Aircraft Detection 1.0
Loading...
Searching...
No Matches
svm_training.cpp File Reference
#include "svm_training.h"
#include "utils.h"
#include "hog_features_extraction.h"
#include "template_matching.h"
#include <random>
#include <filesystem>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>

Functions

void classifyPointsByYoloBoxes (const std::vector< cv::Rect > &yolo_boxes, const std::vector< cv::Point > &max_corr_points, std::vector< cv::Point > &max_corr_points_inside_yolo, std::vector< cv::Point > &max_corr_points_outside_yolo)
 Classifies points based on whether they fall inside or outside of YOLO bounding boxes.
 
std::vector< cv::Rect > selectROIsWithHighestIoU (const std::vector< std::pair< cv::Rect, std::vector< cv::Rect > > > &yoloBox_roi_pairs)
 Selects the Regions of Interest (ROIs) with the highest Intersection over Union (IoU) for each YOLO bounding box.
 
std::vector< std::pair< cv::Rect, std::vector< cv::Rect > > > associateYoloBoxesWithRois (const std::vector< cv::Rect > &yolo_boxes, const std::vector< cv::Point > &max_corr_tp)
 Associates YOLO bounding boxes with Regions of Interest (ROIs) generated from points.
 
void generateSvmTrainingData ()
 Generates SVM training data by extracting HOG features and saving them to CSV files.
 
std::unordered_map< int, std::vector< cv::Point > > groupPointsByYoloBox (const std::vector< cv::Rect > &yolo_boxes, const std::vector< cv::Point > &points)
 Groups points by the YOLO bounding box they fall into.
 

Function Documentation

◆ associateYoloBoxesWithRois()

std::vector< std::pair< cv::Rect, std::vector< cv::Rect > > > associateYoloBoxesWithRois ( const std::vector< cv::Rect > & yolo_boxes,
const std::vector< cv::Point > & max_corr_tp )

Associates YOLO bounding boxes with Regions of Interest (ROIs) generated from points.

This function associates each YOLO bounding box with a set of ROIs generated from points that fall within the bounding box. The ROIs are generated using predefined sizes calculated from clusters.

Parameters
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes.
[in]max_corr_tpA vector of cv::Point objects representing the points to be associated with ROIs.
Returns
A vector of pairs, where each pair consists of a YOLO bounding box (cv::Rect) and a vector of associated ROIs (cv::Rect).
Note
The function groups points by their corresponding YOLO bounding box and generates ROIs for each group of points. The ROIs are generated using the sizes calculated from the clusters in the "kmeans_by_size" directory.
See also
groupPointsByYoloBox
listDirectories
calculateAvgDims
generateRoisFromPoints

◆ classifyPointsByYoloBoxes()

void classifyPointsByYoloBoxes ( const std::vector< cv::Rect > & yolo_boxes,
const std::vector< cv::Point > & max_corr_points,
std::vector< cv::Point > & max_corr_points_inside_yolo,
std::vector< cv::Point > & max_corr_points_outside_yolo )

Classifies points based on whether they fall inside or outside of YOLO bounding boxes.

This function iterates through a list of points and classifies each point as either inside or outside any of the provided YOLO bounding boxes. Points that fall inside any of the YOLO bounding boxes are added to the max_corr_points_inside_yolo vector, while points that fall outside are added to the max_corr_points_outside_yolo vector.

Parameters
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes.
[in]max_corr_pointsA vector of cv::Point objects representing the points to be classified.
[out]max_corr_points_inside_yoloA vector to store points that fall inside any YOLO bounding box.
[out]max_corr_points_outside_yoloA vector to store points that fall outside all YOLO bounding boxes.

◆ generateSvmTrainingData()

void generateSvmTrainingData ( )

Generates SVM training data by extracting HOG features and saving them to CSV files.

This function processes a dataset of images and their corresponding YOLO labels to generate training data for an SVM. It performs template matching, classifies points based on their location relative to YOLO bounding boxes, extracts HOG features for true positives and false positives, and saves the features to CSV files.

The function performs the following steps:

  1. Lists directories for k-means clustering by size and calculates average dimensions for ROIs.
  2. Reads dataset image paths and YOLO label paths.
  3. Reads images in grayscale.
  4. Iterates through each image in the dataset: a. Performs template matching. b. Reads YOLO bounding boxes. c. Classifies points inside and outside YOLO boxes. d. Filters points outside YOLO boxes by minimum distance. e. Associates each YOLO box with ROIs extracted from points inside it. f. Selects ROIs with the highest Intersection over Union (IoU) for true positives. g. Extracts ROIs for false positives. h. Extracts HOG features for true positives and false positives. i. Stores the HOG features in vectors, reserving space to minimize reallocations.
  5. Saves the HOG features to CSV files for SVM training.
Note
The function assumes that the dataset images and YOLO label files are in the specified directory.
The function initializes a random number generator for choosing random ROI sizes to extract false positives.
See also
listDirectories
calculateAvgDims
globFiles
readImages
templateMatching
readYoloBoxes
classifyPointsByYoloBoxes
filterPointsByMinDistance
associateYoloBoxesWithRois
selectROIsWithHighestIoU
hog_features_extraction
writeHogFeaturesToCsv

◆ groupPointsByYoloBox()

std::unordered_map< int, std::vector< cv::Point > > groupPointsByYoloBox ( const std::vector< cv::Rect > & yolo_boxes,
const std::vector< cv::Point > & points )

Groups points by the YOLO bounding box they fall into.

This function iterates through a list of points and groups them by the YOLO bounding box they fall into. Each YOLO bounding box is associated with a vector of points that are contained within it.

Parameters
[in]yolo_boxesA vector of cv::Rect objects representing the YOLO bounding boxes.
[in]pointsA vector of cv::Point objects representing the points to be grouped.
Returns
An unordered map where the key is the index of the YOLO bounding box and the value is a vector of cv::Point objects that fall within that bounding box.

◆ selectROIsWithHighestIoU()

std::vector< cv::Rect > selectROIsWithHighestIoU ( const std::vector< std::pair< cv::Rect, std::vector< cv::Rect > > > & yoloBox_roi_pairs)

Selects the Regions of Interest (ROIs) with the highest Intersection over Union (IoU) for each YOLO bounding box.

This function iterates over pairs of YOLO bounding boxes and associated ROIs, and selects the ROI with the highest IoU for each YOLO bounding box. The selected ROIs are returned in a vector.

Parameters
[in]yoloBox_roi_pairsA vector of pairs, where each pair consists of a YOLO bounding box (cv::Rect) and a vector of associated ROIs (cv::Rect).
Returns
A vector of cv::Rect objects representing the ROIs with the highest IoU for each YOLO bounding box.
Note
The function calculates the IoU for each ROI and selects the ROI with the maximum IoU for each YOLO bounding box. If no valid ROI is found for a YOLO bounding box, that box is skipped.