我將灰度圖片轉換為一個矩陣,該矩陣包含每個像素作為整數的強度值。現在我正在尋找一種方法來按其像素強度值對矩陣進行排序。以下面的矩陣為例:

輸出應該是:

我無法使用 獲得想要的結果numpy.sort,有沒有人知道如何做到這一點?
編輯:這是代碼:
import numpy as np
import matplotlib.pyplot as plt
# Pillow library, to manipulate images
from PIL import Image
# reading image with PIL module
original = Image.open('original_reduced.png')
# converting it to a numpy array. numpy arrays are easier to manipulate
im_original = np.array(original)
#plt.imshow(im_original, cmap='gray', vmin=0, vmax=255)
# size of full image
im_original.shape
## size and cropping coordinates
s = 170
x, y = 85, 85
# crops image of his face
im = im_original[y:y s, x:x s]
## im_b should be "im" sorted
im_b = ???
#print(im)
#print(im_b)
plt.imshow(im_b, cmap='gray', vmin=0, vmax=255)
plt.show()
uj5u.com熱心網友回復:
您可以reshape按類似于 Fortran 的順序對展平的陣列進行排序和原始形狀:
np.sort(a.ravel()).reshape(a.shape, order='F')
輸入:
a = np.array([[4,2,1,3],
[6,5,7,8],
[11,10,9,12]
])
輸出:
array([[ 1, 4, 7, 10],
[ 2, 5, 8, 11],
[ 3, 6, 9, 12]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348725.html
