def replaceDefects(maindefects, conotur, olddefectspoint, newdefects):
	i = v2.getEnoughtDefectPosition(maindefects, contour, olddefectspoint)
	olddist = v2.EuclideanDistance(tuple(conotur[maindefects[i][0]][0]),tuple(conotur[maindefects[i][2]][0]))
	newdist = v2.EuclideanDistance(tuple(conotur[maindefects[i][0]][0]),tuple(conotur[newdefects[2]][0]))
	if newdist>olddist: 
		maindefects[i][2] = newdefects[2]
		maindefects[i][3] = newdefects[3]
	return maindefects, tuple(contour[maindefects[i][2]][0])
maindefects = findMainDefectsV2(contour,hull,img)
maindefects = findMainDefectsV2(contour,hull,allimg)
maindefects = findMainDefectsV2(contour,hull,hullimg) #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#maindefects = findMainDefects(contour,hull,img)
#maindefects = findMainDefects(contour,hull,allimg)
#maindefects = findMainDefects(contour,hull,hullimg) #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

cv.imwrite(path+"defects-"+str(pos)+".jpg",hullimg)
shortestimg = hullimg.copy()


#szukanie punktow charakterystycznych
#dolny prawy poczatek dłoni
rightpalmstartpoint = v2.getRightPalmStartPoint(maindefects, contour)
rightpalmstartdefect = v2.getRightPalmStartPointCloserPalmV2(maindefects[v2.getEnoughtDefectPosition(maindefects, contour, rightpalmstartpoint)], 3, contour, allimg)
#print("r",rightpalmstartpoint)
#cv.circle(allimg,rightpalmstartpoint,30,[0,150,0],-1)
#rightpalmstartdefect = v2.getRightPalmStartPointCloserPalm(maindefects[v2.getEnoughtDefectPosition(maindefects, contour, rightpalmstartpoint)], contour, 9, allimg)
maindefects,rightpalmstartpoint = replaceDefects(maindefects, contour, rightpalmstartpoint, rightpalmstartdefect)
#rightpalmstartpoint = tuple(contour[rightpalmstartdefect[2]][0])
#print("r2",rightpalmstartpoint)

#między kciukiem i prawym placem
thumbfingerpoint = v2.getDefectPointBetweenThumbAndFinger(maindefects, contour, rightpalmstartpoint)
#miedzy prawymi palcami
tworightfingerspoint = v2.getDefectPointBetweenTwoRightFingers(maindefects, contour, thumbfingerpoint)
#miedzy środkowymi palcami
centralfingerspoint = v2.getDefectPointBetweenTwoCentralFingers(maindefects, contour, thumbfingerpoint)
#między lewymi palcami
twoleftfingerspoint = v2.getDefectPointBetweenTwoLeftFingers(maindefects, contour, thumbfingerpoint)