need help hand tracking mouse

asked 2015-08-11 09:00:40 -0600

hello guys, I'm working on hand recognition method with finger counting and decide to add a trackbar for inputing the value that determine how the mouse action react when the hand shown the value that has been set on input trackbar.

image description

but no matter how much value i set on trackbar will always do left click, even i set different value

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "Windows.h"

// C
#include <stdio.h>
// C++
#include <iostream>
#include <sstream>

using namespace cv;
using namespace std;


// Function Headers
void createTrackbars();
string intToString(int number);
void writeScreen(Mat &screen, string String);
void MoveMouse(LONG X, LONG Y);
void DoMouseClick();
void DoRightClick();
void ClickPressed();
void ReleaseClick();
void convertSize (int &x, int &y);
void writeDesc(Mat &screen2);
void center_circle();


//              Variables
// Matrix variables
Mat frame, ycrcb, Output;

//initial min and max ycrcb filter values.
//these will be changed using trackbars
int Y_MIN = 0;
int Y_MAX = 255;
int Cb_MIN = 131;
int Cb_MAX = 185;
int Cr_MIN = 80;
int Cr_MAX = 135;
int THRESH_MIN = 0;
int THRESH_MAX = 255;
int jari1 = 1;
int jari5 = 5;
int kiri =0;
int kanan = 0;
int tengah = 0;
int hitung = 0;
int jumlah;


//rectangle utk screen area buat mouse area
        int scrX = 10;
        int scrY = 10;
//for 3x times scale
        int scrW = 455;
        int scrH = 256;


// contours variables
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int countDefects = 0;
bool klikmouse = false;
//names that will appear at the top of each window
const string trackbarWindowName = "Trackbars";
void on_trackbar2( int, void*)
{

}

int main( int argc, char* arg[] )
{
    // Set video source
    VideoCapture cap(0);

    int key;
    RNG rng(12345);

    if(!cap.isOpened()){

        cout << "Error"; return -1;
    }

    while(true){
        // Get frame from the screen
        cap.read(frame);
        flip(frame,frame,1);

        // Convert it to ycrcb format
        cvtColor(frame, ycrcb, COLOR_BGR2YCrCb);

        // Filter ycrcb image between values and store it in other matrix
        inRange( ycrcb, Scalar( Y_MIN, Cb_MIN, Cr_MIN ), Scalar( Y_MAX, Cb_MAX, Cr_MAX ),
                Output);

        // Detect edges using Threshold
        threshold( Output, Output, THRESH_MIN, THRESH_MAX, THRESH_BINARY );

        // Find contours
        findContours( Output.clone(), contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE,
                        Point( 0, 0 ) );

        int largestPos = 0;     // Largest contour position
        int largestArea = 0;    // largestArea of this contour
        int x,y;                // largest contour's x and y coordinate

        vector< vector<Point> > hull( contours.size());
        vector< vector<int> > hullsI( contours.size());
        vector< vector<Vec4i> >defects(contours.size());
        vector<Point2f>center( contours.size() );
        vector<vector<Point> > contours_poly( contours.size() );
        vector<Point2f> lingkar(contours.size());


    namedWindow(trackbarWindowName);
    createTrackbars();
    //tarckbar jari
    namedWindow( "Konfigurasi Inputan Mouse", 1 );

    createTrackbar("Klik kiri", "Konfigurasi Inputan Mouse", &kiri, 5,on_trackbar2 );
    createTrackbar("Klik kanan", "Konfigurasi Inputan Mouse", &kanan, 5,on_trackbar2 );
    createTrackbar("Scroll", "Konfigurasi Inputan Mouse", &tengah, 5, on_trackbar2 );

        // Draw contours
        Mat drawing = Mat::zeros( Output.size(), CV_8UC3 );

        for( int i = 0; i < contours.size(); i++ )
        {
            Moments moment = moments( (Mat)contours[i] );
            double area = moment.m00;

            convexHull( Mat(contours[i]), hull[i], false );
            convexHull( Mat(contours[i]), hullsI[i], false );

            // If major area and major then minimum required
            if( area > largestArea)
            {
                largestArea = area;
                // Get x,y coordinates
                x = moment.m10 ...
(more)
edit retag flag offensive close merge delete