Opencv人头跟踪检测
//-------------------------------------人头检测-------------------------------------
int main()
{
//VideoCapture video("E:\\C_VC_code\\Text_Photo\\feini.flv");
vector<float>head;
FILE *read = fopen("detector_TQ.txt", "r");
if (read == NULL)
{
cout << "Error: The file read fail" << endl;
return -1;
}
double data;
while (fscanf(read, "%lf", &data) != -1)
{
head.push_back(data);
}
cout << head.size() << endl;
VideoCapture video(0);
if (!video.isOpened())
{
return 0;
}
Mat img, dstimg;
vector<Rect>found;
vector<Rect>result;
Rect r;
HOGDescriptor hog(Size(48, 48), Size(16, 16), Size(8, 8), Size(8, 8), 9);
hog.setSVMDetector(head);
double scalar = 0.5;
int i, j;
while (1)
{
video >> img;
resize(img, dstimg, Size(img.cols*scalar, img.rows*scalar), 1, 1, 3);
hog.detectMultiScale(dstimg, found, 1, cv::Size(8, 8), cv::Size(0, 0), 1.1, 3, false);
//去除嵌套的矩形
result.clear();
for (i = 0; i < found.size(); i++)
{
r = found[i];
for (j = 0; j < found.size(); j++)
{
if (i != j && (r&found[j]) == r)
{
break;
}
}
if (j == found.size())
{
result.push_back(r);
}
}
//draw rect
for (i = 0; i<result.size(); i++)
{
r = result[i];
r.x = r.x/scalar*0.81;
r.y = r.y/scalar;
r.width = r.width/scalar*1.7;
r.height = r.height/scalar*4;
rectangle(img, r, CV_RGB(0, 255, 0), 2);
}
//imshow("video1", dstimg);
imshow("video", img);
found.clear();
if (waitKey(33) == 27)
{
imshow("video", img);
waitKey(0);
break;
}
}
}