我正在Visual Studio Community 2017中制作涉及手识别的节奏游戏。
在该游戏中,有一个功能可以检查屏幕上某个圆圈中是否有手。 以下是该函数的代码。
bool check(Mat &show)
{
//show is the image where the function will detect hands from
std::vector<Rect> hands;
Mat grayVer;
int i;
long long distance;
printf("Check function started!\n");
cvtColor(show, grayVer, COLOR_BGR2GRAY);
imshow("greyVer", grayVer);
waitKey(1);
printf("detecting...\n");
hand_cascade.detectMultiScale(
grayVer, hands, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(200, 200));
if (hands.size() > 0)
printf("detected hands!\n");
for (i = 0; i < hands.size(); i++)
{
Point center(hands[i].x + hands[i].width / 2, hands[i].y + hands[i].height / 2);
ellipse(show, center,
Size(hands[i].width / 2, hands[i].height / 2), 0, 0, 360,
Scalar(255, 0, 255), 4, 8, 0);
distance =
square( xCoor/*the X Coordinate of the circle a hand has to be in*/
- (hands[i].x + hands[i].width / 2)) +
square(yCoor/*the Y Coordinate of the circle a hand has to be in*/
- (hands[i].y + hands[i].height / 2));
if (distance > square(
range/*the size of the circle a hand has to be in*/))
{
return true;
}
}
return false;
}
构建此代码时没有错误发生。但是,当调用detectMultiScale函数时,程序立即暂停几秒钟并崩溃。同样,该函数有时不会使程序崩溃,但是在结果Vector中什么也不留下。
如果要获取程序的完整文件(Visual Studio项目文件),请单击此Link