forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMOGSegmentation.py
More file actions
136 lines (109 loc) · 4.01 KB
/
Copy pathMOGSegmentation.py
File metadata and controls
136 lines (109 loc) · 4.01 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from SimpleCV.Features import Feature, FeatureSet, BlobMaker
from SimpleCV.ImageClass import Image
from SimpleCV.Segmentation.SegmentationBase import SegmentationBase
class MOGSegmentation(SegmentationBase):
"""
Background subtraction using mixture of gausians.
For each pixel store a set of gaussian distributions and try to fit new pixels
into those distributions. One of the distributions will represent the background.
history - length of the pixel history to be stored
nMixtures - number of gaussian distributions to be stored per pixel
backgroundRatio - chance of a pixel being included into the background model
noiseSigma - noise amount
learning rate - higher learning rate means the system will adapt faster to new backgrounds
"""
mError = False
mDiffImg = None
mColorImg = None
mReady = False
# OpenCV default parameters
history = 200
nMixtures = 5
backgroundRatio = 0.7
noiseSigma = 15
learningRate = 0.7
bsMOG = None
def __init__(self, history = 200, nMixtures = 5, backgroundRatio = 0.7, noiseSigma = 15, learningRate = 0.7):
try:
import cv2
except ImportError:
raise ImportError("Cannot load OpenCV library which is required by SimpleCV")
return
if not hasattr(cv2, 'BackgroundSubtractorMOG'):
raise ImportError("A newer version of OpenCV is needed")
return
self.mError = False
self.mReady = False
self.mDiffImg = None
self.mColorImg = None
self.mBlobMaker = BlobMaker()
self.history = history
self.nMixtures = nMixtures
self.backgroundRatio = backgroundRatio
self.noiseSigma = noiseSigma
self.learningRate = learningRate
self.mBSMOG = cv2.BackgroundSubtractorMOG(history, nMixtures, backgroundRatio, noiseSigma)
def addImage(self, img):
"""
Add a single image to the segmentation algorithm
"""
if( img is None ):
return
self.mColorImg = img
self.mDiffImg = Image(self.mBSMOG.apply(img.getNumpyCv2(), None, self.learningRate), cv2image=True)
self.mReady = True
return
def isReady(self):
"""
Returns true if the camera has a segmented image ready.
"""
return self.mReady
def isError(self):
"""
Returns true if the segmentation system has detected an error.
Eventually we'll consruct a syntax of errors so this becomes
more expressive
"""
return self.mError #need to make a generic error checker
def resetError(self):
"""
Clear the previous error.
"""
self.mError = false
return
def reset(self):
"""
Perform a reset of the segmentation systems underlying data.
"""
self.mModelImg = None
self.mDiffImg = None
def getRawImage(self):
"""
Return the segmented image with white representing the foreground
and black the background.
"""
return self.mDiffImg
def getSegmentedImage(self, whiteFG=True):
"""
Return the segmented image with white representing the foreground
and black the background.
"""
return self.mDiffImg
def getSegmentedBlobs(self):
"""
return the segmented blobs from the fg/bg image
"""
retVal = []
if( self.mColorImg is not None and self.mDiffImg is not None ):
retVal = self.mBlobMaker.extractFromBinary(self.mDiffImg ,self.mColorImg)
return retVal
def __getstate__(self):
mydict = self.__dict__.copy()
self.mBlobMaker = None
self.mDiffImg = None
del mydict['mBlobMaker']
del mydict['mDiffImg']
return mydict
def __setstate__(self, mydict):
self.__dict__ = mydict
self.mBlobMaker = BlobMaker()