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
|
#include <iostream>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect/objdetect.hpp"
using namespace cv;
using namespace std;
int i = 0;
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03);
Size subPixWinSize(10,10), winSize(31,31);
//detectAndDisplay - displacement
tuple<pair<Mat, Mat>,pair<Vector<Point2f>, Vector<Point2f>> > detectAndDisplay ( Mat frame )
{
Mat grayframe, prevGray, image;
if (frame.type()!= 8) {
cvtColor(frame, frame, CV_8U);
}
cvtColor(frame, grayframe, CV_RGB2GRAY);
vector<Point2f> corners_prev;
vector<Point2f> corners_now;
String face_cascade_XML = "/Users/x/Documents/x/facedetect/facedetect/haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
vector<Rect> faces(1);
Point center_of_frame(grayframe.size().width/2,grayframe.size().height/2);
pair<Point, Point> corners;
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_XML ) ){
cout << "Cascade Error" << endl;
};
circle(grayframe, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
//-- Detect faces
face_cascade.detectMultiScale( grayframe, faces, 1.1, 2, 0, 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(frame, faces, CV_RGB(0,255,0),4,8,0);
circle(frame, center_position_of_face, 8, CV_RGB(128,128,128),8,8,0);
circle(frame, corner_1, 1, CV_RGB(128,128,128),8,8,0);
circle(frame, corner_2, 1, CV_RGB(128,128,128),8,8,0);
circle(frame, corner_3, 1, CV_RGB(128,128,128),8,8,0);
circle(frame, corner_4, 1, CV_RGB(128,128,128),8,8,0);
line(frame, center_position_of_face, center_of_frame, CV_RGB(0,200,0),1,8);
}
Mat mask(frame.size(),CV_8UC1 ,Scalar(0));
mask(faces.back()).setTo(Scalar(255));
if(prevGray.empty())
{
grayframe.copyTo(prevGray);
}
goodFeaturesToTrack(prevGray, corners_prev, 300, 0.01, 10,mask,3,0,0.04);
goodFeaturesToTrack(grayframe, corners_now, 300, 0.01, 10, mask, 3, 0, 0.04);
cornerSubPix(prevGray, corners_prev, subPixWinSize, Size(-1,-1), termcrit);
cornerSubPix(grayframe, corners_now, subPixWinSize, Size(-1,-1), termcrit);
size_t i, k;
vector<uchar> status;
vector<float> err;
for ( i = 0; i < corners_now.size(); i++)
{
circle( frame, corners_now[i], 4, Scalar(0,255,0), -1, 8, 0 );
}
}
flip(frame, frame, 1);
imshow("Facedetection", frame);
return make_tuple(make_pair(grayframe,prevGray), make_pair(corners_now, corners_prev));
}
int main( int argc, const char** argv )
{
VideoCapture cam(0);
Mat frame;
tuple<pair<Mat, Mat>,pair<Vector<Point2f> , Vector<Point2f>> > grey_frames;
//-- 2. Read the video stream
if( !cam.isOpened() )
{
cout <<"Webcam error" << endl;
return 0;
}
else
{
cout << "Is face detected ?" << endl;
while(waitKey(1) != 'y')
{
cam.read(frame);
if( !frame.empty()){
grey_frames = detectAndDisplay(frame);
}
}
while(waitKey(1))
{
cam.read(frame);
//-- 3. Apply the classifier to the frame
if( !get<1>(grey_frames).second.empty())
{
vector<uchar> status;
vector<float> err;
Mat prevImg = get<0>(grey_frames).second;
Mat nextImg = get<0>(grey_frames).first;
Vector<Point2f> prevPts = get<1>(grey_frames).second;
Vector<Point2f> nextPts = get<1>(grey_frames).first;
calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, nextPts, status, err); //---> here the error occurs.
flip(frame,frame,1);
imshow("tracking", frame);
}
}
}
return 0;
}
|