<reponame>malidrsn/OpenCV-Tutorial-w-Basic-Examples
import cv2
import numpy as np

font = cv2.FONT_HERSHEY_SIMPLEX
font1 = cv2.FONT_HERSHEY_COMPLEX

img = cv2.imread("1.1 polygons.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
# contours bulunacaksa üstteki işlemler yapılmalı

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    epsilon = 0.01 * cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, epsilon=epsilon, closed=True)

    cv2.drawContours(img, [approx], 0, (0), 5)

    x = approx.ravel()[0]  # approx.ravel dik sayıları sütuna döker
    y = approx.ravel()[1]

    # print(approx)
    # print(len(approx))

    if len(approx) == 3:
        cv2.putText(img, "Triangle", (x, y), font, 1, (0))
    elif len(approx) == 4:
        cv2.putText(img, "Ractangle", (x, y), font1, 1, (0))
    elif len(approx) == 5:
        cv2.putText(img, "Pentagon", (x, y), font, 1, (0))
    elif len(approx) == 6:
        cv2.putText(img, "Hexagon", (x, y), font1, 1, (0))
    else:
        cv2.putText(img, "Ellipse", (x, y), font, 1, (0))

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

import cv2
import numpy as np

font = cv2.FONT_HERSHEY_SIMPLEX
font1 = cv2.FONT_HERSHEY_COMPLEX

img = cv2.imread("1.1 polygons.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
# contours bulunacaksa üstteki işlemler yapılmalı

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    epsilon = 0.01 * cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, epsilon=epsilon, closed=True)

    cv2.drawContours(img, [approx], 0, (0), 5)

    x = approx.ravel()[0]  # approx.ravel dik sayıları sütuna döker
    y = approx.ravel()[1]

    # print(approx)
    # print(len(approx))

    if len(approx) == 3:
        cv2.putText(img, "Triangle", (x, y), font, 1, (0))
    elif len(approx) == 4:
        cv2.putText(img, "Ractangle", (x, y), font1, 1, (0))
    elif len(approx) == 5:
        cv2.putText(img, "Pentagon", (x, y), font, 1, (0))
    elif len(approx) == 6:
        cv2.putText(img, "Hexagon", (x, y), font1, 1, (0))
    else:
        cv2.putText(img, "Ellipse", (x, y), font, 1, (0))

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# contours bulunacaksa üstteki işlemler yapılmalı # approx.ravel dik sayıları sütuna döker # print(approx) # print(len(approx))