簡體   English   中英

Matplotlib - 如何重新調整 RGB 圖像的像素強度

[英]Matplotlib - how to rescale pixel intensities for RGB image

我對 matplotlib 如何處理 fp32 像素強度感到困惑。 據我了解,它會重新調整圖像最大值和最小值之間的值。 但是,當我嘗試通過使用 imshow() 將像素強度重新縮放到 [-1,1](通過 im*2-1)來查看最初在 [0,1] 中的圖像時,圖像的顏色會有所不同。 如何重新縮放以使圖像不不同?

編輯:請看圖片-

PS:我需要將此作為輸出 [-1,1] 中這些值的程序的一部分

以下是用於此的代碼:

img = np.float32(misc.face(gray=False))
fig,ax = plt.subplots(1,2)
img = img/255 # Convert to 0,1 range
print (np.max(img), np.min(img))    
img0 = ax[0].imshow(img)
plt.colorbar(img0,ax=ax[0])
print (np.max(2*img-1), np.min(2*img-1))
img1 = ax[1].imshow(2*img-1)  # Convert to -1,1 range
plt.colorbar(img1,ax=ax[1])
plt.show() 

最大、最小輸出為:

(1.0, 0.0)
(1.0, -1.0)

您可能在這里錯誤地使用了 matplotlib。

如果正常化步驟處於活動狀態,它應該可以正常工作。 文檔告訴我們,如果 input-image 是 float 類型,那只有在默認情況下才處於活動狀態!

代碼

import numpy as np
import matplotlib.pyplot as plt
from scipy import misc

fig, ax = plt.subplots(2,2)

# This usage shows different colors because there is no normalization
# FIRST ROW
f = misc.face(gray=True)
print(f.dtype)
g = f*2  # just some operation to show the difference between usages
ax[0,0].imshow(f)
ax[0,1].imshow(g)

# This usage makes sure that the input-image is of type float
# -> automatic normalization is used!
# SECOND ROW
f = np.asarray(misc.face(gray=True), dtype=float)  # TYPE!
print(f.dtype)
g = f*2  # just some operation to show the difference between usages
ax[1,0].imshow(f)
ax[1,1].imshow(g)

plt.show()

輸出

uint8
float64

在此處輸入圖片說明

分析

第一行顯示了錯誤的用法,因為輸入是 int 類型,因此不會使用規范化。

第二行顯示正確用法!

編輯:

sascha 在評論中正確指出重新縮放不適用於 RGB 圖像,並且必須確保輸入在 [0,1] 范圍內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM