Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to find Finger tips out of convexity defects

Hello, I have created a program and I am successfully able to detect convex hull and convexity defects but I am unable to find finger tips reliably. I am too much exhausted now. HELP ME PLEASE 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);
                    }
                }
                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;
}

How to find Finger tips out of convexity defects

Hello, I have created a program and I am successfully able to detect convex hull and convexity defects (defects Check this out https://vine.co/v/OLA5d0jQd00 ) but I am unable to find finger tips reliably. I am too much exhausted now. HELP ME PLEASE 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);
                    }
                }
                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;
}