我有一個簡單的 2d numpy 數組,它是灰度圖像的像素圖。我正在嘗試打印圖像的某些部分。我的代碼是from google.colab import drivedrive.mount('/content/drive')import numpy as npimport matplotlib.pyplot as pltimport cv2img = cv2.imread('/content/drive/My Drive/Colab Notebooks/sample2.jpg') # the source file is correctly mountedimg_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)和i = 0while i < (len(roi) - 1): # roi is a list of strictly increasing positive integers print(roi[i], roi[i+1]) plt.imshow(img_gray[roi[i]:roi[i+1]], cmap='gray') i += 1例如,如果roi = [10, 40, 50, 100],它應該打印圖像的兩個部分。但是當我運行上面的單元格時,它只打印一張圖像,即圖像的最后一部分。是否可以不覆蓋其他圖像并將其全部打印?
1 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
您應該嘗試plt.show()在每個之后調用plt.imshow(...):
i = 0
while i < (len(roi) - 1): # roi is a list of strictly increasing positive integers
print(roi[i], roi[i+1])
plt.imshow(img_gray[roi[i]:roi[i+1]], cmap='gray')
plt.show() # <----- this will show all plots
i += 1
或者,如果你想保留一個更好、更有組織的圖,你可以使用子圖,盡管你應該說明你想要多少個子圖,下面是一個隨機輸入的示例:
import matplotlib.pyplot as plt
import numpy as np
ims = np.random.randn(3, 224, 224)
fig, ax = plt.subplots(1, 3)
for i in range(3):
ax[i].imshow(ims[i])
最后一個示例將繪制水平排列的圖像:
添加回答
舉報
0/150
提交
取消