Commit 2797f7eb authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

uniform LBP arguments with bob.ip.base

parent 11d0d666
...@@ -12,18 +12,21 @@ class LBPHistogram(TransformerMixin, BaseEstimator): ...@@ -12,18 +12,21 @@ class LBPHistogram(TransformerMixin, BaseEstimator):
Parameters Parameters
---------- ----------
lbptype : str lbp_type : str
The type of the LBP operator (regular, uniform or riu2) The type of the LBP operator (regular, uniform or riu2)
elbptype : str elbp_type : str
The type of extended version of LBP (regular if not extended version Which type of LBP codes should be computed; possible values: ('regular',
is used, otherwise transitional, direction_coded or modified) 'transitional', 'direction-coded'). For the old 'modified' method,
rad : float specify `elbp_type` as 'regular` and `to_average` as True.
to_average : bool
Compare the neighbors to the average of the pixels instead of the central pixel?
radius : float
The radius of the circle on which the points are taken (for circular The radius of the circle on which the points are taken (for circular
LBP) LBP)
neighbors : int neighbors : int
The number of points around the central point on which LBP is The number of points around the central point on which LBP is
computed (4, 8, 16) computed (4, 8, 16)
circ : bool circular : bool
True if circular LBP is needed, False otherwise True if circular LBP is needed, False otherwise
n_hor : int n_hor : int
Number of blocks horizontally for spatially-enhanced LBP/MCT Number of blocks horizontally for spatially-enhanced LBP/MCT
...@@ -43,11 +46,12 @@ class LBPHistogram(TransformerMixin, BaseEstimator): ...@@ -43,11 +46,12 @@ class LBPHistogram(TransformerMixin, BaseEstimator):
def __init__( def __init__(
self, self,
lbptype="uniform", lbp_type="uniform",
elbptype="regular", elbp_type="regular",
rad=1, to_average=False,
radius=1,
neighbors=8, neighbors=8,
circ=False, circular=False,
dtype=None, dtype=None,
n_hor=1, n_hor=1,
n_vert=1, n_vert=1,
...@@ -55,82 +59,40 @@ class LBPHistogram(TransformerMixin, BaseEstimator): ...@@ -55,82 +59,40 @@ class LBPHistogram(TransformerMixin, BaseEstimator):
): ):
super().__init__(**kwargs) super().__init__(**kwargs)
self.lbp_type = lbp_type
elbps = { self.elbp_type = elbp_type
"regular": "regular", self.to_average = to_average
"transitional": "trainsitional", self.radius = radius
"direction_coded": "direction-coded", self.neighbors = neighbors
"modified": "regular", self.circular = circular
}
if elbptype == "modified":
mct = True
else:
mct = False
if lbptype == "uniform":
if neighbors == 16:
lbp = LBP(
neighbors=16,
uniform=True,
circular=circ,
radius=rad,
to_average=mct,
elbp_type=elbps[elbptype],
)
else: # we assume neighbors==8 in this case
lbp = LBP(
neighbors=8,
uniform=True,
circular=circ,
radius=rad,
to_average=mct,
elbp_type=elbps[elbptype],
)
elif lbptype == "riu2":
if neighbors == 16:
lbp = LBP(
neighbors=16,
uniform=True,
rotation_invariant=True,
radius=rad,
circular=circ,
to_average=mct,
elbp_type=elbps[elbptype],
)
else: # we assume neighbors==8 in this case
lbp = LBP(
neighbors=8,
uniform=True,
rotation_invariant=True,
radius=rad,
circular=circ,
to_average=mct,
elbp_type=elbps[elbptype],
)
else: # regular LBP
if neighbors == 16:
lbp = LBP(
neighbors=16,
circular=circ,
radius=rad,
to_average=mct,
elbp_type=elbps[elbptype],
)
else: # we assume neighbors==8 in this case
lbp = LBP(
neighbors=8,
circular=circ,
radius=rad,
to_average=mct,
elbp_type=elbps[elbptype],
)
self.dtype = dtype self.dtype = dtype
self.lbp = lbp
self.n_hor = n_hor self.n_hor = n_hor
self.n_vert = n_vert self.n_vert = n_vert
self.fit()
def fit(self, X=None, y=None):
self.lbp_ = LBP(
neighbors=self.neighbors,
radius=self.radius,
circular=self.circular,
to_average=self.to_average,
uniform=self.lbp_type in ("uniform", "riu2"),
rotation_invariant=self.lbp_type == "riu2",
elbp_type=self.elbp_type,
)
return self
def __getstate__(self):
d = self.__dict__.copy()
d.pop("lbp_")
return d
def __setstate__(self, state):
self.__dict__.update(state)
self.fit()
def comp_block_histogram(self, data): def comp_block_histogram(self, data):
""" """
Extracts LBP/MCT histograms from a gray-scale image/block. Extracts LBP/MCT histograms from a gray-scale image/block.
...@@ -153,12 +115,10 @@ class LBPHistogram(TransformerMixin, BaseEstimator): ...@@ -153,12 +115,10 @@ class LBPHistogram(TransformerMixin, BaseEstimator):
assert isinstance(data, np.ndarray) assert isinstance(data, np.ndarray)
# allocating the image with lbp codes # allocating the image with lbp codes
lbpimage = np.ndarray(self.lbp.lbp_shape(data), "uint16") lbpimage = np.ndarray(self.lbp_.lbp_shape(data), "uint16")
self.lbp(data, lbpimage) # calculating the lbp image self.lbp_(data, lbpimage) # calculating the lbp image
hist = histogram( hist = histogram(lbpimage, (0, self.lbp_.max_label - 1), self.lbp_.max_label)
lbpimage, (0, self.lbp.max_label - 1), self.lbp.max_label hist = hist / np.sum(hist) # histogram normalization
)
hist = hist / sum(hist) # histogram normalization
if self.dtype is not None: if self.dtype is not None:
hist = hist.astype(self.dtype) hist = hist.astype(self.dtype)
return hist return hist
...@@ -204,6 +164,3 @@ class LBPHistogram(TransformerMixin, BaseEstimator): ...@@ -204,6 +164,3 @@ class LBPHistogram(TransformerMixin, BaseEstimator):
def _more_tags(self): def _more_tags(self):
return {"stateless": True, "requires_fit": False} return {"stateless": True, "requires_fit": False}
def fit(self, X, y=None):
return self
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment