Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
BSH_project2
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Ryan
2021-04-10 18:18:40 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
60ec24e19146968281cf5b2ebc0b3080dfebd609
60ec24e1
1 parent
9c68785d
etc: add motion_crop
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
etc/motion_crop.py
etc/motion_crop.py
0 → 100644
View file @
60ec24e
import
cv2
import
numpy
as
np
cap
=
cv2
.
VideoCapture
(
'./videos/test.mp4'
)
#load video
count1
=
0
#counts number of frames
#capture two frames
rval
,
frame1
=
cap
.
read
()
rval
,
frame2
=
cap
.
read
()
while
cap
.
isOpened
():
diff
=
cv2
.
absdiff
(
frame1
,
frame2
)
#difference of two frames
gray
=
cv2
.
cvtColor
(
diff
,
cv2
.
COLOR_BGR2GRAY
)
#convert diff into greyscale
blur
=
cv2
.
GaussianBlur
(
gray
,
(
5
,
5
),
0
)
#blur image to reduce noise
_
,
thresh
=
cv2
.
threshold
(
blur
,
20
,
255
,
cv2
.
THRESH_BINARY
)
#set threshold
dilated
=
cv2
.
dilate
(
thresh
,
None
,
iterations
=
3
)
#fill holes in the thresholded image
#find contour
contours
,
_
=
cv2
.
findContours
(
dilated
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
count2
=
0
#counts number of motions in a frame
#further implementation(removing, rectangle)
for
contour
in
contours
:
(
x
,
y
,
w
,
h
)
=
cv2
.
boundingRect
(
contour
)
#values of contour
if
cv2
.
contourArea
(
contour
)
<
7000
:
#to remove small unnecessary contours, (modify number for better results)
continue
filename
=
"clip{}-{}.jpg"
.
format
(
count1
,
count2
)
motion
=
frame1
[
y
:
y
+
h
,
x
:
x
+
w
]
cv2
.
imwrite
(
filename
,
motion
)
count2
+=
1
#cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 0, 255), 2) #draw rectangle on frame1
count1
+=
1
#show results
#cv2.imshow("feed", frame1)
#change to next frame
frame1
=
frame2
rval
,
frame2
=
cap
.
read
()
#incase of error
if
cv2
.
waitKey
(
40
)
==
27
:
break
cap
.
release
()
cv2
.
destroyAllWindows
()
\ No newline at end of file
Please
register
or
login
to post a comment