C++ application that locates objects in a photo by matching their contours against a reference shape.
A pattern image is loaded first and its contour extracted. Every photo in data/photos/ is then scanned for contours, and each candidate is compared against the pattern using OpenCV's matchShapes, which is based on Hu moment invariants. Because those moments are invariant to translation, scale and rotation, the same object is recognised regardless of where it sits in the frame or how it is turned.
- Load
data/pattern/pattern.jpgand extract its contour. - Iterate over every
.jpgindata/photos/. - Extract all contours from the photo.
- Compare each one against the pattern with
matchShapes(CV_CONTOURS_MATCH_I1). - Display the contours scoring below the match threshold, each in its own window.
The score is printed for every candidate, so the threshold — currently 0.05 — can be tuned by watching the output.
- OpenCV 2.x. The code uses pre-3.0 constants such as
CV_WINDOW_AUTOSIZEandCV_CONTOURS_MATCH_I1. - Windows and Visual Studio. Directory traversal uses
<io.h>and_findfirst, which are MSVC-specific. The OpenCV parts themselves are portable.
Open openCVContourDetection.sln, point the project at your OpenCV include and lib directories, and build.
- Windows-only, because of the file-listing code.
- The match threshold is hard-coded.
- Source comments are in Polish.