Haar-like特徵
阿新 • • 發佈:2018-11-14
haar特徵多用於人臉檢測、行人檢測等;Haar-like特徵可以理解為卷積模板;該模板的特徵值為白色矩形畫素和減去黑色矩形畫素和,反映了影象的灰度變化情況,但矩形特徵只對一些簡單的圖形結構,如邊緣、線段較敏感,所以只能描述特定走向(水平、垂直、對角)的結構。
Haar-like特徵分類
Haar-like特徵可以分為四類:線性特徵、邊緣特徵、點特徵(中心特徵)、對角線特徵:
積分圖
計算haar特徵值
def intergral(self,image):
height,weigth = np.shape( image)
output = np.zeros((height + 1,weigth + 1))
for h in range(1,height + 1):
columnSum = np.zeros((weigth + 1))
for w in range(1,weigth + 1):
columnSum[w] = columnSum[w - 1] + image[h - 1][w - 1] # 每行的列合計
output[h] [w] = output[h - 1][w] + columnSum[w]
return output
def HaarRectPyramidUp(self,imageSize,minSize=1,minDeep=2):
size = minSize
deep = minDeep
yield size,deep
while True:
if size < deep:
size = size + 1
deep = size * minDeep
else:
deep = deep + 1
size = deep * minSize
if max(size,deep) > imageSize:
break
if size == deep and size * 2 > imageSize:
break
yield size,deep
def Haar2RectLR(self, sum, minSize=1, minDeep=2):
'''
b | d| e|
--------------------
| white | black |
--------------------
c | white a| black f|
# white= a+ b-c-d
'''
height,weigth = np.shape(sum)
feature_map = []
for size,deep in self.HaarRectPyramidUp(weigth - 1,minSize,minDeep):
for y in range(1,height - deep + 1):
for x in range(1,weigth - 2 * size + 1):
by = dy = ey = y - 1
cy = ay = fy = y + deep - 1
bx = cx = x - 1
dx = ax = x + size - 1
ex = fx = x + size * 2 - 1
white = sum[ay][ax] + sum[by][bx] - sum[cy][cx] - sum[dy][dx]
black = sum[fy][fx] + sum[dy][dx] - sum[ay][ax] - sum[ey][ex]
feature_map.append(int(white) - int(black))
return feature_map
def GetHaarFeatureDescriptor(self,images,imgSize):
features = []
for fn in images:
# 讀入彩色影象,並轉換為灰度值影象
img = cv2.imread(fn, cv2.IMREAD_GRAYSCALE)
# 圖片拉伸
img = cv2.resize(img, imgSize, interpolation=cv2.INTER_CUBIC)
# 歸一化
img = img / float(np.max(img)) * 255
# 積分圖
intim = self.intergral(img)
# 垂直方向邊緣特徵
feature = self.Haar2RectLR(intim)
features.append(feature)
return features