当我尝试启动我的应用程序时,在执行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. findContours,
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/