import pygame, math
from random import randrange
class Yildiz:
def __init__(self, num_stars, max_depth):
pygame.init()
self.screen = pygame.display.set_mode((640, 480))
self.clock = pygame.time.Clock()
self.num_stars = num_stars
self.max_depth = max_depth
self.init_stars()
def init_stars(self):
self.stars = []
for i in range(self.num_stars):
star = [randrange(-25,25), randrange(-25,25), randrange(1, self.max_depth)]
self.stars.append(star)
def move_and_draw_stars(self):
orjin_x = self.screen.get_width() / 2
orjin_y = self.screen.get_height() / 2
for star in self.stars:
star[2] -= 0.19
if star[2] <= 0:
star[0] = randrange(-25,25)
star[1] = randrange(-25,25)
star[2] = self.max_depth
k = 128.0 / star[2]
x = int(star[0] * k + orjin_x)
y = int(star[1] * k + orjin_y)
if 0 <= x < self.screen.get_width() and 0 <= y < self.screen.get_height():
size = (1 - float(star[2]) / self.max_depth) * 5
shade = (1 - float(star[2]) / self.max_depth) * 255
self.screen.fill((shade,shade,shade),(x,y,size,size))
def run(self):
while 1:
self.clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
self.screen.fill((0,0,0))
self.move_and_draw_stars()
pygame.display.flip()
if __name__ == "__main__":
Yildiz(512, 32).run()
import cv2
import numpy as np
WINDOW_NAME = 'Mavi kapak tespit edici'
def track(image):
blur = cv2.GaussianBlur(image, (5,5),0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
mavi = np.array([0,21,38])
mavi2 = np.array([18,255,255])
mask = cv2.inRange(hsv, mavi, mavi2)
bmask = cv2.GaussianBlur(mask, (5,5),0)
moments = cv2.moments(bmask)
m00 = moments['m00']
centroid_x, centroid_y = None, None
if m00 != 0:
centroid_x = int(moments['m10']/m00)
centroid_y = int(moments['m01']/m00)
ctr = (-1,-1)
if centroid_x != None and centroid_y != None:
ctr = (centroid_x, centroid_y)
cv2.circle(image, ctr, 20, (255,255,0), 30)
cv2.imshow(WINDOW_NAME, image)
if cv2.waitKey(1) & 0xFF == 27:
ctr = None
return ctr
if __name__ == '__main__':
capture = cv2.VideoCapture(0)
while True:
okay, image = capture.read()
if okay:
if not track(image):
break
if cv2.waitKey(1) & 0xFF == 27:
break
else:
print('hata olustu')
break
Öyle şeyler bi ara düşünüyorum ama şimdilik yapabildiğim bu.Hoş duruyor, daha önce uğraşmıştım bunlarla
bir alternatif kod starfield için
Eğer böyle şeylerle uğraşıyorsan opencv lib'ine de göz atmanı tavsiye ederim, gayet zevkliKod:import pygame, math from random import randrange class Yildiz: def __init__(self, num_stars, max_depth): pygame.init() self.screen = pygame.display.set_mode((640, 480)) self.clock = pygame.time.Clock() self.num_stars = num_stars self.max_depth = max_depth self.init_stars() def init_stars(self): self.stars = [] for i in range(self.num_stars): star = [randrange(-25,25), randrange(-25,25), randrange(1, self.max_depth)] self.stars.append(star) def move_and_draw_stars(self): orjin_x = self.screen.get_width() / 2 orjin_y = self.screen.get_height() / 2 for star in self.stars: star[2] -= 0.19 if star[2] <= 0: star[0] = randrange(-25,25) star[1] = randrange(-25,25) star[2] = self.max_depth k = 128.0 / star[2] x = int(star[0] * k + orjin_x) y = int(star[1] * k + orjin_y) if 0 <= x < self.screen.get_width() and 0 <= y < self.screen.get_height(): size = (1 - float(star[2]) / self.max_depth) * 5 shade = (1 - float(star[2]) / self.max_depth) * 255 self.screen.fill((shade,shade,shade),(x,y,size,size)) def run(self): while 1: self.clock.tick(50) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return self.screen.fill((0,0,0)) self.move_and_draw_stars() pygame.display.flip() if __name__ == "__main__": Yildiz(512, 32).run()
Tutoriallerden anladıgım kadarıyla yapmış oldugum bir mavi kapak tracking sistemi
Kırmızı rengi takip edip mouse konumunu ona göre değiştiren bir sistem yapabilirsin mesela, lazer ile mouse takibi yapılır falan çok hoş şeyler.Kod:import cv2 import numpy as np WINDOW_NAME = 'Mavi kapak tespit edici' def track(image): blur = cv2.GaussianBlur(image, (5,5),0) hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) mavi = np.array([0,21,38]) mavi2 = np.array([18,255,255]) mask = cv2.inRange(hsv, mavi, mavi2) bmask = cv2.GaussianBlur(mask, (5,5),0) moments = cv2.moments(bmask) m00 = moments['m00'] centroid_x, centroid_y = None, None if m00 != 0: centroid_x = int(moments['m10']/m00) centroid_y = int(moments['m01']/m00) ctr = (-1,-1) if centroid_x != None and centroid_y != None: ctr = (centroid_x, centroid_y) cv2.circle(image, ctr, 20, (255,255,0), 30) cv2.imshow(WINDOW_NAME, image) if cv2.waitKey(1) & 0xFF == 27: ctr = None return ctr if __name__ == '__main__': capture = cv2.VideoCapture(0) while True: okay, image = capture.read() if okay: if not track(image): break if cv2.waitKey(1) & 0xFF == 27: break else: print('hata olustu') break