How to count number of fingers out of convexity defects in C++ OpenCV

1k Views Asked by At

I have created a program in C++ with OpenCV which is now able to recognize skin and successfully creating convex hull and convexity defects. Now I want to count the number of fingers and also is there any way to improve this code ?
What I am doing here is to find the size of hull points but it is not giving me accurate result :(

I am unable to find a way to count the number of fingers, can you please help me on how to count them.

Here is my code

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv/cxcore.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;

void DetectContour(Mat img) {
    Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
    vector<vector<Point> > contours;
    vector<vector<Point> > bigContours;
    vector<Vec4i> hierarchy;

    findContours(img,contours, hierarchy, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE, Point());

    if(contours.size() > 0) {
        vector<vector<int> >hull( contours.size() );
        vector<vector<Vec4i> > convDef(contours.size() );
        vector<vector<Point> > hull_points(contours.size());
        vector<vector<Point> > defect_points(contours.size());


        for( int i = 0; i < contours.size(); i++ ) {
            if(contourArea(contours[i]) > 5000) {
                convexHull( contours[i], hull[i], false );
                convexityDefects( contours[i],hull[i], convDef[i]);

                for(int k=0; k < hull[i].size() ; k++) {
                    int ind=hull[i][k];
                    hull_points[i].push_back(contours[i][ind]);
                }

                for(int k=0;k<convDef[i].size();k++) {
                    if( convDef[i][k][3] > 20*256 ) { // filter defects by depth
                        int ind_0=convDef[i][k][0];
                        int ind_1=convDef[i][k][1];
                        int ind_2=convDef[i][k][2];
                        defect_points[i].push_back(contours[i][ind_2]);
                        circle(drawing,contours[i][ind_0],5,Scalar(0,255,0),-1);
                        circle(drawing,contours[i][ind_1],5,Scalar(0,255,0),-1);
                        circle(drawing,contours[i][ind_2],5,Scalar(0,0,255),-1);
                        line(drawing,contours[i][ind_2],contours[i][ind_0],Scalar(0,0,255),1);
                        line(drawing,contours[i][ind_2],contours[i][ind_1],Scalar(0,0,255),1);


                        namedWindow("test",CV_WINDOW_AUTOSIZE);

                        Mat pic = Mat::zeros(250,250,CV_8UC3);
                        if(hull_points.size() <= 2)
                            putText(pic, "0",Point(50,50), CV_FONT_HERSHEY_SIMPLEX, 2, Scalar(255),3,8,false);
                        else if(hull_points.size() <= 3)
                            putText(pic,"1",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        else if(hull_points.size() <= 5)
                            putText(pic,"2",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        else if(hull_points.size() <= 7)
                            putText(pic,"3",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        else if(hull_points.size() <= 9)
                            putText(pic,"4",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        else if(hull_points.size() <= 11)
                            putText(pic,"5",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        else
                            putText(pic,"OUT OF RANGE",Point(50,50),CV_FONT_HERSHEY_SCRIPT_SIMPLEX,2,Scalar(255),3,8,false);
                        imshow("test",pic);


                    }
                }

                drawContours( drawing, contours, i, Scalar(0,255,0), 1, 8, vector<Vec4i>(), 0, Point() );
                drawContours( drawing, hull_points, i, Scalar(255,0,0), 1, 8, vector<Vec4i>(), 0, Point() );
            }
        }
    }
    imshow( "Hull demo", drawing );
}

int main(void) {

    int lh = 0;
    int hh = 179;
    int ls = 0;
    int hs = 255;
    int lv = 0;
    int hv = 255;

    namedWindow("HSV",CV_WINDOW_AUTOSIZE);
    createTrackbar("LH","HSV",&lh,179);
    createTrackbar("HH","HSV",&hh,179);
    createTrackbar("LS","HSV",&ls,255);
    createTrackbar("HS","HSV",&hs,255);
    createTrackbar("LV","HSV",&lv,255);
    createTrackbar("HV","HSV",&hv,255);

    Mat img;
    VideoCapture cap(0);
    int th = 100;
    while(cap.read(img)) {

        Mat frame;
        cvtColor(img,frame,CV_BGR2HSV);
        inRange(frame,Scalar(lh,ls,lv),Scalar(hh,hs,hv),frame);

        threshold(frame,frame,th,2*th+55,THRESH_OTSU);


        DetectContour(frame);

        if(waitKey(5) == 27)
            break;
    }

    destroyAllWindows();

    return 0;
}
0

There are 0 best solutions below