我正在研究Java OpenCV项目,我想找到一个形状的市长和短轴。我使用fitEllipse方法,但Eclipse抛出以下错误:
OpenCV错误:cvFitEllipse2,文件........ \ opencv \ modules \ imgproc \ src \ shapedescr.cpp,第799行中输入数组的大小不正确(点数应该> = 5) / p>
后期转换大小为1x1。为什么会这样?
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(sourceImg, contours, new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i< contours.size();i++){
//Conversion between MatOfPoint to MatOfPoint2f
MatOfPoint2f temp=new MatOfPoint2f(contours.get(i).toArray());
RotatedRect elipse1=Imgproc.fitEllipse(temp);
}
答案 0 :(得分:2)
是的,方法fitEllipse只接受参数中的MatOfPoint2f。因此,您必须将MatOfPoint转换为MatOfPoint2f,您可以轻松地执行此操作:
MatOfPoint2f temp=new MatOfPoint2f();
temp.fromList(contours.get(i).toList());
RotatedRect elipse1=Imgproc.fitEllipse(temp);
答案 1 :(得分:1)
您需要temp
至少有5分才能拨打Imgproc.fitEllipse(temp)
。
结帐opencv's source here
您会看到cvFitEllipse2
(由fitEllipse
使用)检查最少5个点。
我不完全确定为什么你必须在数学上至少有5个点才能做到这一点,但是在我阅读here时,4个点并没有完全锁定椭圆。
另外,如果您正在使用它的数学,请同时检查this一个(它还有其他解释的内部链接)。