gpt4 book ai didi

java - 使用 OpenCV 计算矩形的面积

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:38 27 4
gpt4 key购买 nike

我有一个多边形的 8 个点,如下图所示:

enter image description here

我想使用 OpenCV Java 找出这个形状的面积

这是我正在尝试的当前代码:

Mat temp_mat=new Mat();
List<MatOfPoint> temp_contour=new ArrayList();
temp_contour.add(new MatOfPoint(new Point(w1,w2),new Point(x1,x2),new Point(y1,y2),new Point(z1,z2)));
Imgproc.drawContours(temp_mat,temp_contour,0,new Scalar(255,0,0));
double contourArea = Imgproc.contourArea(temp_contour.get(0));

但是 contourArea 值返回为空

找了一些OpenCV Python的引用代码,如下:

import numpy
import cv2

contours = [numpy.array([[1,1],[10,50],[50,50]], dtype=numpy.int32) , numpy.array([[99,99],[99,60],[60,99]], dtype=numpy.int32)]

drawing = numpy.zeros([100, 100],numpy.uint8)
for cnt in contours:
cv2.drawContours(drawing,[cnt],0,(255,255,255),2)

cv2.imshow('output',drawing)
cv2.waitKey(0)

不幸的是,我无法将它转换成 Java。我将如何找到这个形状的面积?

最佳答案

计算多边形的面积与 OpenCV 无关,实际上可以在没有库的情况下完成...

假设您有一个由 4 个点组成的多边形 P1 直到 P4 enter image description here

那么面积可以计算为enter image description here

关于java - 使用 OpenCV 计算矩形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44389689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com