Showing
3 changed files
with
41 additions
and
0 deletions
Prj/shape.png
0 → 100644
12 KB
Prj/signDetection.py
0 → 100644
| 1 | +import os | ||
| 2 | +import math | ||
| 3 | +import cv2 | ||
| 4 | + | ||
| 5 | +def setLabel(img, pts, label): | ||
| 6 | + (x, y, w, h) = cv2.boundingRect(pts) | ||
| 7 | + pt1 = (x, y) | ||
| 8 | + pt2 = (x + w, y + h) | ||
| 9 | + cv2.rectangle(img, pt1, pt2, (0, 255, 0), 2) | ||
| 10 | + cv2.putText(img, label, (pt1[0], pt1[1]-3), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255)) | ||
| 11 | + | ||
| 12 | +# 지금 그레이스케일이 된 사진만 인식되는 묘한 경우임 코드 확인 필요 | ||
| 13 | +#이미지 불러오기 후 그레이스케일 변환 후 이진화 | ||
| 14 | +src = cv2.imread("shape.png", cv2.IMREAD_COLOR) | ||
| 15 | + | ||
| 16 | +gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) | ||
| 17 | + | ||
| 18 | +ret, thr = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) | ||
| 19 | + | ||
| 20 | +contours, _ = cv2.findContours(thr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | ||
| 21 | + | ||
| 22 | +for cont in contours: | ||
| 23 | + approx = cv2.approxPolyDP(cont, cv2.arcLength(cont, True)*0.02, True) | ||
| 24 | + vtc = len(approx) | ||
| 25 | + | ||
| 26 | + if vtc ==3: | ||
| 27 | + setLabel(src, cont, 'Tri') | ||
| 28 | + else: | ||
| 29 | + area = cv2.contourArea(cont) | ||
| 30 | + _, radius = cv2.minEnclosingCircle(cont) | ||
| 31 | + | ||
| 32 | + ratio = radius*radius*math.pi / area | ||
| 33 | + if int(ratio) ==1: | ||
| 34 | + setLabel(src, cont, 'Cir') | ||
| 35 | + | ||
| 36 | +cv2.imshow('img', src) | ||
| 37 | +cv2.imshow('binary', thr) | ||
| 38 | + | ||
| 39 | +cv2.waitKey() | ||
| 40 | +cv2.destroyAllWindows() | ||
| 41 | + | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
Prj/test.png
0 → 100644
63.8 KB
-
Please register or login to post a comment