Hand detection using convex hull [closed]

asked 2016-03-04 04:25:38 -0600

Lily gravatar image

updated 2016-03-04 04:43:15 -0600

I am trying to implement a hand recognition system using convex hull. I tried to detect the hand using biggest contour but it is getting the whole frame as biggest contour instead of the hand itself. Help !

#include "stdafx.h"
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\video\background_segm.hpp>
#include <opencv2\opencv.hpp>
#include "opencv2/objdetect.hpp"
#include < stdio.h>  
#include <iostream>

using namespace cv;
using namespace std;


const string trackbarWindowName = "Trackbars"; 
int H_MIN = 0;
int H_MAX = 256;
int S_MIN = 0;
int S_MAX = 256;
int V_MIN = 0;
int V_MAX = 256;

int findBiggestContour(vector<vector<Point> > contours);
int IndexOfBiggestContour;

void on_trackbar( int, void* )
{//This function gets called whenever a
    // trackbar position is changed


}

void createTrackbars(){

    namedWindow(trackbarWindowName,0);
    //create memory to store trackbar name on window
    char TrackbarName[50];
    sprintf( TrackbarName, "H_MIN", H_MIN);
    sprintf( TrackbarName, "H_MAX", H_MAX);
    sprintf( TrackbarName, "S_MIN", S_MIN);
    sprintf( TrackbarName, "S_MAX", S_MAX);
    sprintf( TrackbarName, "V_MIN", V_MIN);
    sprintf( TrackbarName, "V_MAX", V_MAX);
    //create trackbars and insert them into window
    //3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
    //the max value the trackbar can move (eg. H_HIGH), 
    //and the function that is called whenever the trackbar is moved(eg. on_trackbar)
    //                                  ---->    ---->     ---->      
    createTrackbar( "H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar );
    createTrackbar( "H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar );
    createTrackbar( "S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar );
    createTrackbar( "S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar );
    createTrackbar( "V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar );
    createTrackbar( "V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar );


}


int _tmain(int argc, _TCHAR* argv[])
{
    VideoCapture cap("pathaka.MP4");  
    Mat frame(Size(640, 420),CV_8UC3);
    Mat frame2(Size(640, 420),CV_8UC3);
    createTrackbars();
    if(!cap.isOpened())   
        return -1;

    while(true){
        cap>>frame;
        cap>>frame2;
        //blur( frame, frame, Size(3,3) );

        Size kSize;
        kSize.height = 3;
        kSize.width = 3;
        double sigma = 0.3*(3/2 - 1) + 0.8;
        GaussianBlur(frame,frame,kSize,sigma,0.0,4);

        Mat hsv(Size(640, 420),CV_8UC3);
        cvtColor(frame,hsv,CV_BGR2YCrCb);

        Mat bw(Size(640, 420),CV_8UC1);
        inRange(hsv,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),bw);  


        vector<Vec4i> hierarchy;
        vector<vector<Point> > contours_hull;


        Mat Erode(Size(640, 420),CV_8UC1);
        cv::erode(bw, Erode, cv::Mat(), cv::Point(-1,-1));

        Mat Dialate(Size(640, 420),CV_8UC1);


        cv::dilate(Erode, Dialate, cv::Mat(), cv::Point(-1,-1),2);


        findContours(Dialate.clone(), contours_hull, hierarchy, CV_RETR_TREE , CV_CLOCKWISE, Point(0, 0) ); // CV_CHAIN_APPROX_SIMPLE

        if(contours_hull.size() > 0)
        {
            /// Find the convex hull object for each contour
            vector<vector<Point> >hull( contours_hull.size() );
            //find the defects points for each contour
            vector<vector<Vec4i>> defects( contours_hull.size()) ;

            vector<vector<int> > hullsI(contours_hull.size());

            //find the biggest contour
            IndexOfBiggestContour = findBiggestContour(contours_hull);

            Point2f rect_points[4];  
            vector<RotatedRect> minRect( contours_hull.size() );

            vector<vector<Point> > contours_poly( contours_hull.size() );
            vector<Rect> boundRect( contours_hull.size() );


            try{
                for( int i = 0; i < contours_hull.size(); i++ )
                { 
                    convexHull( Mat(contours_hull[i]), hull[i], false );
                    convexHull( Mat(contours_hull[i]), hullsI[i], false );
                    convexityDefects(Mat(contours_hull[i]),hullsI[i], defects[i]);

                    if(IndexOfBiggestContour == i)
                    {
                        minRect[i] = minAreaRect( Mat(contours_hull[i]) );

                        drawContours( frame2, contours_hull,IndexOfBiggestContour, CV_RGB ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-26 08:09:24.734730

Comments

Please, your question itself should go into the body of the topic, not into the title ... please adapt ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-03-04 04:36:47 -0600 )edit
1

Oh i didn't realise .. I edited the question ...

Lily gravatar imageLily ( 2016-03-04 04:44:03 -0600 )edit