# 基于非参数核密度估计的行人分割

[1] L. Zhao, L.S. Davis, “Iterative figure-ground discrimination,” 17th International Conference on Pattern Recognition (ICPR), vol. 1, pp. 67-70, 2004.

[2] Automatic Pedestrian Segmentation Combining Shape, Puzzle and Appearance，2013

# 算法步骤：

Ft(x)$F_t(x)$Bt(x)$B_t(x)$分别为像素x在第t(t=0,…,N)次迭代中属于前景和背景的概率， 基于KDE-EM的前景概率估计过程如下：

## 初始化：

F0(x)=PM(x)$F_0(x)=P_M(x)$
B0(x)=1F0$B_0(x)=1-F_0$

## 开始迭代，设置迭代次数：

### M-步骤：

#### 计算前景和背景概率：

Ft=cFt1(y)xiXFt1(xi)j=1dkerj(yjxi,j)

Bt=cBt1(y)xiXBt1(xi)j=1dkerj(yjxi,j)

x——采样点
y——图像中所有的点

F=FF+B

B=1F

# 代码：

# -*- coding: utf-8 -*-
# Author: XieYi

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import math
from sklearn import preprocessing
from skimage import filters
import matplotlib.cm as cm
import time
"""

"""
img = Image.open("p26.bmp")
img = img.convert('L')
img = np.array(img,'f')
img = img/img.max()
m,n = img.shape
imgVector = img.reshape((m*n,1))

# 参数
c = 1.0
sigma = 0.005   # 控制边界精度，即模糊程度sigma越大越模糊
sampleRate = 0.05

"""

"""
#将前三列置0

# 采样点,初始化F0和B0
nSamples = int(sampleRate * m * n)
F0 = np.zeros((m*n,1))
F0max = F0.max()
F0 = F0 / F0max
B0 = 1-F0
F, B = F0, B0
#迭代
t0 = time.time()
for i in np.arange(6):
print str(i+1),"次迭代...."
#S步
samples = np.zeros((nSamples,1))
for j in np.arange(nSamples):
y = np.random.randint(0, m-1)
x = np.random.randint(0, n-1)
samples[j,0] = y * n + x

#M步
f = np.zeros((m*n,1))
b = np.zeros((m*n,1))
for Xi in np.arange(nSamples):
posSample = samples[Xi,0]
valueSample = imgVector[samples[Xi,0],0]
diffMat = imgVector - np.tile(valueSample,(m*n,1))
diffMat  = diffMat**2
expDiff = np.exp(-diffMat / 2.0 / (sigma**2))
f = f + c * (F[posSample,0]) / math.sqrt(2*math.pi) / sigma * expDiff

f = f * F
min_max_scaler = preprocessing.MinMaxScaler()
f = min_max_scaler.fit_transform(f)

for Yi in np.arange(nSamples):
posSample = samples[Yi,0]
valueSample = imgVector[samples[Yi,0],0]
diffMat = imgVector - np.tile(valueSample,(m*n,1))
diffMat  = diffMat**2
expDiff = np.exp(-diffMat / 2.0 / (sigma**2))
b = b + c * (B[posSample,0]) / math.sqrt(2*math.pi) / sigma * expDiff
b = b * B
b = min_max_scaler.fit_transform(b)

b = 1 - f
F, B = f, b
output = F.reshape((m,n))
plt.imsave(str(i+1), output,  cmap=cm.gray)
plt.figure(str(i+1))
plt.imshow(output,cmap=cm.gray)

print "time:",time.time() - t0
thresh = filters.threshold_otsu(output)
dst =(output >= thresh)*1.0
plt.figure("output")
plt.imshow(dst,cmap=cm.gray)

# 直接利用阈值分割
threshImg = filters.threshold_otsu(img)
dstImg =(img >= threshImg)*1.0
plt.figure("Img")
plt.imshow(dstImg,cmap=cm.gray)
plt.show()

• 本文已收录于以下专栏：

举报原因： 您举报文章：基于非参数核密度估计的行人分割 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)