由于某种原因,谷歌没有使用 cvCalcPGH() 的好例子,也没有其他使用 Freeman 代码进行轮廓匹配的好例子。所以我试着做到:
#include "opencv2/opencv.hpp"
#include "opencv2/legacy/legacy.hpp"
int
main(int argc, char* argv[]) {
IplImage* g_gray = cvLoadImage("lisa.png",CV_LOAD_IMAGE_GRAYSCALE);
cvThreshold( g_gray, g_gray, 100, 255, CV_THRESH_BINARY );
CvMemStorage* storage=cvCreateMemStorage(0);
CvSeq* firstContour=NULL;
int a=cvFindContours(g_gray, storage, &firstContour,sizeof(CvChain),CV_RETR_LIST, CV_CHAIN_CODE);
CvHistogram *hist;
int h_bins = 30, s_bins = 30;
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 255 };
int hist_size[] = { h_bins, s_bins };
float* ranges[] = { h_ranges, s_ranges };
hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
if( CV_SEQ_ELTYPE( firstContour ) != CV_32SC2 ) {
printf(":(\n");
}
cvCalcPGH((CvSeq *)firstContour,hist);
return 0;
}
cvFindContours() 将按记录运行,但 cvCalcPGH() 将失败:
$ ./calcpgh
:(
OpenCV Error: Unsupported format or combination of formats (The contour is not valid or the point type is not supported) in cvCalcPGH, file /Users/tonu-samuel/OpenCV-2.4.2/modules/legacy/src/pgh.cpp, line 351
terminate called throwing an exceptionAbort trap: 6
$
OpenCV 是 2.4.2 并且表示 assert() 是为了CV_SEQ_ELTYPE( contour ) != CV_32SC2
我也在我的代码中检查。如何解决它不确定。