A
A
Adik Izat2020-05-01 15:06:39
Python
Adik Izat, 2020-05-01 15:06:39

How to reverse a python matrix?

out_gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY) # черно-белая матрица картинки type = tuple

out_gray_reverse = [] # реверсированная матрица (объявление)

for i in range(0, len(out_gray) - 1): # перебор первого уровня
    for j in range(0, len(out_gray[i]) - 1): #  перебор второго уровня
        out_gray_reverse[j][i] = out_gray[i][j]

What is going to happen?
This:

turn into this:

My code above is throwing an error
Traceback (most recent call last):
  File "img.py", line 64, in <module>
    out_gray_reverse[j][i] = out_gray[i][j]
IndexError: list index out of range

The matrix above is just an example. And what really: we have a matrix of 131x1026, but we should have 1026x131.
len(out_gray) # 131
len(out_gray[0]) # 1026

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2020-05-01
@JaxAdam

How to reverse

What you want to do (judging by the example) is called Transpose.
your_matrix.T
>>> x
array()
>>> x.T
array()

D
Drill, 2020-05-02
@Drill

out_gray_reverse = list(zip(*out_gray))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question