image-processing - 前景对象上的边界矩形以进行分类

标签 image-processing opencv computer-vision

我有以下程序在前景对象上绘制边界框。
通过测量矩形一侧的长度,此边界框将有助于按长度对对象进行分类。

现在,这仅在实例的一个对象上绘制矩形,我想使其同时绘制所有对象。但是我被困住了。需要指导或协助。请查看所附图片。

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;

最佳答案

如果要用countours绘制所有大于area的矩形,则应保存索引,然后使用循环绘制所有矩形。

vector<int> idx;
for(int i=0; i < contours.size() ;i++)
{
    if(area < contours[i].size())
        idx.push_back(i);
}
for((int i=0; i < idx.size() ;i++)
    \\ Draw every contours[idx[i]]  

关于image-processing - 前景对象上的边界矩形以进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13261216/

相关文章:

c++ - 在 OpenCV 中获取 Z "depth"的简单方法

python - 如何使用 opencv 从图像中删除黑色背景并使其透明?

neural-network - 使用卷积神经网络检测四边形/矩形的想法

python - 将齐次坐标 (x0=1) 添加到 numpy 中的图像

linux - 使用 GNU/Linux 的图像格式计算器和转换器

python - skimage.color.rgb2gray 导入问题

matlab - MATLAB 的 `regionprops` 函数如何计算周长?

matlab - graycomatrix偏移参数的使用

opencv - 在拥挤区域进行强大的人体检测和跟踪

python - 从圆形边框内提取文本