forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorphologyFeatureExtractor.py
More file actions
104 lines (89 loc) · 3.21 KB
/
Copy pathMorphologyFeatureExtractor.py
File metadata and controls
104 lines (89 loc) · 3.21 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
from SimpleCV.base import *
from SimpleCV.ImageClass import Image
from SimpleCV.Features.FeatureExtractorBase import *
from SimpleCV.Features.BlobMaker import *
class MorphologyFeatureExtractor(FeatureExtractorBase):
"""
This feature extractor collects some basic morphology infromation about a given
image. It is assumed that the object to be recognized is the largest object
in the image. The user must provide a segmented white on black blob image.
This operation then straightens the image and collects the data.
"""
mNBins = 9
mBlobMaker = None
mThresholdOpeation = None
def __init__(self, thresholdOperation=None):
"""
The threshold operation is a function of the form
binaryimg = threshold(img)
the simplest example would be:
def binarize_wrap(img):
"""
self.mNBins = 9
self.mBlobMaker = BlobMaker()
self.mThresholdOpeation = thresholdOperation
def setThresholdOperation(self, threshOp):
"""
The threshold operation is a function of the form
binaryimg = threshold(img)
Example:
>>> def binarize_wrap(img):
>>> return img.binarize()
"""
self.mThresholdOperation = threshOp
def extract(self, img):
"""
This method takes in a image and returns some basic morphology
characteristics about the largest blob in the image. The
if a color image is provided the threshold operation is applied.
"""
retVal = None
if(self.mThresholdOpeation is not None):
bwImg = self.mThresholdOpeation(img)
else:
bwImg = img.binarize()
if( self.mBlobMaker is None ):
self.mBlobMaker = BlobMaker()
fs = self.mBlobMaker.extractFromBinary(bwImg,img)
if( fs is not None and len(fs) > 0 ):
fs = fs.sortArea()
retVal = []
retVal.append(fs[0].mArea/fs[0].mPerimeter)
retVal.append(fs[0].mAspectRatio)
retVal.append(fs[0].mHu[0])
retVal.append(fs[0].mHu[1])
retVal.append(fs[0].mHu[2])
retVal.append(fs[0].mHu[3])
retVal.append(fs[0].mHu[4])
retVal.append(fs[0].mHu[5])
retVal.append(fs[0].mHu[6])
return retVal
def getFieldNames(self):
"""
This method gives the names of each field in the feature vector in the
order in which they are returned. For example, 'xpos' or 'width'
"""
retVal = []
retVal.append('area over perim')
retVal.append('AR')
retVal.append('Hu0')
retVal.append('Hu1')
retVal.append('Hu2')
retVal.append('Hu3')
retVal.append('Hu4')
retVal.append('Hu5')
retVal.append('Hu6')
return retVal
def getNumFields(self):
"""
This method returns the total number of fields in the feature vector.
"""
return self.mNBins
def __getstate__(self):
mydict = self.__dict__.copy()
self.mBlobMaker = None
del mydict['mBlobMaker']
return mydict
def __setstate__(self, mydict):
self.__dict__ = mydict
self.mBlobMaker = BlobMaker()