İki farklı işleve sahip kod nasıl birleştirilir?

sevko5885

Femtopat
Katılım
12 Kasım 2024
Mesajlar
4
Daha fazla  
Cinsiyet
Erkek
Benim biri şekil tespit biri çizgi takip olmak üzere iki kodum var bunları birleştirmem lazım asıl amaç çizgiyi takip ederken çizgi üstünde önüne çıkan şekilleri tespit etmesi bunu nasıl yapacağımı bir türlü çözemedim.
Çizgi takip;

Python:
import cv2.
import numpy as np.

cap = cv2.VideoCapture(0) # 0, varsayılan kamerayı kullanır.

# Kontrast artırma (CLAHE) için parametreler.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))

# Kamera kalibrasyonu için distorsiyon matrisleri (bunları gerçek kalibrasyonla elde edebilirsin)
# Aşağıdaki örnek, kamera distorsiyonunu düzeltmek için kullanılabilir.
# dist_coeffs = np.zeros((5, 1)) # Distorsiyon katsayıları.
# camera_matrix = np.array([[1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]) # Kamera matrisi.

while True:
 ret, frame = cap.read() # Kameradan görüntü al.
 if not ret:
 break.

 # Görüntüyü gri tonlamaya çevir.
 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

 # CLAHE ile kontrastı artır.
 gray = clahe.apply(gray)

 # Kamera distorsiyon düzeltmesi (Kamera matrisi ve distorsiyon katsayılarını kullan)
 # frame = cv2.undistort(frame, camera_matrix, dist_coeffs)

 # HSV renk uzayına dönüştür.
 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

 # Siyah renkleri filtrele (Siyah çizgiler için)
 lower_black = np.array([0, 0, 0])
 upper_black = np.array([180, 255, 50]) # Siyah için uygun aralık.
 mask = cv2.inRange(hsv, lower_black, upper_black)

 # Maskeyi uygula.
 result = cv2.bitwise_and(frame, frame, mask=mask)

 # Görüntüyü bulanıklaştır (yaklaşırken kenarları netleştirmek için)
 result = cv2.GaussianBlur(result, (5, 5), 0)

 # Canny Edge Detection.
 edges = cv2.Canny(result, 50, 150)

 # Morfolojik işlemler (gürültü temizleme)
 kernel = np.ones((5, 5), np.uint8)
 edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)

 # Konturları bul.
 contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

 if contours:
 # En büyük konturu (çizgiyi) bul.
 largest_contour = max(contours, key=cv2.contourArea)

 # Momentleri hesapla (Merkez noktasını bulmak için)
 M = cv2.moments(largest_contour)
 if M["m00"] != 0:
 cx = int(M["m10"] / M["m00"]) # X koordinatı.
 cy = int(M["m01"] / M["m00"]) # Y koordinatı.

 # Çizginin merkezine bir daire çiz.
 cv2.circle(frame, (cx, cy), 5, (0, 255, 0), -1)

 # Çizginin merkezine göre yön belirle.
 frame_center = frame.shape[1] // 2 # Ekranın ortası.
 if cx < frame_center - 50:
 direction = "SOL" # Çizgi sola kaydı, sola dön.
 elif cx > frame_center + 50:
 direction = "SAG" # Çizgi sağa kaydı, sağa dön.
 else:
 direction = "DUZ İLERLE"

 cv2.putText(frame, f"Yon: {direction}", (50, 50),
 cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

 # Görüntüyü ekranda göster.
 cv2.imshow("Underwater Line Following", frame)
 cv2.imshow("Threshold", mask)

 # 'q' tuşuna basılırsa çık.
 if cv2.waitKey(1) & 0xFF == ord('q'):
 break.

cap.release()
cv2.destroyAllWindows()

Şekil tespit;

Python:
import cv2.
import numpy as np.

framewidth = 640.
frameheight = 480.
cap = cv2.VideoCapture(0)
cap.set(3, framewidth)
cap.set(4, frameheight)

def empty(a):
 pass.

cv2.namedWindow("Parameters")
cv2.resizeWindow("Parameters", 640, 240)
cv2.createTrackbar("Threshold1", "Parameters", 150, 255, empty)
cv2.createTrackbar("Threshold2", "Parameters", 150, 255, empty)
cv2.createTrackbar("Area", "Parameters", 5000, 30000, empty)

def stackImages(scale, imgArray):
 rows = len(imgArray)
 cols = len(imgArray[0])
 rowsAvailable = isinstance(imgArray[0], list)
 width = imgArray[0][0].shape[1]
 height = imgArray[0][0].shape[0]
 if rowsAvailable:
 for x in range(rows):
 for y in range(cols):
 if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
 imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
 else:
 imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
 if len(imgArray[x][y].shape) == 2:
 imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR)
 imageBlank = np.zeros((height, width, 3), np.uint8)
 hor = [imageBlank] * rows.
 for x in range(rows):
 hor[x] = np.hstack(imgArray[x])
 ver = np.vstack(hor)
 else:
 for x in range(rows):
 if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
 imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
 else:
 imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
 if len(imgArray[x].shape) == 2:
 imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
 hor = np.hstack(imgArray)
 ver = hor.
 return ver.

def getContours(img, imgContour):
 contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 for cnt in contours:
 area = cv2.contourArea(cnt)
 areaMin = cv2.getTrackbarPos("Area", "Parameters")
 if area > areaMin:
 cv2.drawContours(imgContour, contours, -1, (255, 0, 255), 2)
 perimeter = cv2.arcLength(cnt, True)
 approx = cv2.approxPolyDP(cnt, 0.02 * perimeter, True)
 x, y, w, h = cv2.boundingRect(approx)
 cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 2)

 shape = "Bilinmiyor"
 sides = len(approx)
 if sides == 3:
 shape = "Ucgen"
 elif sides == 4:
 aspectRatio = w / float(h)
 if 0.95 < aspectRatio < 1.05:
 shape = "Kare"
 else:
 shape = "Dikdortgen"
 elif sides == 5:
 shape = "Besgen"
 elif sides == 6:
 shape = "Altigen"
 elif sides > 6:
 shape = "Daire"

 cv2.putText(imgContour, f"{shape}", (x, y - 10), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
 cv2.putText(imgContour, f"Points: {sides}", (x + w + 20, y + 20), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
 cv2.putText(imgContour, f"Area: {int(area)}", (x + w + 20, y + 45), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)

while True:
 success, img = cap.read()
 if not success:
 break.
 imgContour = img.copy()
 imgBlur = cv2.GaussianBlur(img, (7, 7), 1)
 imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)

 threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
 threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
 imgCanny = cv2.Canny(imgGray, threshold1, threshold2)
 kernel = np.ones((5, 5))
 imgDil = cv2.dilate(imgCanny, kernel, iterations=1)

 getContours(imgDil, imgContour)

 imgStack = stackImages(0.8, ([img, imgBlur, imgCanny],
 [imgDil, imgContour, imgContour]))
 cv2.imshow("Result", imgStack)
 if cv2.waitKey(1) & 0xFF == 27: # Escape key.
 break.

cap.release()
cv2.destroyAllWindows()
 
Son düzenleyen: Moderatör:

Technopat Haberler

Geri
Yukarı