Skip to content

Commit bfd3797

Browse files
area,perimeter,center,radius and curvature of objects or contours
1 parent aad474b commit bfd3797

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Area, Perimeter, Center, Curvature
2+
import numpy as np
3+
import cv2
4+
5+
img=cv2.imread('detect_blob.png')
6+
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
7+
thresh=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,115,1)
8+
_,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
9+
10+
img2=img.copy()
11+
color=(123,255,61)
12+
index=-1
13+
tk=2
14+
frame=np.zeros([img.shape[0],img.shape[1],3],'uint8')
15+
i=1
16+
for c in contours:
17+
cv2.drawContours(frame,[c],index,color,tk)
18+
area=cv2.contourArea(c)
19+
arclength=cv2.arcLength(c,True)
20+
print('{} Area: {}, perimeter: {}'.format(i,area,arclength))
21+
i+=1
22+
m=cv2.moments(c)
23+
cx=int(m['m10']/m['m00'])
24+
cy=int(m['m01']/m['m00'])
25+
cv2.circle(frame,(cx,cy),1,(0,0,255),-1)
26+
cv2.imshow('Original',frame)
27+
28+
cv2.waitKey(0)
29+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)