简体   繁体   中英

Printing a matrix inside a function being called in a for loop

I am trying to build a tic tac toe game using openCV and a webcam, I have a houghcircles detection set up and can get the coordinates from that, however when I try to print the location of the circles using a 3x3 matrix I get this error.

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')

This is what I am using.

def quadrantFinder(x, y):
    # This function will take the coordinates of a circle and find its location
    if x <= 200 & y >= 300:
        arr[2][0] = 1
        print("array quadrant 6" + arr)
    else:
        print ("none of the above")
    return

arr = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])

for (x, y, r) in circles:
    # draw the circle in the output image, then draw a rectangle in the image
    # corresponding to the center of the circle
    cv2.circle(output, (x, y), r, (0, 255, 0), 4)
    cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    quadrantFinder(x, y) 

I should get something like this

[0,0,0,
 0,0,0,
 1,0,0]

solved it, the problem was I should have been returning arr in the function quadrantFinder and printing that function in for loop

so like this

def quadrantFinder(x, y):
    # This function will take the coordinates of a circle and find its location
    if x <= 200 & y >= 300:
        arr[2][0] = 1
        return arr
    else:
        return "none of the above"

then in the for loop

for (x, y, r) in circles:
    # draw the circle in the output image, then draw a rectangle in the image
    # corresponding to the center of the circle
    cv2.circle(output, (x, y), r, (0, 255, 0), 4)
    cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    print(quadrantFinder(x, y))

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