Why does it keep switching between states?

I am using openCV to per for head tracking using lukas kanade optical tracking.. The method i am using it is not problem, is how it is applied that is problem.. so here is the code.

Here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Rect Facerecognizer(Mat now_frame)
{
    Mat prev, next;
    cvtColor(now_frame, next, CV_BGR2GRAY);
    equalizeHist(next,next);
    Point center_of_frame(now_frame.size().width/2,now_frame.size().height/2);
    circle(now_frame, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
    String face_cascade_XML = "/Users/x/Downloads/opencv-master/data/haarcascades/haarcascade_frontalface_alt2.xml";
    CascadeClassifier face_cascade;
    vector<Rect> faces(1);
    if( !face_cascade.load( face_cascade_XML ) ){
        cout << "Cascade Error" << endl;
    };
    face_cascade.detectMultiScale( now_frame, faces, 1.1, 2, CASCADE_DO_CANNY_PRUNING, Size(300, 300) );
    
    if (!faces.empty()) {
        for( auto faces : faces)
        {
            Point center_position_of_face((faces.br().x+faces.tl().x)/2,(faces.tl().y+faces.br().y)/2);
            Point corner_1(faces.br().x,faces.tl().y);
            Point corner_2 = faces.tl();
            Point corner_3 = faces.br();
            Point corner_4(faces.tl().x,faces.br().y);
            rectangle(now_frame, faces, CV_RGB(0,255,0),4,8,0);
            circle(now_frame, center_position_of_face, 8, CV_RGB(128,128,128),8,8,0);
            circle(now_frame, corner_1, 1, CV_RGB(128,128,128),8,8,0);
            circle(now_frame, corner_2, 1, CV_RGB(128,128,128),8,8,0);
            circle(now_frame, corner_3, 1, CV_RGB(128,128,128),8,8,0);
            circle(now_frame, corner_4, 1, CV_RGB(128,128,128),8,8,0);
            line(now_frame, center_position_of_face, center_of_frame, CV_RGB(0,200,0),1,8);
            
        }
        
    }
    flip(now_frame, now_frame, 1);
    imshow( "Facerecognizer", now_frame );
    return  faces.back(); // return a rect of where the face was detected. 
}


int main( int argc, char** argv )
{
    
    VideoCapture cap(0);
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.01);
    Size subPixWinSize(10,10), winSize(31,31);
    
    
    
    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }
    
    Mat gray, prevGray, image,test;
    //vector<Point2f> points[2];
    Rect face;
    
    while (waitKey(1) != 'y') {
        cap >> test;
        face = Facerecognizer(test);
    }
    
    destroyWindow("Facerecognizer");
    while (1) { // tracking state
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;
        
        
        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);
        Mat mask(frame.size(),CV_8UC1,Scalar(0));
        mask(face).setTo(Scalar(255));
        bool reinit;
        static uint64_t c;
        if (c++ == 0 || reinit == true) //detect good feature for tracking of face
        {
            cout << "automatic initialization" << endl;
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, mask, 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            reinit = false;

        }
        
        
        if( !points[0].empty() )
        {
           // cout << "i am here" << endl;
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
            {
                gray.copyTo(prevGray);
            }
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,3, termcrit, 0, quality level); // track points
            //RotatedRect ka = fitEllipse(points[1]);
            //rectangle(image, ka.boundingRect(), CV_RGB(0, 0, 255));
            size_t i, k;
            //outlier_point();
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( !status[i] )
                {
                    cout << " point removed " << endl;
                    circle( image, points[1][i],11, Scalar(255,0,0), -1, 8);
                    continue;
                }
                
                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
                
            }
            cout << "k: " << k  << endl;
            points[1].resize(k);
            
            
        }
        
        if( points[1].size() < (size_t)MAX_COUNT ) 
        {
            vector<Point2f> tmp;
            tmp.push_back(point);
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
        }
        
        
        
        flip(image, image, 1);
        imshow("LK Demo", image);
        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
        cout << "new - point: "<<points[1].size() << endl;
        cout << "old - point: " << points[0].size() << endl;
        
        if(points[1].size() < 100 ) // problem occurring  when i reach this if state - No points to track. 
        {
            destroyWindow("LK Demo");
            face = Facerecognizer(frame);
            destroyWindow("Facerecognizer");
            reinit = true;
        }

    }
    
    return 0;
}


the Problem occurs when it has to redirect the face since the points gets deleted for not having a heigh enough weight. when i reach this state it keeps switching between detection and tracking, and keeps looping that state.. how do i make it redetect and continue without any problems as those which are occurring now.
Topic archived. No new replies allowed.