2016-10-27 307 views
2

我試圖用fillConvexPoly函數填充掩碼中的三角形。 但我得到以下錯誤。C++中的OpenCV fillConvexPoly函數拋出異常

OpenCV Error: Assertion failed (points.checkVector(2, CV_32S) >= 0) in fillConvexPoly, file /home/iris/Downloads/opencv-3.1.0/modules/imgproc/src/drawing.cpp, line 2256 
terminate called after throwing an instance of 'cv::Exception' 
what(): /home/iris/Downloads/opencv-3.1.0/modules/imgproc/src/drawing.cpp:2256: error: (-215) points.checkVector(2, CV_32S) >= 0 in function fillConvexPoly 

我調用函數作爲像這樣,

cv::Mat mask = cv::Mat::zeros(r2.size(), CV_32FC3); 
cv::fillConvexPoly(mask, trOutCroppedInt, cv::Scalar(1.0, 1.0, 1.0), 16, 0); 

其中trOutCroppedInt定義像這樣,

std::vector<cv::Point> trOutCroppedInt 

我在矢量推3分,

[83, 46; 0, 48; 39, 0] 

我應該如何糾正這個錯誤R'

+0

請發表[mcve]。這對我來說完美無瑕,可能你在代碼中沒有顯示出問題 – Miki

回答

1

有關錯誤points.checkVector(2,CV_32S)> =當數據類型是複雜得多比CV_32S和尺寸大於二0)可能發生

此錯誤。例如所有數據類型(如vector<Point2f>)都會導致此問題。所以我們可以使用fillConvexpoly如下步驟

1.讀取圖像

cv::Mat src=cv::imread("what/ever/directory"); 

2.確定點

我們必須確定像下面的圖片 enter image description here

我們的觀點所以我們的代碼爲這個點將是

vector<cv::Point> point; 
point.push_back(Point(183,146)); //point1 
point.push_back(Point(100,148)); //point2 
point.push_back(Point(100,110)); //point3 
point.push_back(Point(139,110)); //point4 

3.使用cv::fillConvexPoly功能

假設我們的形象是src,我們希望這個圖像上繪製多邊形(點之前(含)),那麼我們的代碼將

cv::fillConvexPoly(src,    //Image to be drawn on 
        point,     //C-Style array of points 
        Scalar(255,0,0), //Color , BGR form 
        CV_AA,    // connectedness, 4 or 8 
        0);   // Bits of radius to treat as fraction 

所以輸出圖像是像下面(前:左側 - 後右側)

enter image description here