Pyramid search matching based on edge gradient cosine similarity
This project provides functionality for template matching using OpenCV. It includes shared libraries for template creation and finding, as well as executables for training and inference.
The similarity is defined as:
Split into normalized vectors:
Then, the cosine similarity becomes:
Performance Tests (CPU I7-10700)
Note
Some pictures are sourced from NCC
The project directory structure is as follows:
.
├── 3rdparty
│ └── opencv # OpenCV dependencies
│ ├── bin # Executable binaries
│ ├── include # OpenCV headers
│ ├── lib # OpenCV libraries
│ └── share # OpenCV share files
├── assert # Test images for matching
├── build_opencv_with_contrib.sh # Script to build OpenCV with contrib modules
├── CMakeLists.txt # CMake build configuration file
├── include # Project header files
│ ├── FindTemplateV1.h
│ ├── MakeTemplateV1.h
│ ├── ROI.h
│ ├── Timer.h
│ └── Type.h
├── inference.cpp # Inference executable source code
├── LICENSE
├── README.md
├── src # Source code for the libraries
│ ├── FindTemplateV1.cpp
│ └── MakeTemplateV1.cpp
└── train.cpp # Training executable source codeEnsure that you have the following installed in the Linux:
CMake(version 3.10 or higher)g++compiler (with support for C++11)OpenCV 4.7.0(with contrib modules, optionally)
To build OpenCV with contrib modules, use the provided build_opencv_with_contrib.sh script.
if you cmake OpenCV 4.7.0 with contrib modules failed, you can download .cache.zip file from the following link:
https://wwyn.lanzout.com/iB7Mb34k8kwd and extract it to the your_opencv_source_dir/.cache directory.
-
Open a terminal and navigate to the project root directory.
-
Run the script:
./build_opencv_with_contrib.sh
This will download and build OpenCV4.7.0 along with the contrib modules, placing the necessary files under the 3rdparty/opencv directory.
-
Create a build directory:
mkdir build cd build -
Run
CMaketo configure the project:cmake ..
-
Build the project using
make:make -j$(nproc)
After building the project, you can run the executables:
-
To run the
trainingexecutable:./train
or
./train ../assert/m1.png
-
To run the
inferenceexecutable:./inference
or
./inference ../assert/src.bmp
If you prefer to compile using g++ directly instead of using CMake, you can compile the source code manually.
-
Compile the libraries:
- For compiling the shared library
libMakeTemplateV1.so
g++ -std=c++11 -O3 -fopenmp -fPIC -march=native -msse -msse2 -msse3 -msse4 -mavx -o libMakeTemplateV1.so -shared src/MakeTemplateV1.cpp \ -I./include \ -I./3rdparty/opencv/include \ -L./3rdparty/opencv/lib \ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_calib3d \ -pthread
- For compiling the shared library
libFindTemplateV1.so
g++ -std=c++11 -O3 -fopenmp -fPIC -march=native -msse -msse2 -msse3 -msse4 -mavx -o libFindTemplateV1.so -shared src/FindTemplateV1.cpp \ -I./include \ -I./3rdparty/opencv/include \ -L./3rdparty/opencv/lib \ -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_calib3d \ -pthread
- For compiling the shared library
-
Compile the executables:
- For the
trainexecutable
g++ train.cpp src/MakeTemplateV1.cpp -std=c++11 -O3 -fopenmp -Iinclude -I3rdparty/opencv/include -I3rdparty/opencv/include/opencv4 -L3rdparty/opencv/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_calib3d -lopencv_videoio -lopencv_ximgproc -lopencv_xfeatures2d -o train
- For the
inferenceexecutable
g++ inference.cpp src/FindTemplateV1.cpp -std=c++11 -O3 -fopenmp -Iinclude -I3rdparty/opencv/include -I3rdparty/opencv/include/opencv4 -L3rdparty/opencv/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_calib3d -lopencv_videoio -lopencv_ximgproc -lopencv_xfeatures2d -o inference
- For the
After compilation, you can run the executables as follows:
-
To run the
trainingexecutable:./train assert/m1.png
-
To run the
inferenceexecutable:./inference assert/src.bmp
To install the libraries and header files to the system directories:
-
Run the following command to install:
make install
This will copy the shared libraries and headers to the appropriate system directories.
- The shared libraries will be installed in the
publish/libdirectory. - The executables will be installed in the
publish/bindirectory. - The header files will be installed in the
publish/include/shapeMatchdirectory.
This project is licensed under the MIT License - see the LICENSE file for details.




