如何在轮廓周围绘制矩形? [英] How to draw a rectangle around the contours?

查看:103
本文介绍了如何在轮廓周围绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从opencv开始,我正在尝试创建一个程序,该程序在一块沙子上的岩石图片周围放置正方形.函数此处的文档包括一个示例如何使用它.

I am just starting out with opencv and I am trying to make a program that puts squares around a picture of rocks on some sand. The documentation for the function here includes an example of how to use it.

findContours( src, contours, hierarchy,
  CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

findContours的原型是

The prototype of findContours is

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point()) ;

我有两个问题.
1.示例hierarchy中的第三个参数是vector<Vec4i>与findContours期望的类型不匹配.这是为什么?
2.如何使用contours中存储的数据找到轮廓在何处创建边界框?

I have two questions.
1. The third argument in the example hierarchy is a vector<Vec4i> does not match the type findContours expects. Why is that?
2. How does one use the data stored in contours to find where the contours are to create a bounding box?

推荐答案

std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours( mask, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
for ( size_t i=0; i<contours.size(); ++i )
{
    cv::drawContours( img, contours, i, Scalar(200,0,0), 1, 8, hierarchy, 0, Point() ); 
    cv::Rect brect = cv::boundingRect(contours[i]);
    cv::rectangle(img, brect, Scalar(255,0,0));
}

这篇关于如何在轮廓周围绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆