我正在使用OpenCV 2.4.8创建一个应用程序来检测枪炮。应用程序将部署在Raspberry Pi上。一个LED/蜂鸣器也连接到树莓Pi。如果检测到枪,则应根据特定情况打开LED/蜂鸣器。我已经附上了相关的代码。问题是,如何告诉密码,枪是否被发现?我是说,探测到的枪是用一个包围盒显示的。现在,如何使用如果条件下的边界框?我已经注释掉了代码中想要的行,就像我实际上想要做的一样。有人能指点我怎么做吗?任何帮助都是非常感谢的!谢谢。
下面是我的代码片段:
for(;;)
{
cap.retrieve(frame);
cap >> frame;
std::vector<Rect> guns;
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// Detect guns
gun_cascade.detectMultiScale(frame_gray, guns, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30 , 30));
// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;
cv::Rect bbox;
size_t ic = 0; // ic is index of current element
int ac = 0; // ac is area of current element
size_t ib = 0; // ib is index of biggest element
int ab = 0; // ab is area of biggest element
for (ic = 0; ic < guns.size(); ic++) // Iterate through all current elements (detected guns)
{
roi_c.x = guns[ic].x;
roi_c.y = guns[ic].y;
roi_c.width = (guns[ic].width);
roi_c.height = (guns[ic].height);
ac = roi_c.width * roi_c.height; // Get the area of current element (detected gun)
roi_b.x = guns[ib].x;
roi_b.y = guns[ib].y;
roi_b.width = (guns[ib].width);
roi_b.height = (guns[ib].height);
ab = roi_b.width * roi_b.height; // Get the area of biggest element, at beginning it is same as "current" element
if (ac > ab)
{
ib = ic;
roi_b.x = guns[ib].x;
roi_b.y = guns[ib].y;
roi_b.width = (guns[ib].width);
roi_b.height = (guns[ib].height);
}
Point pt1(guns[ic].x, guns[ic].y); // Display detected guns on main window - live stream from camera
Point pt2((guns[ic].x + guns[ic].height), (guns[ic].y + guns[ic].width));
//bbox = cvRect(roi_b.x, roi_b.y, roi_b.width, roi_b.height);
//int counter = 0;
rectangle(frame, pt1, pt2, Scalar(0, 0, 255), 2, 8, 0);
putText(frame, "Gun Detected!", pt1, FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0,250,250), 1, CV_AA);
// if(gun detected)
//counter = counter + 1;
/*
if(counter > 5)
{
turn on the LED of Raspberry Pi;
counter = 0;
}
*/
} //end of inner for loop
imshow("original", frame);
if(waitKey(30) >= 0) break;
} //end of outer for loop
return 0;
} //end main 发布于 2014-10-10 09:28:53
您有很多显式代码,这些代码可以归结为单行语句。
ab = 0;
for( ic=0; ic < guns.size(); ic++ )
{
roi_c = guns[i];
if( roi_c.area() > ab )
{
roi_b = roi_c;
ab = roi_c.area();
}
rectangle(frame, roi_c, Scalar(0, 0, 255), 2, 8, 0);
}如果您想检查是否至少检测到一支枪,您可以写:if( guns.size() > 0 ) { .. }。如果你想检查探测到的最大的枪是否大于某个阈值,你可以使用类似if( roi_b.area() > some_threshold )的东西。
希望这能回答。
https://stackoverflow.com/questions/26294738
复制相似问题