0
我正在使用OpenCV 2.4.8創建一個應用程序,它將檢測槍。該應用程序將部署在Raspberry Pi上。 LED /蜂鳴器也連接到樹莓派。如果檢測到槍,應根據某些特定條件打開LED /蜂鳴器。我附上了相關的代碼。問題是,如何判斷槍是否被檢測到?我的意思是,檢測到的槍由邊界框顯示。現在,如何在IF-Condition中使用邊界框?我已經在代碼中註釋了所需的行,就像我想要的那樣。任何人都可以請指導我如何做到這一點?任何幫助,高度讚賞!謝謝。
這裏是我的代碼片段:如何在決策條件下使用Object Detection Bounding Box來使用C++ OpenCV打開Raspberry 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
非常感謝你親愛的@ a-Jays爲你提供的幫助。是的,它的作品:) – 2014-10-10 18:24:40