import numpy as np
import cv2
im = cv2. imread( 'zhua.jpg' )
imgray = cv2. cvtColor( im, cv2. COLOR_BGR2GRAY)
ret, thresh = cv2. threshold( imgray, 100 , 255 , 0 )
img, contours, hierarchy = cv2. findContours( thresh, cv2. RETR_TREE, cv2. CHAIN_APPROX_SIMPLE)
img= cv2. drawContours( img, contours, - 1 , ( 0 , 255 , 0 ) , 3 )
cnt = contours[ - 1 ]
cv2. destroyAllWindows( )
hull = cv2. convexHull( cnt, returnPoints= False )
defects = cv2. convexityDefects( cnt, hull)
print ( defects[ 0 , 0 , 0 ] )
203
import cv2
import numpy as np
img = cv2. imread( 'zhua.jpg' )
img_gray = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
ret, thresh = cv2. threshold( img_gray, 100 , 255 , 0 )
img2, contours, hierarchy = cv2. findContours( thresh, 2 , 1 )
cnt = contours[ - 1 ]
hull = cv2. convexHull( cnt, returnPoints = False )
defects = cv2. convexityDefects( cnt, hull)
for i in range ( defects. shape[ 0 ] ) :
s, e, f, d = defects[ i, 0 ]
start = tuple ( cnt[ s] [ 0 ] )
end = tuple ( cnt[ e] [ 0 ] )
far = tuple ( cnt[ f] [ 0 ] )
cv2. line( img, start, end, [ 0 , 255 , 0 ] , 2 )
cv2. circle( img, far, 5 , [ 0 , 0 , 255 ] , - 1 )
cv2. imshow( 'img' , img)
cv2. waitKey( 0 )
cv2. destroyAllWindows( )
dist= cv2. pointPolygonTest( cnt, ( 500 , 500 ) , True )
print ( dist)
-238.9016534057477
import cv2
import numpy as np
img1 = cv2. imread( 'zhua.jpg' , 0 )
img2 = cv2. imread( 'shou.jpg' , 0 )
ret, thresh = cv2. threshold( img1, 100 , 255 , 0 )
ret, thresh2 = cv2. threshold( img2, 127 , 255 , 0 )
_, contours, hierarchy = cv2. findContours( thresh, 2 , 1 )
cnt1 = contours[ 0 ]
_, contours, hierarchy = cv2. findContours( thresh2, 2 , 1 )
cnt2 = contours[ 0 ]
ret = cv2. matchShapes( cnt1, cnt2, 1 , 0.0 )
print ( ret)
0.06213205685249473