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

[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()

#### 人群密度估计--Crowd Counting Via Scale-adaptive Convolutional Nerual Network

2017-12-15 15:06:22

#### 浅入浅出TensorFlow 8 - 行人分割

2017-04-20 23:41:16

#### 图像分割与FCN

2017-05-11 21:55:24

#### 行人分割DPM

2017年10月02日 23.1MB 下载

#### tensorflow 优化器optimizer

2017-10-17 13:36:06

#### 生成对抗网络的简单介绍（TensorFlow 代码）

2017-05-15 12:15:11

#### 核密度函数加权直方图的计算

2014-04-26 10:23:15

#### python核密度估计(KernelDensity)

2017-06-29 20:42:19

#### 非参数估计：核密度估计KDE

2016-12-14 11:38:46

#### 密度分析（七） Python实现

2016-12-16 13:55:12