当我尝试启动我的应用程序时,它在执行轮廓区域时意外崩溃。
这是错误:
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.catch frame from camera, 2. 高斯和中值滤波, 3. morphological opening, 4. threshold, 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;
}
我该如何解决?