基于计算机视觉和OpenCV：创建一个能够计算道路交通流量的应用

https://github.com/creotiv/object_detection_projects/tree/master/opencv_traffic_counting

1.了解用于前景检测的背景差分算法的主旨 2.OpenCV图像滤波器 3.物体轮廓的检测 4.为进一步的数据操作构建处理管道

foreground_objects = current_frame – background_layer（前景物体=当前帧 – 背景层）

http://docs.opencv.org/3.1.0/d7/d4d/tutorial_py_thresholding.html

http://docs.opencv.org/3.1.0/d9/d61/tutorial_py_morphological_ops.html

```def filter_mask(img):

kernel= cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))

# Fill any small holes
closing= cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# Remove noise
opening= cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)

# Dilate to merge adjacent blobs
dilation= cv2.dilate(opening, kernel, iterations=2)

# threshold
th= dilation[dilation <240]= 0

return th```

```cv2.CV_RETR_EXTERNAL — get only outer contours.
cv2.CV_CHAIN_APPROX_TC89_L1 - use Teh-Chin chain approximation algorithm (faster)
def

get_centroid(x, y, w, h):
x1= int(w/ 2)
y1= int(h/ 2)

cx= x+ x1
cy= y+ y1

return (cx, cy)

matches= []

# finding external contours
im, contours, hierarchy= cv2.findContours(

# filtering by with, height
for (i, contour)in enumerate(contours):
(x, y, w, h)= cv2.boundingRect(contour)
contour_valid= (w >= min_contour_width)and (
h >= min_contour_height)

if not contour_valid:
continue

# getting center of the bounding box
centroid= get_centroid(x, y, w, h)

matches.append(((x, y, w, h), centroid))

return matches```

https://gist.githubusercontent.com/creotiv/6db4c523ae7c64554c3d08ff5edb8e79/raw/3b3a9f7253412dab686fe935808f66eb101057ed/pipeline.py

https://gist.githubusercontent.com/creotiv/75a84e9a3f634c0f5c399bc495137075/raw/535b048a088e734e7bfcc1d14cb9e5e9cd434ebd/detection.py

```EXIT_PTS= np.array([
[[732,720], [732,590], [1280,500], [1280,720]],
[[0,400], [645,400], [645,0], [0,0]]
])

base= np.zeros(SHAPE+ (3,), dtype='uint8')

```new_pathes= []

for pathin self.pathes:
_min= 999999
_match= None
for pin points:
if len(path)== 1:
# distance from last point to current
d= utils.distance(p[0], path[-1][0])
else:
# based on 2 prev points predict next point and calculate
# distance from predicted next point to current
xn= 2 * path[-1][0][0]- path[-2][0][0]
yn= 2 * path[-1][0][1]- path[-2][0][1]
d= utils.distance(
p[0], (xn, yn),
x_weight=self.x_weight,
y_weight=self.y_weight
)

if d < _min:
_min= d
_match= p

if _matchand _min <= self.max_dst:
points.remove(_match)
path.append(_match)
new_pathes.append(path)

# do not drop path if current frame has no matches
if _matchis None:
new_pathes.append(path)

self.pathes= new_pathes

if len(points):
for pin points:
if self.check_exit(p[1]):
continue
self.pathes.append([p])

# save only last N points in path
for i, _in enumerate(self.pathes):
self.pathes[i]= self.pathes[i][self.path_size* -1:]```

```# count vehicles and drop counted pathes:
new_pathes= []
for i, pathin enumerate(self.pathes):
d= path[-2:]

if (
# need at list two points to count
len(d) >= 2 and
# prev point not in exit zone
not self.check_exit(d[0][1])and
# current point in exit zone
self.check_exit(d[1][1])and
# path len is bigger then min
self.path_size <= len(path)
):
self.vehicle_count+= 1
else:
for pin path:
if self.check_exit(p[1]):
break
new_pathes.append(path)

self.pathes= new_pathes

context['pathes']= self.pathes
context['objects']= objects
context['vehicle_count']= self.vehicle_count

self.log.debug('#VEHICLES FOUND: %s' % self.vehicle_count)

return context```

https://gist.github.com/creotiv/2998928abe5ab8606c07450965393261/raw/7b7633492d4ae0e8499e706029ceb452fe44ba60/output.py

CSV写入器是按时间保存数据的，因为我们需要将它进一步分析。因此，我使用这个公式向unixtimestamp添加额外的帧计时:

```time = ((self.start_time + int(frame_number / self.fps)) * 100
+ int(100.0 / self.fps) * (frame_number % self.fps))```

0 条评论

相关文章

能在不同的深度学习框架之间转换模型？微软的MMdnn做到了

Microsoft/MMdnn：深度学习框架随心切换 学习深度学习的各位同学都希望自己的模型能在不同的深度学习框架之间随意转换，比如，斯坦福大学CVGL实验室的...

41511

6441

A Gentle Introduction to Autocorrelation and Partial Autocorrelation (译文)

A Gentle Introduction to Autocorrelation and Partial Autocorrelation 自相关和偏自相关的简单...

3036

37310

能在不同的深度学习框架之间转换模型？微软的MMdnn做到了

Microsoft/MMdnn：深度学习框架随心切换 学习深度学习的各位同学都希望自己的模型能在不同的深度学习框架之间随意转换，比如，斯坦福大学CVGL实验室的...

5945

3948

2785

2085

4519