2013-01-10 76 views
0

我有這些加速運動檢測代碼的OpenCV蟒numpy的

codebook.py

#http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.148.9778&rep=rep1&type=pdf 

import numpy as np 
import cv2 

alpha = 5 
beta = 0.95 

Tdel = 80 
Tadd = 140 
Th= 80 

mn,mx,f,l,p,q=0,1,2,3,4,5 

class CodeBook(): 
    def __init__(self,h,w): 
     self.h = h 
     self.w = w 
     self.M = np.empty((h, w), dtype=np.object) 
     self.H = np.empty((h, w), dtype=np.object) 
     filler = np.frompyfunc(lambda x: list(), 1, 1) 
     filler(self.M,self.M) 
     filler(self.H,self.H) 
     self.t = 1 

    def updatev(self,gray,cb): 
     I,t = gray,self.t  
     if not cb: 
      c = [max(0.0,I-alpha),min(255.0,I+alpha),1,t-1,t,t] 
      cb.append(c) 
     else: 
      found = False 
      for cm in cb: 
       if(cm[mn]<=I<=cm[mx] and not found): 
        cm[mn] = ((I-alpha)+(cm[f]*cm[mn]))/(cm[f]+1.0)  
        cm[mx] = ((I+alpha)+(cm[f]*cm[mx]))/(cm[f]+1.0) 
        cm[f] += 1 
        #cm[l] = max(cm[l],t-cm[q]) 
        cm[l] = 0 
        cm[q] = t 
        found = True 
       else: 
        cm[l] = max(cm[l],10-cm[q]+cm[p]-1) 
      if not found: 
       c = [max(0.0,I-alpha),min(255.0,I+alpha),1,t-1,t,t] 
       cb.append(c)     
     return cb 
    def update(self,gray):  
     h,w,M = self.h,self.w,self.M 
     updatev = np.vectorize(self.updatev,otypes=[np.object]) 
     self.M=updatev(gray,M) 
     self.t += 1 
    def fgv(self,gray,cwm,cwh): 
     I,t = gray,self.t 
     pixval = 0 
     found = False 
     for cm in cwm: 
      if(cm[mn]<=I<=cm[mx] and not found): 
       cm[mn] = (1-beta)*(I-alpha) + (beta*cm[mn]) 
       cm[mx] = (1-beta)*(I+alpha) + (beta*cm[mx]) 
       cm[f] += 1 
       #cm[l] = max(cm[l],t-cm[q]) 
       cm[l] = 0 
       cm[q] = t 
       found = True 
      else: 
       cm[l] += 1 
       #cm[l]=max(cm[l],t-cm[q]+cm[p]-1) 
     cwm[:] = [cw for cw in cwm if cw[l]<Tdel] 
     if found: return 0 
     for cm in cwh: 
      if(cm[mn]<=I<=cm[mx] and not found): 
       cm[mn] = (1-beta)*(I-alpha) + (beta*cm[mn]) 
       cm[mx] = (1-beta)*(I+alpha) + (beta*cm[mx]) 
       cm[f] += 1 
       #cm[l] = max(cm[l],t-cm[q]) 
       cm[l] = 0 
       cm[q] = t 
       found = True 
      else: 
       #cm[l]=max(cm[l],t-cm[q]+cm[p]-1) 
       cm[l] += 1 
     if not found: 
      c = [max(0.0,I-alpha),min(255.0,I+alpha),1,0,t,t] 
      cwh.append(c) 
     cwh[:] = [cw for cw in cwh if cw[l]<Th] 
     tomove = [cw for cw in cwh if cw[f]>Tadd] 
     cwh[:] = [cw for cw in cwh if not cw in tomove] 
     cwm.extend(tomove) 
     return 255 
    def fg(self,gray): 
     h,w,M,H = self.h,self.w,self.M,self.H 
     fgv = np.vectorize(self.fgv,otypes=[np.uint8]) 
     fg = fgv(gray,M,H) 
     self.t += 1 
     return fg 

test.py

import cv2 
import sys 
import numpy as np 
import time 
import cProfile 
import pyximport; pyximport.install(reload_support=True,          
            setup_args={'script_args':["--compiler=mingw32"]}) 
import codebook 
c = cv2.VideoCapture(0) 
c.set(3,320) 
c.set(4,240) 
cv2.namedWindow('vid',0) 
cv2.namedWindow('fg',0) 
_,img = c.read() 
img = cv2.resize(img,(160,120)) 
h,w = img.shape[:2] 
cb = codebook.CodeBook(h,w) 
N=0 

def fillholes(gray): 
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) 
    res = cv2.morphologyEx(gray,cv2.MORPH_OPEN,kernel)  

def run(): 
    while(1): 
     global N 
     _,img = c.read() 
     img = cv2.resize(img,(160,120)) 
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
     cv2.imshow('vid',gray)  
     if N < 10:   
      cb.update(gray) 
     else: 
      start = time.clock() 
      fg = cb.fg(gray) 
      print time.clock()-start 
      element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2)) 
      fg= cv2.erode(fg,element) 
      fillholes(fg)     
      cv2.imshow('fg',fg) 
     N += 1 
     if cv2.waitKey(5)==27: 
      break 
run() 
cv2.destroyAllWindows() 
c.release() 

代碼被卡住@ FGV。 cython可以加快它的速度。但仍然運行緩慢。我在想這樣做要麼兩個

  • 使其在平行
    • 多線程運行。我正在使用epd的numpy並將MKL_NUM_THREADS更改爲8,但它仍然綁定到單個內核。
    • 分配給工作進程陣列片
  • 重做部分/全部(哪個?我沒有EXP)部分在CPP雖然我真的想避免這種

我已經改變FGV之多因爲我知道如何。請讓我知道接下來應該看什麼。非常感謝!

回答