Simple OpenCV Demos
Published:
I posted several tutorials for OpenCV beginners on my WeChat Public Account. I’ve found that providing simple demos can help readers a lot to understand the use of OpenCV.
You can get the code from my Github.
This post is a brief summary of the demos.
Calculate image convolution using DFT
Convolution can be efficiently calculated in frequency domain. In fact, all OpenCV Image Filter API perform their calculations this way.
In this demo I implement this myself. The result may not be exactly the same as what you get when you use Image Filter API directly, because OpenCV has some extra operations.
You need some basic knowledge of DSP in order to understand the code.
The result is shown below:
figure #1: original image; figure #2: convolution result
SURF & SIFT Feature Detect
SURF feature & SIFT feature detector is included in opencv_contrib modules. Therefore, you may find the implementation quite different from other Opencv project.
- Remember to use the namespace cv::xfeatures2d rather than cv. Many other modules in opencv_contrib also have their own namespaces. Pay attention to this in other demos.
- In this implementation, we first define two structure, rather than use the Opencv class & function directly. The reason is that the corresponding classes are defined as abstract classes, thus cannot be used directly.
detection result (figure #1: SURF, figure #2: SIFT):
Flood Fill algorithm
Remember the Fill tool in Windows Paint Application? It can paint a connected region in the image with one color. This looks like:
Figure #2 is created based on Flood Fill algorithm. The API in OpenCV also returns a mask which indicates the painted region.
Please refer to the code and OpenCV documents for more details.
Histogram Calculation
This demo calculate & display the histogram of the input image.
figure #1: input image, figure #2-4: histogram of channel R, G, B, respectively.
Histogram Back-projection
This demo is a simple use of histogram backprojection to detect an monochromatic (or nealy monochromatic) object. I provide one test image with the sample patch extracted from it. Be free to use you own test images! You may be wondering how to use you camera to run histogram back-projection online. We’ll see how to do this in the following demos (refer to V4).
You may find out that the outcome is very upsetting. We’ll see the reason and try to modify it in demo ‘V2’.
In this demo, HSV color space is used to improve the performance in V1.
The result is shown below.
This demo has the following modifications based on HistBackProject V2.
- Morphology operation is used to enhance the target object in the binary image.
- Extra contours and calculate bounding box.
The result is shown below.
This demo run Histogram Back-projection online, i.e. capture video frames via the camera on you computer and detect the target.
Follow the steps:
- Use your mouse to select the target region.
- When you are done with step 1, press ‘Y’ and watch the object detection result.
- Press ‘Q’ at any time to quit.
Pedestrian detection
This is a fairly simple demo. The result is shown below.
LSD Line Segment Detector
LSD is an algorithm proposed in this IEEE paper.
This algorithm is powerful, but sometimes too ‘powerful’ to eliminate tiny line elements in an image. This problem is shown below.
In this demo, I filter the detection result by eliminating lines whose length are less than the threshold. When setting threshold to be 30, the result looks much better.
Object Tracking
There are 5 tracking algorithms in OpenCV Tracking API. You can select one via command line parameters.
Running this demo needs extra parameters from the command line:
./opencv_exp trackerName {-vid | -img} { <video filename> | <dir name>
The parameter ‘trackerName’ can be one of the following:
- Boosting
- KCF
- MedianFlow
- MIL
- TLD
For example
to use KCF tracker & video file:
./opencv_exp KCF -vid test.mp4
to use KCF tracker & image sequence:
./opencv_exp KCF -img test