我有以下程序在前景对象上绘制边界框。通过测量矩形一侧的长度,该边界框将有助于按长度对对象进行分类。
现在,这只在一个实例上仅在一个对象上绘制矩形,我想让它同时绘制所有它们。但我被困住了。需要指导或帮助。请参阅所附图片。
Rect boundingRect(InputArray contours);
// Finds the contour with the largest area
int area = 200;
int idx = 0;
for(int i=0; i<contours.size() ;i++)
{
if(area < contours[i].size())
idx = i;
}
//cout<< contours.size();
// Calculates the bounding rect of the largest area contour
Rect rect = boundingRect(contours[idx]);
Point pt1, pt2;
pt1.x = rect.x;
pt1.y = rect.y;
pt2.x = rect.x + rect.width;
pt2.y = rect.y + rect.height;
cout<< idx<< "\t \t";
// Draws the rect in the original image and show it
rectangle(frame_Original, pt1, pt2, CV_RGB(0,0,255), 2);
//cout << pt1; cout << pt2;