Matplotlib - 如何重新调整RGB图像的像素强度

时间:2016-09-24 14:41:59

标签: python image-processing matplotlib

我对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)

1 个答案:

答案 0 :(得分:1)

你可能在这里使用了matplotlib错误。

标准化步骤应该正常工作,如果它是活动的。 docs告诉我们,如果输入图片的类型为float,则默认只有活动状态

代码

$html="<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'>Trading Code:</td>
      <td width='85%'>RCL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: Royal Capital Ltd., DSE TREC No. 21, has withdrawn one of its Authorized Representatives, Mr. Md. Zikrul Haque, with immediate effect.</td>
   </tr>
</table>
<br>
<table border='0' cellspacing='3' width='100%'>
   <tr>
      <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td>
      <td width='85%'>ISL</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News Title:</b></font></td>
      <td>DSENEWS: Withdrawal of Authorized Representative</td>
   </tr>
   <tr>
      <td><font color='#3366FF'><b>News:</b></font></td>
      <td align='justify'>Withdrawal of Authorized Representative: IDLC Securities Ltd., DSE TREC No. 58, has withdrawn one of its Authorized Representatives, Mr. Mohammad Ziaur Rahman, with immediate effect.</td>
   </tr>
</table>";




/* Construct XPath expression to find required data*/
$query='//td[contains( . , "Trading Code" )]/following-sibling::td|//td[contains( . , "News Title" )]/following-sibling::td';

/* create the DOMDocument & DOMXPath objects */
$dom=new DOMDocument;
$dom->loadHTML( $html );
$xp=new DOMXPath( $dom );

/* Run the query to find nodes */
$col=$xp->query( $query );

/* Process the nodes */
if( !empty( $col ) ){
    foreach( $col as $td ){
        /* do something with data found */
        echo $td->nodeValue;
    }
}

输出

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()

enter image description here

分析

第一行显示错误的用法,因为输入的类型为int,因此不会使用规范化。

第二行显示正确的用法!

编辑:

sascha在评论中正确指出,重新缩放不适用于RGB图像,输入必须保证在[0,1]范围内。