打开cv查找凸包
Open cv finding convex hull
OpenCV中的以下代码用于检测黄色球并绘制其凸包。该代码虽然没有给出任何编译错误,但在输出窗口中给出了以下错误。我使用了最大面积函数来避免较小的不需要的轮廓。错误为
断言失败<0<=contourIdx&;contourIdx<最后>在cv::drawContours,文件C:Buildsmasters中。。(某些路径(,线路2299***
#include <opencvcv.h>
#include <opencv2highguihighgui.hpp>
#include<opencvcvaux.h>
#include<opencvcxcore.h>
#include <opencv2imgprocimgproc.hpp>
#include <iostream>
#include<conio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main(){
Mat img, frame, img2, img3;
double maxarea = 0;
int lrgctridx; //largest contour index
VideoCapture cam(0);
while (true){
cam.read(frame);
cvtColor(frame, img, CV_BGR2HSV);
//thresholding
inRange(img, Scalar(0, 143, 86), Scalar(39, 255, 241), img2);
//finding contours
vector<vector<Point>> Contours;
vector<Vec4i> hier;
//morphological transformations
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
//finding the contours required
findContours(img2, Contours, hier, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));
//finding the contour of largest area and storing its index
for (int i = 0; i < Contours.size(); i++)
{
double a=contourArea(Contours[i]);
if (a> maxarea)
{
maxarea = a;
lrgctridx=i;
}
}
//convex hulls
vector<vector<Point> >hull(Contours.size());
for (int i = 0; i < Contours.size(); i++)
{
convexHull(Contours[i], hull[i], false);
}
//REQUIRED contour is detected,then draw a convex hull
if (maxarea!=0)
drawContours(frame, hull, lrgctridx, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point());
imshow("output", frame);
char key = waitKey(33);
if (key == 27) break;
}
}
任何帮助都将不胜感激。提前解冻!
您应该在每次迭代时重置lrgctridx
。
假设您在时间"t"找到一个计数,并设置lrgctridx = 1;
。在时间"t+1",你没有找到任何轮廓,所以Contours
和hull
的大小将是0
,但你正试图访问位置1。
只需将lrgctridx = 0
放在for
循环之前。与maxarea
相同。
lrgctridx = 0;
maxarea = 0;
for (int i = 0; i < Contours.size(); i++)
{
....
现在你画轮廓的条件还可以,但你最好用代替它
if(!Contours.empty()) {
drawContours(...);
....
相关文章:
- 了解元函数以在类型包中查找类型
- CGAL:带有带有信息的点的 3D 凸包
- 查找非凸多边形的代表性平均 INTERIOR 点
- CMAKE 查找包但不链接库
- 影响格雷厄姆查找凸包算法的未知错误
- 根据另一个可变参数包查找可变参数包的收缩
- 查找参数包的唯一值的数目
- 使用 OpenCV C++绘制最大轮廓的凸包
- 点云库中的凸包计算在二维和三维都失败
- 递归方法中的 C++ 凸包
- 使用 CMake 查找包 OpenCV
- 为什么我的凸包周长计算不起作用?
- 3D 凸包的体积
- 在opencv c++函数中查找凸性缺陷点
- 创建一个较小的凸包算法,可以用1个循环来完成
- 如何查找重新传输的TCP数据包
- 找出一个点是否在由一组点生成的凸包中
- 打开cv查找凸包
- 消息包-如何查找表示的字符串
- 如何使用boost::asio查找UDP数据包的目标地址