HandDetection is a light weighted C++ library to track hands using OpenCV.
C++
(11 or above) and OpenCV
(2 or above) installed.
The library using background subtraction method to detect hand(s) to do the tracking. The pictures below show an instance of the frame and foreground from the library.
Current Frame | Foreground Frame |
---|---|
With clear and clean foreground images, accurate analysis can be done. For example, finger tip locations, palm center, movement, etc, endless things can be acheived.
Compare to pure skin color detection, it is more adaptable, as it does not need to be calibrated to adapt to different skin colors and surrounding. Therefore, as long as the background is stationary, the background subtraction will always work like a charm.
HandDetection class so far provides the following functions:
start()
- start the detectorstop()
- stop the detectorgetFrame()
- get the current frame from webcamgetBackground()
- get the current background frame by using background subtractiongetForeground()
- get the current foreground frame by using background subtractiongetFingers()
- get the number of fingers are currently upgetDirection()
- get the current hand moving direction (beta)
main.cpp
already shown an example usage of the HandDetection class, but the following code snippet is provided to give a more comprehensive example:
#include <thread>
#include "hand_detection.h"
using namespace std;
int main(int argc, char *argv[]) {
HandDetection handDetector;
// always put start() in another thread
thread t(&HandDetection::start, &handDetector);
namedWindow("Frame");
namedWindow("Foreground");
namedWindow("Background");
while(1) {
imshow("Frame", handDetector.getFrame());
imshow("Foreground", handDetector.getForeground());
imshow("Background", handDetector.getBackground());
cout << "Finger Number: " << handDetector.getFingers() << endl;
cout << "Moving Ditection: " << handDetector.getDirection() << endl;
if (waitKey(10) >= 0) {
handDetector.stop();
break;
}
}
t.join();
return 0;
}
Enjoy ใฝ(^o^)ใ