def contouring(img,match_coeff):
#Defining coefficients
#----------------------------------
#Max value of contour shape matching coefficient
match_coeff = 0.1
#max contour area
max_cont_area = 100
#----------------------------------
#find countours
im2, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#truncate contours less than predefined maximum area
c_counter = 0
for c in contours:
A = cv2.contourArea(c)
if A<max_cont_area:
contours=np.delete(contours,c_counter,0)
c_counter=c_counter-1
c_counter=c_counter+1
#length of truncated contour array
clen=c_counter
#create match_array [dimension = len x len] with 0s
match_array=np.zeros((clen,clen),np.uint8)
#loop over the contours and compare two by two
icounter = 0
for i in contours:
jcounter = 0
for j in contours:
#If difference has index <0.01 then regard as TRUE
ret=cv2.matchShapes(i,j,1,0.0)
if ret<match_coeff:
match_array[icounter,jcounter]=1
else:
match_array[icounter,jcounter]=0
jcounter=jcounter+1
icounter=icounter+1
#sum each row of the array (for TRUEs and FALSEs]
sum_array=np.sum(match_array,axis=1,dtype=np.uint16)
#finding mean of the comparison value
sum_all=np.sum(sum_array,axis=0,dtype=np.uint16)
ave_sim_val=sum_all/clen
#Assumption: there is a lot of 1s
return contours,sum_array,ave_sim_val
评论列表
文章目录