-
-
Save furkanbakkal/8c186c70945fd8b4b4ad02c380debcb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import imutils | |
import time | |
from imutils.video import VideoStream | |
red_low = (161, 155, 84) #kırmızı rengin max ve min HSV kodları | |
red_high = (179, 255, 255) | |
xc=0 #cismin : x-merkez ve y-merkez | |
yc=0 | |
vs = VideoStream(usePiCamera=True).start() #pi kamerayı başlattık | |
time.sleep(2) | |
while True: | |
frame = vs.read() #kameradan okuma yapıyoruz | |
frame = imutils.resize(frame, width=400) #önizleme ekranı oluşturduk | |
if frame is None: | |
break | |
blurred = cv2.GaussianBlur(frame, (11, 11), 0) #görüntüyü sadeleştirmek için blur koyduk | |
w, h = frame.shape[:2] | |
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) #görüntüyü HSV cinsine çevirdik | |
mask = cv2.inRange(hsv, red_low, red_high) #HSV cinsinden tüm piksellerin renklerini kontrol ediyoruz | |
mask = cv2.erode(mask, None, iterations=2) | |
mask = cv2.dilate(mask, None, iterations=2) | |
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
cnts = imutils.grab_contours(cnts) | |
center = None | |
if len(cnts) > 0: | |
c = max(cnts, key=cv2.contourArea) | |
((x, y), radius) = cv2.minEnclosingCircle(c) | |
M = cv2.moments(c) | |
xc=int(M["m10"] / M["m00"]) | |
yc=int(M["m01"] / M["m00"]) | |
center = (xc,yc) | |
if radius > 10: #eğer kırmızı renk tespit edildiyse | |
cv2.rectangle(frame,(int(x-radius),int(y-radius)) , (int(x+radius),int(y+radius)), (0, 255, 255), 3) | |
#cisme sarı çerçeve çizdik. | |
cv2.circle(frame, center, 5, (0, 0, 255), -1) #cismin merkezine kırmızı nokta koyduk | |
print(xc,yc) | |
cv2.imshow("Frame", frame) #kapatmak için q tuşuna basın | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
vs.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment