Skip to content

Instantly share code, notes, and snippets.

@akirayou
Created November 8, 2018 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirayou/7324df566d139ed4721681fde0d5b148 to your computer and use it in GitHub Desktop.
Save akirayou/7324df566d139ed4721681fde0d5b148 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 8 21:50:03 2018
@author: akira
"""
import cv2
import numpy as np
CV_CAP_PROP_POS_FRAMES = 1
nofMarker=100
s_limit=0.3
v_limit=0.05
cv2.namedWindow("result", cv2.WINDOW_NORMAL)
cv2.namedWindow("raw", cv2.WINDOW_NORMAL)
cap = cv2.VideoCapture("tt.mp4")
for i in range(3):
ret, frame = cap.read()
frame=frame.astype(np.float32)/255
#detect positions
img= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img=cv2.GaussianBlur(img,(21,21),0)
img = img - cv2.GaussianBlur(img,(51,51),0)
buf=np.copy(img)
pos=[]
for i in range(nofMarker):
min_val, max_val, min_loc, max_loc=cv2.minMaxLoc(buf)
cv2.circle(buf,max_loc,20,0,-1)
pos.append(np.array(max_loc))
del buf
show=np.copy(img)
for i in range(nofMarker):
cv2.circle(show,tuple(pos[i]),10,255,1)
cv2.imshow("detected positon",show)
cap.set(CV_CAP_PROP_POS_FRAMES,1)
of=np.empty( (frame.shape[0]*2,frame.shape[1],3))
cv2.imshow("result",of)
cv2.waitKey(10000)
while(True):
ret,f=cap.read()
if(not ret):
break
cap.set(CV_CAP_PROP_POS_FRAMES,1)
ret,f=cap.read()
cv2.imshow("raw",f)
org=f=f.astype(np.float32)/255
f=cv2.GaussianBlur(f,(21,21),0)
f = f - cv2.GaussianBlur(f,(51,51),0)
f/=np.max(f)
#read Color
rep=[]
for p in pos:
hwin=5
pmin=p-hwin
pmax=p+hwin
pmin[pmin<0]=0
if(pmax[0]>f.shape[1]):pmax[0]=f.shape[1]
if(pmax[1]>f.shape[0]):pmax[1]=f.shape[0]
a=f[pmin[1]:pmax[1],:,:][:,pmin[0]:pmax[0],:]
a=np.mean(a.reshape(-1,3),axis=0)
rep.append(a)
rep=np.array(rep).reshape(1,-1,3)
hsv = cv2.cvtColor(rep, cv2.COLOR_BGR2HSV).reshape(-1,3)
h=hsv[:,0]
s=hsv[:,1]
v=hsv[:,2]
for i in range(nofMarker):
if(s[i]>s_limit and v[i] >v_limit): #十分に輝度があって、彩度も高い
#h=100 Green h=0 Red
gd=h[i]-100
if(gd>180):gd-=360
rd=h[i]-0
if(rd>180):rd-=360
col=(0,0,1)
if(abs(gd)<abs(rd) ):col=(0,1,0)
cv2.circle(f,tuple(pos[i]),10,col,2 )
of[:frame.shape[0],:,:]=org
of[frame.shape[0]:,:,:]=f
cv2.imshow("result",of)
if('q' == cv2.waitKey(1)):break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment