简体   繁体   中英

Python ValueError: operands could not be broadcast together with shapes (5,4) (5,5)

So I've been working on a project, but have been stuck on one step. I'm using a series of for loops to iterate a matrix over another, bigger matrix. My entire code works up to the last step, where it gives me a ValueError, saying ValueError: operands could not be broadcast together with shapes (5,4) (5,5)

I understand that this is because the matrices are not the same size- I think the error is in my padding. Could someone please look through this and tell me my error? I'm going crazy trying to figure this out.

def haar_feature_generator(image_array, haar_matrices_list):

    rows, columns = image_array.shape
    output_matrix = numpy.zeros((rows, columns))
    numpy.pad(output_matrix, (4,4), 'constant', constant_values=0)

    for y in range(0, rows):
        for x in range(0, columns):
            for haar_matrix in haar_matrices_list:
                rows_1, columns_1 = haar_matrix.shape
                output_matrix[y, x] = numpy.multiply((image_array[y:(y + rows_1) , x:(x + columns_1)]), haar_matrix).sum()

    output_array = numpy.asarray(abs(output_matrix))
    return output_array

hm_list_1 = ([numpy.matrix('1,1;-1,-1'), numpy.matrix('1,1,1 ; 1,1,-1 ; 1,-1,-1'), numpy.matrix('-1,-1,-1 ; -1,1,1 ; 1,1,1')])
hm_list_2 = ([numpy.matrix('1,1,1 ; 1,1,-1 ; -1,-1,-1'), numpy.matrix('-1,-1,-1 ; -1,-1,-1 ; 1,1,1')])
hm_list_3 = ([numpy.matrix('1,1,1,1 ; 1,1,1,1 ; 1,1,1,-1 ; -1,-1,-1,-1'), numpy.matrix('1,1,1,1 ; 1,1,-1,-1 ; -1,-1,-1,-1 ; -1,-1,-1,-1')])
hm_list_4 = ([numpy.matrix('1,1,1,1 ; 1,-1,-1,-1 ; 1,1,1,-1 ; -1,-1,-1,-1'), numpy.matrix('1,1,1,-1 ; 1,1,-1,-1 ; 1,-1,-1,-1 ; -1,-1,-1,-1')])
hm_list_5 = ([numpy.matrix('1,1,1,1,1 ; 1,1,-1,-1,-1 ; 1,1,1,-1,-1 ; 1,1,1,1,-1 ; -1,-1,-1,-1,-1')])

haar_matrices_list = hm_list_1 + hm_list_2 + hm_list_3 + hm_list_4 + hm_list_5
haar_feature_generator(image_array, haar_matrices_list)

Never mind, solved it. Hope this helps someone who had the same problem I did.

def haar_feature_generator(image_array, haar_matrices_list):

    rows, columns = image_array.shape
    output_matrix = numpy.zeros((rows, columns))

    for haar_matrix in haar_matrices_list:
        numpy.pad(output_matrix, len(haar_matrix[0]), 'constant', constant_values=0)
        for y in range(0, rows):
            for x in range(0, columns):
                rows_1, columns_1 = haar_matrix.shape
                output_matrix[y, x] = numpy.multiply((image_array[y:(y + rows_1) , x:(x + columns_1)]), haar_matrix).sum()

        output_array = numpy.asarray(abs(output_matrix))
        return output_array

hm_list_1 = ([numpy.matrix('1,1;-1,-1'), numpy.matrix('1,1,1 ; 1,1,-1 ; 1,-1,-1'), numpy.matrix('-1,-1,-1 ; -1,1,1 ; 1,1,1')])
hm_list_2 = ([numpy.matrix('1,1,1 ; 1,1,-1 ; -1,-1,-1'), numpy.matrix('-1,-1,-1 ; -1,-1,-1 ; 1,1,1')])
hm_list_3 = ([numpy.matrix('1,1,1,1 ; 1,1,1,1 ; 1,1,1,-1 ; -1,-1,-1,-1'), numpy.matrix('1,1,1,1 ; 1,1,-1,-1 ; -1,-1,-1,-1 ; -1,-1,-1,-1')])
hm_list_4 = ([numpy.matrix('1,1,1,1 ; 1,-1,-1,-1 ; 1,1,1,-1 ; -1,-1,-1,-1'), numpy.matrix('1,1,1,-1 ; 1,1,-1,-1 ; 1,-1,-1,-1 ; -1,-1,-1,-1')])
hm_list_5 = ([numpy.matrix('1,1,1,1,1 ; 1,1,-1,-1,-1 ; 1,1,1,-1,-1 ; 1,1,1,1,-1 ; -1,-1,-1,-1,-1')])

haar_matrices_list = hm_list_1 + hm_list_2 + hm_list_3 + hm_list_4 + hm_list_5

output_matrix = haar_feature_generator(image_array, haar_matrices_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM