1
我想將一段代碼從python移植到C++。我需要繪製黑色的圖像(多邊形)的一部分。Opencv異常fillconvexppoly
在python中,我有點的列表,然後我調用fillconvexpolygon來做到這一點。 我試着在相同C++,但我得到這個異常:
OpenCV Error: Assertion failed (points.checkVector(2, CV_32S) >= 0) in fillConvexPoly, file /home/user/template_matching/repositories/opencv/modules/core/src/drawing.cpp, line 2017
terminate called after throwing an instance of 'cv::Exception'
what(): /home/user/template_matching/repositories/opencv/modules/core/src/drawing.cpp:2017: error: (-215) points.checkVector(2, CV_32S) >= 0 in function fillConvexPoly
我能畫在圖像上線,我在用多邊形輪廓點列表點:
drawMatches(img_object, TSMART_BANKNOTE[i].kp, img_orig, BankNote_t.kp,
good, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
line(img_matches, scene_corners[0] + Point2f(img_object.cols, 0),
scene_corners[1] + Point2f(img_object.cols, 0),
Scalar(0, 255, 0), 4); //Left side
line(img_matches, scene_corners[2] + Point2f(img_object.cols, 0),
scene_corners[3] + Point2f(img_object.cols, 0),
Scalar(0, 255, 0), 4); //right side
line(img_matches, scene_corners[1] + Point2f(img_object.cols, 0),
scene_corners[2] + Point2f(img_object.cols, 0),
Scalar(0, 255, 0), 4); //Down line
line(img_matches, scene_corners[3] + Point2f(img_object.cols, 0),
scene_corners[0] + Point2f(img_object.cols, 0),
Scalar(0, 255, 0), 4); //Up line
vector<Point2f> pt;
pt.push_back(Point2f(scene_corners[0].x + img_object.cols,
scene_corners[1].y));
pt.push_back(Point2f(scene_corners[2].x + img_object.cols,
scene_corners[3].y));
pt.push_back(Point2f(scene_corners[1].x + img_object.cols,
scene_corners[2].y));
pt.push_back(Point2f(scene_corners[3].x + img_object.cols,
scene_corners[0].y));
std::cout << "PT POINTS: " << pt.size() <<"\r\n";
//fillConvexPoly(img_matches, pt, pt.size(), Scalar(1,1,1), 16, 0);
fillConvexPoly(img_matches, pt, cv::Scalar(255), 16, 0);
//-- Show detected matches
std::cout <<"H5\r\n";
imshow("Good Matches & Object detection", img_matches);
waitKey(0);
在文檔中,我找到了一個fillconvexpoly函數,它接收向量的大小。我似乎不接受這一點。我正在使用opencv 2.4.6
謝謝!這讓它工作。有沒有辦法使用浮點數而不是整數? – user1298272
不是。請記住,像素本質上是整數。我不希望即使是最新的openCV來處理float類型。理論上講,你可以自己重載drawPoly函數,該函數使用浮點數,進行逼近/反鋸齒,並繪製多邊形,每個關鍵點定義多個像素,但它需要一些工作並消耗比簡單繪製多邊形更多的資源。 – user3002166