1.代码链接:https://github.com/uoip/KCFpy
2.代码修改下:直接用没用起来,各种小问题,python版本不同造成的。
2.1fhog代码
import numpy as np
import cv2
from numba import jit
# constant
NUM_SECTOR = 9
FLT_EPSILON = 1e-07
@jit
def func1(dx, dy, boundary_x, boundary_y, height, width, numChannels):
r = np.zeros((height, width), np.float32)
alfa = np.zeros((height, width, 2), np.int)
for j in range(1, height-1):
for i in range(1, width-1):
c = 0
x = dx[j, i, c]
y = dy[j, i, c]
r[j, i] = np.sqrt(x*x + y*y)
for ch in range(1, numChannels):
tx = dx[j, i, ch]
ty = dy[j, i, ch]
magnitude = np.sqrt(tx*tx + ty*ty)
if(magnitude > r[j, i]):
r[j, i] = magnitude
c = ch
x = tx
y = ty
mmax = boundary_x[0]*x + boundary_y[0]*y
maxi = 0
for kk in range(0, NUM_SECTOR):
dotProd = boundary_x[kk]*x + boundary_y[kk]*y
if(dotProd > mmax):
mmax = dotProd
maxi = kk
elif(-dotProd > mmax):
mmax = -dotProd
maxi = kk + NUM_SECTOR
alfa[j, i, 0] = maxi % NUM_SECTOR
alfa[j, i, 1] = maxi
return r, alfa
@jit
def func2(dx, dy, boundary_x, boundary_y, r, alfa, nearest, w, k, height, width, sizeX, sizeY, p, stringSize):
mapp = np.zeros((sizeX*sizeY*p), np.float32)
for i in range(sizeY):
for j in range(sizeX):
for ii in range(k):
for jj in range(k):
if((i * k + ii > 0) and (i * k + ii < height - 1) and (j * k + jj > 0) and (j * k + jj < width - 1)):
mapp[i*stringSize + j*p + alfa[k*i+ii,j*k+jj,0]] += r[k*i+ii,j*k+jj] * w[ii,0] * w[jj,0]
mapp[i*stringSize + j*p + alfa[k*i+ii,j*k+jj,1] + NUM_SECTOR] += r[k*i+ii,j*k+jj] * w[ii,0] * w[jj,0]
if((i + nearest[ii] >= 0) and (i + nearest[ii] <= sizeY - 1)):
mapp[(i+nearest[ii])*stringSize + j*p + alfa[k*i+ii,j*k+jj,0]] += r[k*i+ii,j*k+jj] * w[ii,1] * w[jj,0]
mapp[(i+nearest[ii])*stringSize + j*p + alfa[k*i+ii,j*k+jj,1] + NUM_SECTOR] += r[k*i+ii,j*k+jj] * w[ii,1] * w[jj,0]
if((j + nearest[jj] >= 0) and (j + nearest[jj] <= sizeX - 1)):
mapp[i*stringSize + (j+nearest[jj])*p + alfa[k*i+ii,j*k+jj,0]] += r[k*i+ii,j*k+jj] * w[ii,0] * w[jj,1]
mapp[i*stringSize + (j+nearest[jj])*p + alfa[k*i+ii,j*k+jj,1] + NUM_SECTOR] += r[k*i+ii,j*k+jj] * w[ii,0] * w[jj,1]
if((i + nearest[ii] >= 0) and (i + nearest[ii] <= sizeY - 1) and (j + nearest[jj] >= 0) and (j + nearest[jj] <= sizeX - 1)):
mapp[(i+nearest[ii])*stringSize + (j+nearest[jj])*p + alfa[k*i+ii,j*k+jj,0]] += r[k*i+ii,j*k+jj] * w[ii,1] * w[jj,1]
mapp[(i+nearest[ii])*stringSize + (j+nearest[jj])*p + alfa[k*i+ii,j*k+jj,1] + NUM_SECTOR] += r[k*i+ii,j*k+jj] * w[ii,1] * w[jj,1]
return mapp
@jit
def func3(partOfNorm, mappmap, sizeX, sizeY, p, xp, pp):
newData = np.zeros((sizeY*sizeX*pp), np.float32)
for i in range(1, sizeY+1):
for j in range(1, sizeX+1):
pos1 = i * (sizeX+2) * xp + j * xp
pos2 = (i-1) * sizeX * pp + (j-1) * pp
valOfNorm = np.sqrt(partOfNorm[(i )*(sizeX + 2) + (j )] +
partOfNorm[(i )*(sizeX + 2) + (j + 1)] +
partOfNorm[(i + 1)*(sizeX + 2) + (j )] +
partOfNorm[(i + 1)*(sizeX + 2) + (j + 1)]) + FLT_EPSILON
newData[pos2:pos2+p] = mappmap[pos1:pos1+p] / valOfNorm
newData[pos2+4*p:pos2+6*p] = mappmap[pos1+p:pos1+3*p] / valOfNorm
valOfNorm = np.sqrt(partOfNorm[(i )*(sizeX + 2) + (j )] +
partOfNorm[(i )*(sizeX + 2) + (j + 1)] +
partOfNorm[(i - 1)*(sizeX + 2) + (j )] +
partOfNorm[(i - 1)*(sizeX + 2) + (j + 1)]) + FLT_EPSILON
newData[pos2+p:pos2+2*p] = mappmap[pos1:pos1+p] / valOfNorm
newData[pos2+6*p:pos2+8*p] = mappmap[pos1+p:pos1+3*p] / valOfNorm
valOfNorm = np.sqrt(partOfNorm[(i )*(sizeX + 2) + (j )] +
partOfNorm[(i )*(sizeX + 2) + (j - 1)] +
partOfNorm[(i + 1)*(sizeX + 2) + (j )] +
partOfNorm[(i + 1)*(sizeX + 2) + (j - 1)]) + FLT_EPSILON
newData[pos2+2*p:pos2+3*p] = mappmap[pos1:pos1+p] / valOfNorm
newData[pos2+8*p:pos2+10*p] = mappmap[pos1+p:pos1+3*p] / valOfNorm
valOfNorm = np.sqrt(partOfNorm[(i )*(sizeX + 2) + (j )] +
partOfNorm[(i )*(sizeX + 2) + (j - 1)] +
partOfNorm[(i - 1)*(sizeX + 2) + (j )] +
partOfNorm[(i - 1)*(sizeX + 2) + (j - 1)]) + FLT_EPSILON
newData[pos2+3*p:pos2+4*p] = mappmap[pos1:pos1+p] / valOfNorm
newData[pos2+10*p:pos2+12*p] = mappmap[pos1+p:pos1+3*p] / valOfNorm
return newData
@jit
def func4(mappmap, p, sizeX, sizeY, pp, yp, xp, nx, ny):
newData = np.zeros((sizeX*sizeY*pp), np.float32)
for i in range(sizeY):
for j in range