Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

arclength and contourarea wrong results?

I am using cv2's arclength and contourarea in my python project. To verify the results I've applied both functions to a circle with r=100. The circumference of the circle should be 2pi100 = 628,31 and the area should be pi*100^2=31415.92.

Now I get both the circumference and area using cv2:

import numpy as np
import cv2

canvas = np.zeros((500, 500), dtype=np.uint8)
circle = cv2.circle(canvas, (250, 250) , 100, 10, -1)

contours, hierarchy = cv2.findContours(circle, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    circumference = cv2.arcLength(cnt, True)
    area = cv2.contourArea(cnt)
    print("c: %s" % circumference)
    print("area: %s" % area)

I would expect estimations closer to the real value, however it returns circumference=661.75 and area=31134, which both have quite a big error.

I've also estimated the area by simply counting the non-zero pixels:

area_nzp = np.count_nonzero(circle)
print(area_nzp)

Which returns area=31417, being closer to the real value.

I was wondering if anyone could tell me how to use arclength and contourarea in the "correct way" such that the error will become smaller. Thanks in advance.

arclength and contourarea wrong results?

I am using cv2's arclength and contourarea in my python project. To verify the results I've applied both functions to a circle with r=100. The circumference of the circle should be pi2pi100 = 628,31 and the area should be pi*100^2=31415.92.

Now I get both the circumference and area using cv2:

import numpy as np
import cv2

canvas = np.zeros((500, 500), dtype=np.uint8)
circle = cv2.circle(canvas, (250, 250) , 100, 10, -1)

contours, hierarchy = cv2.findContours(circle, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    circumference = cv2.arcLength(cnt, True)
    area = cv2.contourArea(cnt)
    print("c: %s" % circumference)
    print("area: %s" % area)

I would expect estimations closer to the real value, however it returns circumference=661.75 and area=31134, which both have quite a big error.

I've also estimated the area by simply counting the non-zero pixels:

area_nzp = np.count_nonzero(circle)
print(area_nzp)

Which returns area=31417, being much closer to the real value.

I was wondering if anyone could tell me how to use arclength and contourarea in the "correct way" such that the error will become smaller. Thanks in advance.

arclength and contourarea wrong results?

I am using cv2's arclength and contourarea in my python project. To verify the results I've applied both functions to a discretized circle with r=100. The circumference of the circle should be around pi2100 = 628,31 and the area should be approximatly pi*100^2=31415.92.

Now I get both Getting the circumference and area using cv2:

import numpy as np
import cv2

canvas = np.zeros((500, 500), dtype=np.uint8)
circle = cv2.circle(canvas, (250, 250) , 100, 10, -1)

contours, hierarchy = cv2.findContours(circle, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    circumference = cv2.arcLength(cnt, True)
    area = cv2.contourArea(cnt)
    print("c: %s" % circumference)
    print("area: %s" % area)

I would expect the estimations closer to the real value, however to be close to the theoretical value but it returns circumference=661.75 and area=31134, which both have quite a big error.

I've also estimated the area by simply counting the non-zero pixels:

area_nzp = np.count_nonzero(circle)
print(area_nzp)

Which returns area=31417, being much closer to the real theoretical value.

I was wondering if anyone could tell me how to use arclength and contourarea in the "correct way" such that the error will become smaller. Thanks in advance.

Edit: Inspired by LBerger's comment I plotted the error between the error of theoretical surface value of the circle vs ContourArea and the area estimated by counting the non-zero pixels. I do not understand why ContourArea's error can be this big. Does anyone know what actually happens when the function is run?

error