Grishelda Athallah
Grishelda Athallah

Reputation: 25

How can I accurately calculate the surface area from a binary picture in Python?

I have one image that's been divided into three parts like this , and i plan to find the surface area from each parts for further calculation.

I have tried to find its boundary using cv2.findCountours and cv.2drawContours, but when im using cv2.contourArea to calculate the surface area, the output value always showing 0,0.

for example this part im using

cr_midfoot = cv2.imread('cr_midfoot.png')

m_edged = cv2.Canny(cr_midfoot, 30, 200)
m_edged.copy()
contours_m, hierarchy_m = cv2.findContours(m_edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

cont_midfoot = cv2.drawContours(cr_midfoot, contours_m, -1, (0, 255, 0), 3)
midfoot_area = cv2.contourArea(contours_m[0])
cv2.imshow('contour midfoot', cont_midfoot)
print(midfoot_area)

but i couldnt find the information i needed, is there any way to do it? Im using OpenCV and Numpy.

Upvotes: 1

Views: 1031

Answers (1)

Michael
Michael

Reputation: 2387

You can just use the np.sum() on the desired image, which will give you the white area (because white is 1 and black is 0). In case you need the black area - use the cv2.bitwise_not(img) and then sum it up.

cr_midfoot = cv2.imread('cr_midfoot.png')
cr_midfoot_gray= cv2.cvtColor(cr_midfoot , cv2.COLOR_BGR2GRAY)
m_edged = cv2.Canny(cr_midfoot, 30, 200)
white_area  = np.sum(m_edged)

Cheers.

Upvotes: 1

Related Questions