我現在正在使用OpenCV和圖像處理。我正在經歷的當前話題是凸面缺陷。下面的代碼是簡單的程序,可以說明凸性缺陷,但我得到的代碼後顯示的錯誤是錯誤的。所以,請幫我工作錯誤cvConvexityDefect
代碼: -
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
int g_thresh = 128;
int main(int argc, char **argv)
{
CvMemStorage *hull = cvCreateMemStorage(0);
CvMemStorage *mem = NULL;
CvMemStorage *defe_mem = cvCreateMemStorage(0);
mem = cvCreateMemStorage(0);
CvSeq *contours = NULL;
CvSeq *poly;
CvSeq *convex;
CvSeq *i;
CvSeq *defect;
IplImage *img = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
IplImage *gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvCvtColor(img, gray, CV_RGB2GRAY);
cvShowImage("Source", img);
cvWaitKey(0);
cvShowImage("DEstination", gray);
cvWaitKey(0);
cvThreshold(gray, gray, g_thresh, 255, CV_THRESH_OTSU);
cvShowImage("BinaryImage", gray);
cvWaitKey(0);
int a = cvFindContours(gray, mem, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvDrawContours(gray, contours, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0));
cvShowImage("Output",gray);
cvWaitKey(0);
convex = cvConvexHull2(contours, hull, CV_CLOCKWISE, 1);
cvDrawContours(gray, convex, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0));
cvShowImage("Convex_Hull", gray);
cvWaitKey(0);
//Program Completely Works till here, every thing massed up after this line..
defect = cvConvexityDefects(contours, convex, defe_mem);
for(i = contours; i != 0; i = i->h_next){
cvDrawContours(gray, defect, cvScalarAll(255), cvScalarAll(255), 1, 1, 8, cvPoint(0, 0));
}
//fprintf(stderr, "Convexity Defect::%d\n", cvCheckContourConvexity(contours));
cvShowImage("Convexity", gray);
cvWaitKey(0);
}
錯誤: -
OpenCV Error: Unsupported format or combination of formats (Convex hull must represented as a sequence of indices or sequence of pointers) in cvConvexityDefects, file /home/akshit/OpenCV/OpenCV-2.4.3/modules/imgproc/src/convhull.cpp, line 544
terminate called after throwing an instance of 'cv::Exception'
what(): /home/akshit/OpenCV/OpenCV-2.4.3/modules/imgproc/src/convhull.cpp:544: error: (-210) Convex hull must represented as a sequence of indices or sequence of pointers in function cvConvexityDefects
凸包中發現了與輸出正常,但凸性缺陷沒有發現正確
任何建議?
旁註:嘗試使用新的API OpenCV的替代。 – herohuyongtao
我可以得到版本號,現在使用2.4.3 –
2.4.3已經支持新的API。 – herohuyongtao