Format du document : text/plain
Prévisualisation
def SegmentCorps(img):
img_couleur = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_couleur_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
binary_img = cv2.inRange(img_couleur_hsv, (100, 0, 80), (180, 80, 255))
binary_img2 = cv2.inRange(img_couleur_hsv, (20, 0, 80), (35, 255, 230))
img_final = cv2.bitwise_or(binary_img,binary_img2)
# plt.figure()
# plt.imshow(img_final)
# plt.show()
img_delated = cv2.dilate(img_final,kernel,iterations=4)
img_erosion = cv2.erode(img_delated, kernel, iterations=1)
closing = cv2.morphologyEx(img_delated, cv2.MORPH_CLOSE, kernel)
gradient = cv2.morphologyEx(closing, cv2.MORPH_GRADIENT, kernel)
contours, hierarchy = cv2.findContours(gradient, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = max(contours, key = cv2.contourArea)
# cv2.drawContours(img_couleur,c,-1,(255,0,0),16)
# plt.figure()
# plt.imshow(img_couleur)
# plt.show()
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
box[0,0]-=400
box[0,1]+=350
box[1,0]-=400
box[1,1]-=200
box[2,0]+=0
box[2,1]-=200
box[3,0]+=0
box[3,1]+=350
# cv2.drawContours(img_couleur,[box],0,(0,0,255),2)
# plt.figure()
# plt.imshow(img_couleur)
# plt.show()
width = int(rect[1][0])
height = int(rect[1][1])
src_pts = box.astype("float32")
dst_pts = np.array([[0, height-1],
[0, 0],
[width-1, 0],
[width-1, height-1]], dtype="float32")
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
warped = cv2.warpPerspective(img_couleur, M, (width, height))
if(warped.shape[0]>warped.shape[1]):
warped = cv2.rotate(warped,cv2.ROTATE_90_CLOCKWISE)
# plt.figure()
# plt.imshow(warped)
# plt.show()
return warped