forked from wjb711/Python_learn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_0.py
More file actions
86 lines (75 loc) · 2.73 KB
/
background_0.py
File metadata and controls
86 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import cv2
import operator
from pandas import Series,DataFrame
import pandas as pd
cap = cv2.VideoCapture(0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
fgbg =cv2.createBackgroundSubtractorKNN(history = 1, dist2Threshold=30,detectShadows=1)
while cv2.waitKey(1)!=27:
ret, frame = cap.read()
#gray=cv2.cvtColor(frame,6)
blur = cv2.GaussianBlur(frame,(5,5),0)
canny=cv2.Canny(blur,50,100)
cv2.imshow('canny',canny)
fgmask = fgbg.apply(canny)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
blur1 = cv2.GaussianBlur(fgmask,(5,5),0)
cv2.imshow('blur1',blur1)
ret,thresh=cv2.threshold(fgmask,200,255,cv2.THRESH_BINARY)
cv2.imshow('frame1',thresh)
image, cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#cnts = max(cnts, key=cv2.contourArea)
#cnts.index( max(cnts, key=cv2.contourArea) )
#max_index, max_value = max(cnts, key=cv2.contourArea)
#print max_index
#cv2.drawContours(frame, cnts, 0, (0,255,255), 5)
#print 'max is ',max(cnts, key=cv2.contourArea)
#cv2.imshow('frame0',frame)
#print 'max is ',max(cv2.contourArea(contours))
#print max(cv2.contourArea(c) for c in cnts)
#if cnts:
# print cnts
f1=frame.copy()
#len0=len(contours)
#print len0
#i=0
#max=2000
#print 'len of cnts is ',len(cnts)
max=2000
#i=0
bg=np.zeros((480,640,3), np.int8)
for i in range(len(cnts)):
if cv2.contourArea(cnts[i]) > max:
max = cv2.contourArea(cnts[i])
maxIndex = i
bg=cv2.drawContours(bg, cnts, i, (255,255,255), -1)
#x,y,w,h = cv2.boundingRect(cnts[i])
#centerX=(x+w)/2
#centerY=(y+h)/2
#print 'x is ',x,' y is ',y,' w is ',w,' h is ',h
#f2=frame[y:480,x:640]
#frame[0:h*2,0:w*2]=frame[y:y+h,x:x+w]
#cv2.imshow('hello',f2)
#frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
#print 'max is ',max,'i is ',i
cv2.imshow('frame0',frame)
cv2.imshow('bg',bg)
#cv2.imshow('blur',f2)
# cnt = contours[i]
# if cv2.contourArea(cnt) >2000:
# if cv2.contourArea(cnt) >max
# max=cv2.contourArea(cnt)
# cv2.drawContours(f1, contours, i, (0,255,255), 5)
#print cnt(i)
# i=i+1
#for cnt in contours:
# if cv2.contourArea(cnt) >2000:
# cv2.drawContours(f1, cnt, 5, (0,255,255), 5)
#cv2.imshow('frame0',f1)
#fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
#cv2.imshow('frame2',fgmask)
#bitwise=cv2.bitwise_and(frame,frame,mask=fgmask)
#cv2.imshow('bitwise',bitwise)
cap.release()
cv2.destroyAllWindows()