Xoay ma trận 180 độ python

Chỉ định

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
4 sẽ được xoay làm đối số đầu tiên của
print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
3. Mảng quay 90 độ ngược chiều kim đồng hồ

import numpy as np

a_2d = np.arange[6].reshape[2, 3]
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

a_2d_rot = np.rot90[a_2d]
print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]

nguồn. numpy_rot90. py

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
3 trả về một lượt xem. Vì một khung nhìn chia sẻ bộ nhớ với mảng ban đầu, nên việc thay đổi một giá trị sẽ thay đổi giá trị kia

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]

nguồn. numpy_rot90. py

Nếu bạn muốn xử lý dưới dạng dữ liệu riêng biệt, hãy sử dụng

a_2d_rot_copy = np.rot90[a_2d].copy[]
print[a_2d_rot_copy]
# [[2 5]
#  [1 4]
#  [0 3]]

print[np.shares_memory[a_2d, a_2d_rot_copy]]
# False
2

a_2d_rot_copy = np.rot90[a_2d].copy[]
print[a_2d_rot_copy]
# [[2 5]
#  [1 4]
#  [0 3]]

print[np.shares_memory[a_2d, a_2d_rot_copy]]
# False

nguồn. numpy_rot90. py

Chỉ định số lần quay. k

Chỉ định một giá trị số nguyên cho đối số thứ hai

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
6 xoay mảng 90 độ ngược chiều kim đồng hồ
print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
6 lần

print[np.rot90[a_2d, 2]]
# [[5 4 3]
#  [2 1 0]]

print[np.rot90[a_2d, 3]]
# [[3 0]
#  [4 1]
#  [5 2]]

print[np.rot90[a_2d, 4]]
# [[0 1 2]
#  [3 4 5]]

print[np.rot90[a_2d, 100]]
# [[0 1 2]
#  [3 4 5]]

nguồn. numpy_rot90. py

Trong trường hợp giá trị âm, hướng quay theo chiều kim đồng hồ

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
0

nguồn. numpy_rot90. py

Đối với mảng một chiều

Mảng một chiều không xoay được

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
1

nguồn. numpy_rot90. py

Nó có thể được xoay nếu được định nghĩa là một mảng hai chiều chỉ có một hàng

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
2

nguồn. numpy_rot90. py

Liên kết được tài trợ

Đối với mảng nhiều chiều

Các mảng đa chiều có ba chiều trở lên cũng có thể được xoay

hành vi mặc định

Theo mặc định, nó được xoay như sau

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
3

nguồn. numpy_rot90. py

Nó được quay trong một mặt phẳng bao gồm hai trục [kích thước] đầu tiên

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
4

nguồn. numpy_rot90. py

Chỉ định mặt phẳng để xoay. trục

Đối với mảng nhiều chiều, đối số thứ ba

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
7 có thể chỉ định một mặt phẳng để xoay. Trong
print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
7, chỉ định hai trục tạo thành một mặt phẳng với một bộ hoặc danh sách có hai phần tử

Giá trị mặc định là

a_2d_rot_copy = np.rot90[a_2d].copy[]
print[a_2d_rot_copy]
# [[2 5]
#  [1 4]
#  [0 3]]

print[np.shares_memory[a_2d, a_2d_rot_copy]]
# False
7, quay trong mặt phẳng của hai trục đầu tiên. Có thể khẳng định rằng kết quả giống như ví dụ trên

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
8

nguồn. numpy_rot90. py

Một ví dụ về quay trong một mặt phẳng khác như sau. Nếu thứ tự của các trục được chỉ định trong

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
7 bị đảo ngược, hướng quay sẽ bị đảo ngược

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
0

nguồn. numpy_rot90. py

Nó cũng có thể được chỉ định cùng với đối số thứ hai

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
6

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
1

nguồn. numpy_rot90. py

Xoay hình ảnh

Các tệp hình ảnh có thể được đọc dưới dạng mảng NumPy

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
4 bằng các thư viện như Gối [PIL] và OpenCV

  • Xử lý ảnh với Python, NumPy
  • Đọc và lưu file ảnh với Python, OpenCV [imread, imwrite]

Có thể xoay hình ảnh bằng cách sử dụng

print[np.shares_memory[a_2d, a_2d_rot]]
# True

a_2d_rot[0, 0] = 100
print[a_2d_rot]
# [[100   5]
#  [  1   4]
#  [  0   3]]

print[a_2d]
# [[  0   1 100]
#  [  3   4   5]]

a_2d[0, 2] = 2
print[a_2d]
# [[0 1 2]
#  [3 4 5]]

print[a_2d_rot]
# [[2 5]
#  [1 4]
#  [0 3]]
3. Ví dụ sau sử dụng ảnh màu [mảng ba chiều] nhưng ảnh xám [mảng hai chiều] cũng không cần chỉ định đối số nào

Chủ Đề