Skip to content

Commit e65a070

Browse files
committed
图像相减_camera
1 parent 6a79777 commit e65a070

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

ch05-视频/5.VideoPlay.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
frame_no = 121
2525
cap.set(1, frame_no) # Where frame_no is the frame you want
2626
ret, frame = cap.read() # Read the frame
27-
cv2.imshow('frame', frame)
27+
cv2.imshow('frame_no'+str(frame_no), frame)
2828

2929
FRAME_NOW = cap.get(cv2.CAP_PROP_POS_FRAMES)
3030
print('当前帧数', FRAME_NOW) # 当前帧数 122.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/24 下午5:20
3+
# @Author : play4fun
4+
# @File : 图像相减_camera.py
5+
# @Software: PyCharm
6+
7+
"""
8+
图像相减_camera.py:
9+
"""
10+
11+
import cv2
12+
import numpy as np
13+
14+
cap = cv2.VideoCapture(0)
15+
ret = cap.set(3, 640)
16+
ret = cap.set(4, 480)
17+
18+
19+
20+
cap.read()
21+
cap.read()
22+
cap.read()
23+
cap.read()
24+
cap.read()
25+
cap.read()
26+
cap.read()
27+
cap.read()
28+
cap.read()
29+
cap.read()
30+
cap.read()
31+
cap.read()
32+
'''
33+
cal=[cap.read()[1] for x in range(20)]
34+
35+
#mean 直接的加减是不行的
36+
# bgimg0=np.mean(np.sum(cal))
37+
# bgimg0=np.average(cal)
38+
# bgimg0=np.mean(cal)
39+
nps1=sum(cal)
40+
mean1=nps1/len(cal)
41+
# mean1[mean1<0]=0
42+
# mean1[mean1>255]=255
43+
cv2.imshow('bgimg', mean1)
44+
cv2.waitKey(0)
45+
exit(3)
46+
'''
47+
48+
frame_no=100
49+
# cap.set(1, frame_no)#第10帧
50+
ret, bgimg0 = cap.read() # 背景
51+
bgimg = cv2.cvtColor(bgimg0, cv2.COLOR_BGR2GRAY)
52+
cv2.imshow('bgimg'+str(frame_no), bgimg0)
53+
54+
55+
while cap.isOpened():
56+
ret, frame = cap.read()# TODO 图像稳定
57+
cv2.imshow("frame", frame)
58+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
59+
60+
#
61+
st = cv2.subtract(gray, bgimg)
62+
# st = cv2.subtract(img1, img2)#相反
63+
# st[st <= 5] = 0 # 把小于20的像素点设为0
64+
65+
ret, threshold = cv2.threshold(st, 50, 255, cv2.THRESH_BINARY)
66+
67+
#TODO 对比前几/十几帧,新放一张扑克,知道是那张
68+
#等待图像稳定,不放牌后,再计算
69+
70+
71+
cv2.imshow("subtract", st)
72+
cv2.moveWindow("subtract",y=bgimg.shape[0], x=0)
73+
cv2.imshow('threshold',threshold)
74+
cv2.moveWindow("threshold", x=bgimg.shape[1],y=0)
75+
76+
key = cv2.waitKey(delay=1)
77+
if key == ord("q"):
78+
break
79+
80+
cv2.destroyAllWindows()

ch41-背景减除/41.2-BackgroundSubtractorMOG.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
cv2.imshow('frame', fgmask)
3131
k = cv2.waitKey(1) #& 0xff
32-
if k == 27:
32+
if k == ord('q'):
3333
break
3434
cap.release()
3535
cv2.destroyAllWindows()

ch41-背景减除/41.3-BackgroundSubtractorMOG2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
cv2.imshow('frame', fgmask)
3030
k = cv2.waitKey(30) #& 0xff
31-
if k == 27:
31+
if k == ord('q'):
3232
break
3333
cap.release()
3434
cv2.destroyAllWindows()

ch41-背景减除/41.4-morphologyEx.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
print(counter)
3232

3333
k = cv2.waitKey(1) # & 0xff
34-
if k == 27:
34+
if k == ord('q'):
3535
break
3636
cap.release()
3737
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)