İmport CV2.
İmport numpy as np.
Def detect_shapes(image):
Gray = CV2.cvtcolor(image, CV2.color_bgr2gray)
Blurred = CV2.gaussianblur(gray, (5, 5), 0)
Edges = CV2.canny(blurred, 50, 150)
Contours, _ = CV2.findcontours(edges, CV2.retr_external, CV2.chaın_approx_sımple)
For contour in contours:
İf CV2.contourarea(contour) < 500: # küçük konturları yok say.
Continue.
Approx = CV2.approxpolydp(contour, 0.04 * CV2.arclength(contour, true), true)
X, Y, W, h = CV2.boundingrect(approx)
Shape_name = "unknown"
İf len(approx) == 3:
Shape_name = "triangle"
Elif len(approx) == 4:
Shape_name = "square"
Elif len(approx) == 5:
Shape_name = "pentagon"
Elif len(approx) == 6:
Shape_name = "hexagon"
Else:
Shape_name = "circle"
CV2.puttext(image, shape_name, (X, y - 10), CV2.font_hershey_sımplex, 0.6, (0, 255, 0), 2)
CV2.drawcontours(image, [approx], -1, (0, 255, 0), 2)
Def main():
# -- Coding: Utf-8 --
İmage_path = "C:\\Users\\a.jpg"
İmage = CV2.imread(image_path)
İf image is none:
Print("resim yüklenemedi!")
Return.
Detect_shapes(image)
CV2.imshow("shape detection", image)
CV2.waitkey(0)
cv2.destroyAllWindows()
İf name == "main":
main()