我正在使用 Python 实现图像的 LBP 特性。我用它来对行人进行分类,但我没有得到很好的结果,而且由于嵌套了 for 循环(大约 3 小时),它持续时间太长。
任何人都可以看到实施中的错误吗?
有什么建议可以提高效率吗?
功能是这样的:
我正在使用 16x16 的块并将其移动 8 个像素。
def LBP_img_basic(img):
size2, size1 = img.shape
numbers = []
dx = 8
dy = 8
cell_x = 16
cell_y = 16
x = 0
y = 0
hist_list = []
while y + cell_y <= size2:
for i in range(y, y + cell_y):
for j in range(x, x + cell_x):
hood = np.zeros((3,3), dtype = int)
if j == 0 and i == 0:
hood[1:3, 1:3] = img[i:i+2, j:j+2]
elif i == 0 and j == size1 - 1:
hood[1:3, 0:2] = img[i:i+2, j-1:j+1]
elif j == 0 and i == size2 - 1:
hood[0:2, 1:3] = img[i-1:i+1, j:j+2]
elif i == size2 - 1 and j == size1 - 1:
hood[0:2, 0:2] = img[i-1 : i+1, j-1:j+1]
elif i == 0:
hood[1:3,0:3] = img[i:i+2, j-1:j+2]
elif j == 0:
hood[0:3, 1:3] = img[i-1:i+2, j:j+2]
elif i == size2 - 1:
hood[0:2, 0:3] = img[i-1 : i+1, j-1:j+2]
elif j == size1 - 1:
hood[0:3, 0:2] = img[i-1 : i+2, j-1:j+1]
else:
hood = img[i-1 : i+2, j-1:j+2]
ordered_hood = np.concatenate((hood[0], [hood[1,2], hood[2,2], hood[2,1], hood[2,0], hood[1,0]]))
for k in range(len(ordered_hood)):
if ordered_hood[k] < hood [1,1]:
ordered_hood[k] = 0
else:
ordered_hood[k] = 1
binary = ""
for digit in ordered_hood:
binary += str(digit)
integer = int(binary, 2)
numbers.append(integer)
hist = np.zeros(256)
for l in numbers:
hist[l] += 1
hist_list = np.concatenate((hist_list, hist))
if x + dx + cell_x > size1:
x = 0
y = y + dy
else:
x = x + dx
return hist_list