shape detection
import cv2
import numpy as np
# pip install opencv-python numpy
# https://www.emgu.com/wiki/images/Opencvpic3sample.pngdef process_image(img):
h, w = img.shape[:2]
# Siyah tuvaller oluştur (Görselleri üzerine çizmek için)
triangle_rectangle_image = np.zeros((h, w, 3), dtype=np.uint8)
circle_image = np.zeros((h, w, 3), dtype=np.uint8)
line_image = np.zeros((h, w, 3), dtype=np.uint8)
# 1. Gri tonlama ve Gürültü Azaltma
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 1)
# 2. Daire Tespiti
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=2.0, minDist=20.0,
param1=180, param2=120, minRadius=5, maxRadius=100)
# 3. Kenar ve Çizgi Tespiti
canny_edges = cv2.Canny(gray, 180, 120)
lines = cv2.HoughLinesP(canny_edges, 1, np.pi / 45.0, threshold=20,
minLineLength=30, maxLineGap=10)
# 4. Kontur Analizi (Üçgen ve Dikdörtgen)
contours, _ = cv2.findContours(canny_edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
epsilon = 0.05 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
area = cv2.contourArea(approx)
if area > 250:
if len(approx) == 3: # Üçgen
cv2.drawContours(triangle_rectangle_image, [approx], 0, (139, 0, 0), 2)
elif len(approx) == 4: # Dikdörtgen
rect = cv2.minAreaRect(approx)
box = cv2.boxPoints(rect)
# box noktaları float (ondalıklı) döner, ancak çizim için tam sayı gerekir.
# 'astype(int)' hem temizdir hem de her sürümde çalışır.
box = box.astype(int)
# cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
cv2.drawContours(triangle_rectangle_image, [box], 0, (0, 140, 255), 2)
# 5. Çizimleri Uygulama
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(circle_image, (i[0], i[1]), i[2], (42, 42, 165), 2)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_image, (x1, y1), (x2, y2), (0, 128, 0), 2)
# Etiketleme
for frame, label in zip([triangle_rectangle_image, circle_image, line_image],
["Triangles/Rects", "Circles", "Lines"]):
cv2.putText(frame, label, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 200, 200), 2)
# Dikey birleştirme
return cv2.vconcat([img, triangle_rectangle_image, circle_image, line_image])
# --- ANA PROGRAM ---
# 1. Resmi yükle (Dosya yolunu kendine göre düzenle)
image_path = "Opencvpic3sample.png"
input_img = cv2.imread(image_path)
if input_img is None:
print("Hata: Resim bulunamadı! Lütfen dosya yolunu kontrol edin.")
else:
# 2. İşlemi gerçekleştir
final_result = process_image(input_img)
# 3. Sonucu göster
# Ekran boyutuna sığması için gerekirse yeniden boyutlandırabiliriz
screen_res = 1280, 720
scale_width = screen_res[0] / final_result.shape[1]
scale_height = screen_res[1] / final_result.shape[0]
scale = min(scale_width, scale_height)
if scale < 1:
window_width = int(final_result.shape[1] * scale)
window_height = int(final_result.shape[0] * scale)
final_result = cv2.resize(final_result, (window_width, window_height))
cv2.imshow("Geometri Tespit Sonucu", final_result)
print("Çıkmak için bir tuşa basın...")
cv2.waitKey(0) # Bir tuşa basana kadar bekler
cv2.destroyAllWindows()
Yorumlar
Yorum Gönder