Ask Your Question
1

contourArea strange behaviour

asked 2020-04-07 02:26:43 -0600

hamsolo474 gravatar image

I am just starting to play with OpenCV and I have found some very strange behaviour from the contourArea function.

See this image.

Original

It has three non connected areas, the left is a grouping of long strokes and on the top center there is a single dot and finally a big square on the right.

When I run my function, I get this result

Contours detected

Contour[0] Area: 221, Length: 70, Colour: Red 
Contour[1] Area: 13772, Length: 480, Colour: Green 
Contour[2] Area: 150, Length: 2370, Colour: Blue

While I havent actually counted the area of the left part, It seems as if it encompasses much more than 149 pixels and would certainly have a higher value than the dot in the top center, I would say that dot should be able to fit in to the left part at least 10 times. The area of the square does work out, I did calculate the area of the square.

Square Area
width = 118
height = 116
118 * 116 = 13,688

13,688 is really close to what opencv gave as the area, the difference is likely measurement error on my behalf. I manually calculated the area of the dot

Dot Area
width = 27
height = 6 
27*6 = 162

Not too far off from what opencv said it would be

Reading from the OpenCV docs page on contourArea it says that it will give wrong results for contours with self intersections. Not really understanding what self intersections are, I made a test image.

ORIGINAL PIC HERE

As you can see I have a rectangle on the left and a cross in the middle and another cross rotated 45 deg. I would expect the cross to have slightly less than double the area of the rectangle due to the overlap in the center.

CONTOURED PIC HERE

Contour[0] Area: 1805, Length: 423, Colour: Red 
Contour[1] Area: 947, Length: 227, Colour: Green 
Contour[2] Area: 1825, Length: 415, Colour: Blue

As you can see the area of the two crosses are slightly less than double the area of the rectangle. As expected.

I am not interested in capturing the inside of the square or getting a box drawn around the shape on the left and the dot (though it would be tangentially interesting) it's not specifically what I'm asking about in this question.

So my question: Why is the area of my irregular shape severly underestimated?

  1. Am I using the wrong function?
  2. Am I using the right function incorrectly?
  3. Have I found a bug in opencv?
  4. Does self intersections have a meaning that wasn't demonstrated in my test?

I copied most of this code from this tutorial

I have stripped down my code to this self contained example below.

def contour_test(name):
    import cv2 as cv
    colours = [{'name': 'Red ',   'bgr': (0, 0, 255)}, 
               {'name': 'Green ', 'bgr': (0, 255, 0)},
               {'name': 'Blue ',  'bgr': (255, 0, 0)}]
    src = cv.imread(cv.samples.findFile(name))
    src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    src_gray = cv.blur(src_gray, (3,3))
    threshold = 100 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-11 01:58:25 -0600

hamsolo474 gravatar image

This question was answered by Rotem on stackoverflow, view it here

I needed to invert my image to black background white foreground so that area could count the white pixels.

_, src_thresh = cv.threshold(src_gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
contours, _ = cv.findContours(src_thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-04-07 02:26:43 -0600

Seen: 1,012 times

Last updated: Apr 11 '20