gpt4 book ai didi

c++ - OpenCV:contourArea 断言失败

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:56 24 4
gpt4 key购买 nike

当我尝试启动我的应用程序时,它在执行 contourArea 时意外崩溃。

这里是错误:

OpenCV Error: Assertion Failed (contour.checkVector(2) >= 0 && (contour.depth() ==CV_32F || contour.depth() == CV_32S)) in unknown function, file ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 1904

我的程序很简单:1.从相机捕捉帧,2.高斯和中值滤波,3.形态开,4.阈值,5.找到轮廓,6.绘制更大面积的等高线

这是我的代码:

#include <opencv2/opencv.hpp>
#include <stdio.h>

using namespace cv;
using namespace std;

Mat mask(480,640, CV_8UC1);
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
vector<Point> my_contourn;

int main(){
VideoCapture camera(0);

if(!camera.isOpened()){
return -1;
}

while(1){
Mat cameraframe,filtered_img,mask2;
camera >> cameraframe;

GaussianBlur(cameraframe,filtered_img,Size(11,11),0,0);
medianBlur(filtered_img,filtered_img,11);
cvtColor(filtered_img,filtered_img,CV_BGR2HSV);
inRange(filtered_img, Scalar(0, 76, 97), Scalar(20, 143, 205), mask);
morphologyEx(mask,mask,MORPH_OPEN,getStructuringElement(MORPH_RECT,Size(9,9),Point(4,4)));
GaussianBlur(mask,mask,Size(3,3),0,0);
dilate(mask,mask,getStructuringElement(MORPH_ELLIPSE,Size(7, 7),Point(0, 0) ));


mask.copyTo(mask2);
findContours(mask2,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,Point(0, 0));

double area,max_area=0.0;


for(int i=0;i<contours.size();i++){

area = fabs(contourArea(contours[i]));
if (area>max_area)
{
max_area=area;
my_contourn=contours[i];
}
}

drawContours( mask, my_contourn, 10, Scalar(255,0,0), 2, 8);

imshow("my cont",mask);

if(waitKey(30)>=0)
break;
}
return 0;
}

我该如何解决??

最佳答案

我确认这是一个 VS2012 问题。在 VS2010 上一切正常。

关于c++ - OpenCV:contourArea 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17468345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com