0

I'm trying to convert an image into a matrix.

values = []
normal = []
for x in (arr):
    for y in (arr):
        if arr[x,y] > 1:
            normal.append(1)
        else:
            normal.append(0)

And error says:

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Thank in advance.

1 Answer 1

0

It looks like arr is a 2d array. If you want to use x, y as index and arr is a 2d array then try this:

m, n = arr.shape
for x in range(m):
    for y in range(n):
        if arr[x,y] > 1:
            normal.append(1)
        else:
            normal.append(0)

However the code doesn't look pythonic. I would use numpy.where but it is hard to help without knowing what you really want to do.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help, im really new to image processing in python so, can you give me an example where it converts the image into binary matrix?
Take a look at these threads: stackoverflow.com/questions/23225738/… and stackoverflow.com/questions/22351254/… Are you trying to do the same thing?
this is from your second comments, yes although I don't understand the first link

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.