How to find the area of a binary image or contour

Hello everyone, beginner OpenCV student here:

I am trying to overlay 2 images (or binary images) on each other and I need to find the area of the section where both images intersect.
To do this, my approach is simple:
1 - Find contour of the first image, and then find the area of the contour
2 - Find contour of the second image, and then find the area of the contour
3 - Find the contour of the intersection and then find the area.

However, the area = cv2.contourArea(cnt) line of code typically returns 0.0 as the area and so, I am not able to progress to step 3.

I have searched online and the answers that I keep getting say that the contour is open and because of this, the area is 0.0

How do I force the contour to close? or is my approach wrong?

The most important thing for me is to be able to find the area of any contour (from any image I plug in)

Here are my output image(s):

Code and source images can be found here

Why don’t you use bitwise_and and countNonZero?

1 Like

Thank you for your reply.
Unfortunately, I don’t think that countNonZero works for numpy arrays. Perhaps it requires me to plug in an image or something (I didn’t take time to study the error that I was getting)

However, I was able to use numpy’s built in function count_nonzero() to find the count of non zero arrays. Thank you!

PS: is it the same as counting the individual pixels?

It works there is a python binding in doc :

But you can use numpy or opencv I think resut will be equal :innocent:

In python an opencv image is a numpy array

1 Like