consolidate all repos to one for archive
This commit is contained in:
91
semester_4/osnove_racunalniskega_vida/Naloga_4/work.py
Normal file
91
semester_4/osnove_racunalniskega_vida/Naloga_4/work.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
events = [i for i in dir(cv2) if 'EVENT' in i]
|
||||
|
||||
from implement import MeanShift, CamShift
|
||||
|
||||
|
||||
global xLZ
|
||||
global yLZ
|
||||
global xLast
|
||||
global yLast
|
||||
global frame
|
||||
xLZ = 0
|
||||
yLZ = 0
|
||||
frame = None
|
||||
def clickOnImage(event, x, y, flags, param):
|
||||
global frame, xLZ, yLZ, xLast, yLast
|
||||
if event == cv2.EVENT_LBUTTONUP:
|
||||
#print("EVENT_LBUTTONUP ({},{})".format(x,y))
|
||||
xLZ = 0
|
||||
yLZ = 0
|
||||
|
||||
if event == cv2.EVENT_LBUTTONDOWN:
|
||||
#print("EVENT_LBUTTONDOWN ({},{})".format(x,y))
|
||||
xLZ = x
|
||||
yLZ = y
|
||||
|
||||
if event == cv2.EVENT_MOUSEMOVE:
|
||||
#print("EVENT_MOUSEMOVE ({},{})".format(x,y))
|
||||
xLast = x
|
||||
yLast = y
|
||||
|
||||
capture = cv2.VideoCapture(0)
|
||||
cv2.namedWindow("Slika")
|
||||
cv2.setMouseCallback("Slika", clickOnImage)
|
||||
miniFrame = None
|
||||
track_window = None
|
||||
while(1):
|
||||
ret, frame = capture.read()
|
||||
if ret == True:
|
||||
if xLZ != 0 or yLZ != 0:
|
||||
cv2.rectangle(frame,(xLZ,yLZ),(xLast,yLast),(0,255,0),2)
|
||||
|
||||
cv2.imshow('Slika',frame)
|
||||
if cv2.waitKey(100) & 0xFF == ord("q"):
|
||||
cv2.destroyAllWindows()
|
||||
break
|
||||
if cv2.waitKey(100) & 0xFF == ord("r"):
|
||||
miniFrame = frame[yLZ:yLast,xLZ:xLast]
|
||||
track_window = (xLZ, yLZ, xLast-xLZ, yLast-yLZ)
|
||||
cv2.imshow("Mini",miniFrame)
|
||||
|
||||
if miniFrame is not None:
|
||||
|
||||
|
||||
roi = miniFrame
|
||||
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
|
||||
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
|
||||
|
||||
|
||||
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
|
||||
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
|
||||
|
||||
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
|
||||
|
||||
|
||||
while(1):
|
||||
ret, frame = capture.read()
|
||||
frame = cv2.flip(frame,1)
|
||||
if ret == True:
|
||||
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
|
||||
dst = cv2.threshold(dst, 2, 255, cv2.THRESH_BINARY)[1]
|
||||
cv2.imshow("dst",dst)
|
||||
# apply meanshift to get the new location
|
||||
#ret, track_window = MeanShift(dst, track_window, term_crit)
|
||||
|
||||
ret, track_window = CamShift(dst, track_window, term_crit)
|
||||
|
||||
# Draw it on image
|
||||
x,y,w,h = track_window
|
||||
img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
|
||||
cv2.imshow('img2',img2)
|
||||
k = cv2.waitKey(30) & 0xff
|
||||
if k == 27:
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
capture.release()
|
||||
Reference in New Issue
Block a user