我想繪制一些字母并根據字母的寬度和高度將它們分隔 4 個相等的區域。碰巧一切看起來都很好,直到一個區域有空白像素,例如 zone_0.jpg、zone_7 和 zone_12,如下圖所示。我怎樣才能使影像 2d 陣列的每個值都有 255 的影像框列印為白色?

到目前為止,這是我的代碼:
half_width = int(np.floor(width / 2))
half_height = int(np.floor(height / 2))
z11 = boxed[:half_height, :half_width]
z12 = boxed[:half_height, half_width:]
z21 = boxed[half_height:, :half_width]
z22 = boxed[half_height:, half_width:]
# plotting by zone
plt.figure(figsize=(4, 4), tight_layout=True)
plt.subplot(221)
plt.imshow(z11, cmap='gray')
plt.title('z11')
plt.subplot(222)
plt.imshow(z12, cmap='gray')
plt.title('z12')
plt.subplot(223)
plt.imshow(z21, cmap='gray')
plt.title('z21')
plt.subplot(224)
plt.imshow(z22, cmap='gray')
plt.title('z22')
plt.savefig(f'{zone_path}/zone_{i}.jpg')
plt.close()
因此,每個區域變數 ( z11, z12, z21, z22) 都是 2d numpy 陣列,其中每個單元格可以包含 0(將輸出黑色)或 255(將輸出白色)。
例如,關注zone_0.jpg,該z22變數將僅包含值為 255 的單元格:

但z11將包含 0 或 255 個值的混合:

從邏輯上講,應該沒有問題,因為 255 應該始終輸出為白色,所以我想知道為什么會發生這種行為?
uj5u.com熱心網友回復:
默認情況下,Matplotlib 總是縮放顏色圖,以便顏色圖最小顏色是資料最小值,顏色圖最大顏色是資料最大值。您可以只使用 0 和 1 作為資料值并獲得相同的影像。如果您有資料 min = max,則顯然設定了比例以使顯示與顏色圖 min 匹配。
要使用當前 0, 255 資料斷言顏色圖比例,請將以下內容添加到每個繪圖呼叫:vmin=0, vmax=255. 這些強制顏色圖范圍,而不是使用傳遞給繪圖呼叫的資料。有關詳細資訊,請參閱matplotlib.pyplot.imshow 檔案- 所有支持 cmap 的繪圖函式都支持這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457258.html
標籤:Python matplotlib 颜色图
下一篇:如何在直方圖中添加范圍?
