Mohammed Irfan
Mohammed Irfan

Reputation: 131

Find the area of a rectangle using OpenCV

I have 8 points of a polygon that are given in image below:

enter image description here

I want to find out the area of this shape using OpenCV Java

Here is the current code I'm trying:

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));

But the contourArea value is returned as empty

I found some reference code for OpenCV Python, as follows:

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)

Unfortunately, I was unable to convert it into Java. How would I go about finding the area of this shape?

Upvotes: 2

Views: 7672

Answers (1)

Calculating the area of the polygon has nothing to do with OpenCV and can in fact be done without the library...

Assuming you have a polygon of 4 points P1 until P4 enter image description here

then the area can be calculated as enter image description here

Upvotes: 4

Related Questions