当用户将手伸向相机时,我正在使用此代码* 提取手指 * s。
插入这些线以找到凸起后,抛出以下错误。
if (contours[i].size() >3 )
{
cout<<"inside"<<endl;
convexityDefects(contours[i], hull[i], defects[i]);
}
错误是:
内部 OpenCV错误:断言失败(hull.checkVector(1,CV_32S)&gt; 2)在convexityDefects中,文件C:/slave/builds/WinInstallerMegaPack/src/opencv/modules/imgproc/src/contours.cpp,第1971行
此应用程序已请求Runtime以不寻常的方式终止它。 有关更多信息,请联系应用程序的支持团队。 在抛出'cv :: Exception'的实例后终止调用 what():C:/slave/builds/WinInstallerMegaPack/src/opencv/modules/imgproc/src/contours.cpp:1971:error:( - 1515)hull.checkVector(1,CV_32S)&gt; 2在函数convexityDefects
有人可以帮我解决这个问题吗?
代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 147;
int max_thresh = 255;
RNG rng(12345);
/// Function header
void thresh_callback(int, void* );
/** @function main */
int main( int argc, char** argv )
{
src = imread( "D:\\a.jpg", 1 );
/// Convert image to gray and blur it
resize(src,src,Size(640,480),0,0,INTER_LINEAR);
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// Create Window
char* source_window = "Knuckle Extractor";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
// createTrackbar( " Threshold:", source_window, &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat src_copy = src.clone();
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using Threshold
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY||CV_THRESH_OTSU );
// imshow("Grey",src_gray);
imshow("Threshold",threshold_output);
/// Find contours
// findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
//findContours( threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0) );
findContours( threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Find the convex hull object for each contour
vector<vector<Point> >hull( contours.size() );
vector<vector<Vec4i> >defects( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
convexHull( Mat(contours[i]), hull[i], false );
if (contours[i].size() >3 )
{
cout<<"inside"<<endl;
convexityDefects(contours[i], hull[i], defects[i]);
}
}
/// Draw contours + hull results
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
/// Show in a window
namedWindow( "Result", CV_WINDOW_AUTOSIZE );
imshow( "Result", drawing );
}
答案 0 :(得分:3)
问题(或者至少是我能看到的主要问题)是你构建轮廓的方式。你应该使用:
vector<vector<int> >hull( contours.size() );
而不是:
vector<vector<Point> >hull( contours.size() );
这是因为convexityDefects
函数仅适用于由一系列索引而不是一系列点表示的凸包。