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

Functions

std::vector< cv::Mat > loadAvgPlanes ()
 Loads average airplane images from the specified directory.
 
std::vector< int > angle_range (int start, int end, int step)
 Generates a range of angles from start to end with a specified step.
 
cv::Mat rotateImage (const cv::Mat &src_img, int degree_angle)
 Rotates an image by a specified angle.
 
cv::Point transformPoint (const cv::Point &match_center, const cv::Mat &rotation_mat)
 Transforms a point using the inverse of a given affine transformation matrix.
 
std::vector< cv::Point > performTemplateMatching (const cv::Mat &src_img, const cv::Mat &avg_plane, int degree_angle)
 Performs template matching on a source image with a rotated template.
 
std::vector< cv::Point > matchTemplateMultiThreaded (const cv::Mat &src_img, const std::vector< cv::Mat > &avg_planes)
 Performs multi-threaded template matching on a source image using multiple average planes.
 
std::vector< cv::Point > templateMatching (const cv::Mat &src_img)
 Performs template matching on a source image using pre-loaded average planes.
 

Function Documentation

◆ angle_range()

std::vector< int > angle_range ( int start,
int end,
int step )

Generates a range of angles from start to end with a specified step.

This function creates a vector of integers representing angles starting from start, incremented by step, and ending before end.

Parameters
[in]startThe starting angle.
[in]endThe ending angle (exclusive).
[in]stepThe step size between consecutive angles.
Returns
A vector of integers representing the range of angles.

◆ loadAvgPlanes()

std::vector< cv::Mat > loadAvgPlanes ( )

Loads average airplane images from the specified directory.

This function reads all PNG images from the "avg_airplanes" directory within the source directory and loads them into a vector of cv::Mat objects.

Returns
A vector of cv::Mat objects containing the loaded average airplane images.
Note
The function assumes that the average airplane images are stored in the "avg_airplanes" directory within the source directory defined by SRC_DIR_PATH.
Only images that are successfully read are added to the vector.
See also
globFiles
cv::imread

◆ matchTemplateMultiThreaded()

std::vector< cv::Point > matchTemplateMultiThreaded ( const cv::Mat & src_img,
const std::vector< cv::Mat > & avg_planes )

Performs multi-threaded template matching on a source image using multiple average planes.

This function performs template matching on a source image using a set of average planes, rotating each plane by various angles. It uses multi-threading to parallelize the matching process, combining the results into a single list of matched points.

Parameters
[in]src_imgThe source image in which to perform template matching.
[in]avg_planesA vector of cv::Mat objects representing the average planes used for matching.
Returns
A vector of cv::Point objects representing the coordinates of all matched points.
Note
The function uses a step of 5 degrees for rotating the average planes.
The function uses std::async with std::launch::async to perform template matching in parallel.
The function collects and combines the results from all threads.
See also
performTemplateMatching
angle_range
std::async
std::shared_future

◆ performTemplateMatching()

std::vector< cv::Point > performTemplateMatching ( const cv::Mat & src_img,
const cv::Mat & avg_plane,
int degree_angle )

Performs template matching on a source image with a rotated template.

This function rotates the source image by a specified angle, performs template matching using the normalized cross-correlation method, and returns the coordinates of the matched points transformed back to the original image coordinates.

Parameters
[in]src_imgThe source image in which to perform template matching.
[in]avg_planeThe template image used for matching.
[in]degree_angleThe angle in degrees by which to rotate the source image for matching.
Returns
A vector of cv::Point objects representing the coordinates of the matched points in the original image.
Note
The function uses cv::getRotationMatrix2D to compute the rotation matrix and rotateImage to rotate the source image.
The function uses cv::matchTemplate with the cv::TM_CCOEFF_NORMED method to perform template matching.
The function uses transformPoint to transform the coordinates of the matched points back to the original image coordinates.
See also
cv::getRotationMatrix2D
rotateImage
cv::matchTemplate
cv::minMaxLoc
transformPoint

◆ rotateImage()

cv::Mat rotateImage ( const cv::Mat & src_img,
int degree_angle )

Rotates an image by a specified angle.

This function rotates the given image by a specified angle around its center. It adjusts the bounding box to ensure the entire rotated image fits within the resulting image.

Parameters
[in]src_imgThe source image to be rotated.
[in]degree_angleThe angle in degrees by which the image should be rotated.
Returns
A cv::Mat object containing the rotated image.
Note
The function uses the center of the image as the rotation point and adjusts the translation to ensure the entire rotated image fits within the new bounding box.
See also
cv::getRotationMatrix2D
cv::warpAffine
cv::RotatedRect

◆ templateMatching()

std::vector< cv::Point > templateMatching ( const cv::Mat & src_img)

Performs template matching on a source image using pre-loaded average planes.

This function loads a set of average planes and performs multi-threaded template matching on the source image. It returns the coordinates of all matched points found in the image.

Parameters
[in]src_imgThe source image in which to perform template matching.
Returns
A vector of cv::Point objects representing the coordinates of all matched points.
Note
The function uses loadAvgPlanes to load the average planes from the predefined directory.
The function uses matchTemplateMultiThreaded to perform multi-threaded template matching.
See also
loadAvgPlanes
matchTemplateMultiThreaded

◆ transformPoint()

cv::Point transformPoint ( const cv::Point & match_center,
const cv::Mat & rotation_mat )

Transforms a point using the inverse of a given affine transformation matrix.

This function takes a point and an affine transformation matrix, computes the inverse of the matrix, and applies it to the point to obtain its transformed coordinates.

Parameters
[in]match_centerThe point to be transformed.
[in]rotation_matThe affine transformation matrix to be inverted and applied to the point.
Returns
A cv::Point representing the transformed coordinates of the input point.
Note
The function uses cv::invertAffineTransform to compute the inverse of the affine transformation matrix.
The function uses cv::transform to apply the inverted matrix to the point.
See also
cv::invertAffineTransform
cv::transform