1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import numpy as np
import cv2
def coordonnees():
image = cv2.imread('/home/jooker/Bureau/Appli/images/scan01.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,250,255,cv2.THRESH_TRUNC)
contours,h =cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
perimetre=cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.01*perimetre,True)
M = cv2.moments(cnt)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.drawContours(image,[cnt],-1,(255,200,0),2)
if len(approx)==3:
shape = "triangle"
elif len(approx)==4:
(x, y, w, h) = cv2.boundingRect(approx)
ratio = w / float(h)
if not(0.95 <= ratio <= 1.05):
shape = "rectangle"
else:
shape = ""
else:
shape= "circle"
cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,0.5, (0, 0, 0), 2)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
coordonnees() |