OpenCV findContours function problem
I am trying to use the findContours function in OpenCV, but VS 2008 gives an error saying:
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupport ed array type) in unknown function, file ........\ocv\opencv\src\cxcore\cxarr ay.cpp, line 2476
This application has reques开发者_如何学编程ted the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Press any key to continue . . .
Here's the code:
Mat_<Vec<float,3>> originalimage;
Mat_<Vec<float,3>> resultingimage;
vector<vector<cv::Point>> v;
originalimage = cv::imread("Original.ppm");
cv::findContours(originalimage,v,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);
Thanks in advance
FindContours only accepts binary image. That is , any image which is output of cvThreshold cvAdapiveThreshold cvCanny
try adding this statement before cv::findContours
cvThreshold(originalImage,resultingImage,100,100,CV_THRESH_BINARY)
then call findcontours with resultingImage.
if it works then you should input the correct parameters to cvThreshold ( 100 is just an example).Check the reference for that matter.
EDIT: resultingImage should be a single channel image!!
I have had the same problem (or at least a similar one) with that function. I was not able to fix it, so I used the old C style cvFindContours function instead. I have included an example function in wich I used the cvFindContours function to clean up a blob image. This might not be the fastest solution, but at leas it works.
void filtBproject(Mat& Bproject){
Scalar color = CV_RGB(255,255,255); // text color
IplImage* BprojectIpl = &IplImage(Bproject);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours = 0;
int numCont = 0;
int contAthresh = 45;
numCont= cvFindContours( BprojectIpl, storage, &contours, sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
cvSet(BprojectIpl, cvScalar(0,0,0));
for( ; contours != 0; contours = contours->h_next )
{
if ( (cvContourArea(contours, CV_WHOLE_SEQ) > contAthresh) ){
cvDrawContours( BprojectIpl, contours, color, color, -1, CV_FILLED, 8 );
}
}
}
For your v vector, you need to add a space like so:
vector<vector<cv::Point> > v;
Very subtle and dumb but it works.
精彩评论