1

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
2
  • A good place to start is checking out how to speed up nested for loops Commented Jun 4, 2018 at 17:54
  • I don't know how to adapt it to my code. My programming skills are not so good. But I will try this. Thank you. Commented Jun 5, 2018 at 10:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.