LBP implementation in Python

时间:2018-06-04 17:47:15

标签: python image-processing machine-learning computer-vision lbph-algorithm

I'm implementing the LBP characteristics for images with Python. I'm using it for clasifying pedestrians but I don't obtain good results and it lasts too long because of for loops nested (about 3 hours).

Can anyone see a mistake in the implementation?

And any suggestions for make it more efficient?

The function is this:

I'm using blocks of 16x16 and moving it 8 pixels.

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

0 个答案:

没有答案