如何使用matplotlib读取32位整数图像?

时间:2016-12-09 00:34:10

标签: python image numpy matplotlib tiff

我需要使用Python 2从磁盘读取单通道32位整数TIFF图像来执行一些图像分析。我尝试从matplotlib image.imread但是我无法使代码工作,因为数据被读取为4通道8位整数图像:

* {
    margin: 0;
}
body {
    background-color: gray;
}
html, 
body {
  height: 100%;

}

.left {
    display: flex;
    flex-flow: row wrap;
    align-items: center;
    justify-content: space-around;
    order: 1;
    width: 30%;
}
.middle {
    display: flex;
    flex-flow: column wrap;
    order: 2;
    width: 50%;
    height: 99%;
}
.scienceBox {
	width: 100%;
	background-color: gray;
	-radius: 5px;
	margin-bottom: 2%;
}
.scienceBox p {
	text-align: center;
	font-size: 19px;
	background-color: green;
	padding: 0;
	color: white;
}
.scienceBox li {
	color: black;
	font-size: 17px;
	list-style-type: square;
}
.scienceBox special{
	list-style-type: circle;
}
.container {
    display: flex;
    position: relative;
    flex-wrap:  wrap;
    justify-content: space-between;
    align-items: stretch;
	padding-bottom: 2%;
    min-height: 70vh;
    width: 70%;
    margin: 5% auto 8% auto;
    background-color: white;
}
.container p { 
  margin-bottom: 12%; 
}
.container img {
  margin-bottom: 10%; 
}
.container img:first-child{ 
  margin-top: 5%;
}
.box1 {
	text-align: center;
	font-size: 20px;
}
.box h2{	
	color: orange;
	text-align: center;
}
.right {
 display: flex;
 position: relative;
 flex-flow: row wrap;
 align-content: flex-start;
 justify-content: center;
 order: 3;
 width: 20%;
 height:100%;

}
.right div{
	height: 25%;
}
.right .list{
    height: auto;
	text-align: center;
}
.list ul{
    list-style: none;
    padding: 0;
}
.list a{
    text-decoration: none;
    color: inherit;
}
.headbox h3{
    color: orange;
	text-align: center;
}

.sactive { /* s for sidebar */
	font-weight: bold;
}
a {
	color: orange;
	text-decoration: none;
}
.hundredw {
	position: relative;
	width: 100%;
	height: 10%;
	text-align: center;
}
.papajohns{
	position: absolute;
	top: 60%;
	right: 0;
	left: 0;
	margin: auto
}
.papajohns p {
	margin: 0 1%;
}
ul.square li {
	list-style-type: square;
	margin-bottom: 2%;
}
.mtop {
	margin-top: 3.5%;
}
@media all and (max-width: 900px) {
    #nav {
     flex-direction: column; /*updated*/
     margin-bottom: 7%;
    }
    #nav ul {
      padding-left: 0; /*added*/
    }
    #nav li {
      flex: 1 1 100%; /*updated*/
      padding: 5px;
      -top: 1px solid black;
      -bottom: 1px solid black;
    }   
    #nav li a{
    text-align: center;
    padding: 5px;
    margin: 5px;
    }
    #bigwrap {
      width: 100%;
    }
    .container {
      flex-flow: row wrap;
      min-height: 100vh;
      width: 100%;
    }
	.sarpinos{
		top:56%;
	}
	.left {
		align-content: flex-start;
		height: 50%;
		margin-bottom: 3%;
	}
	.middle {
		height: 40%;
	}
    .left, .right {
      flex: 1 100%;
    }
   .middle {
    flex-flow: row wrap;
    flex-grow: 1;
    flex-shrink: 1;
    justify-content: center;
	align-content: flex-start;
   }
   .box {
    width: 70%;
    text-align: center;
   }
   #header2{
	   justify-content: space-around;
   }
}

@media (min-width: 900px) and (max-width: 1100px) {
  
  #nav{
    justify-content: space-around;
  }
  .container {
    width: 100%;
  }
}

问题:是否可以使用matplotlib读取单通道32位整数图像?

我知道有其他方法可以在Python中读取这样的图像,例如使用PIL中的Image.open

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<div class="container">
		<div class="left">
        	<img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
            <img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
            <img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
            <img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
            <img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
            <img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />


			<div class="hundredw">
   		  		<img src="http://placehold.it/50x50" width="50" height="50" alt="Picture of void" />
           	  	<div class="papajohns">
                    <p>file</p>
					<p>files</p>
					<p>files</p>
					<p><i>files</i></p>
                </div>
            </div>
        </div>
        <div class="middle">
			<div class="box">
            	<h2> Sample <h2>
            </div>
            <div class="box">
            	<p>
              	This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                </p>
                <ul class="square">
            		<li>This is a sample sentence.</li>         
 					<li>This is a sample sentence.</li>
 					<li>This is a sample sentence.</li>
				<ul>          
            </div>
            <div class="box mtop">
            	<p>
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence.This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                This is a sample sentence. This is a sample sentence. This is a sample sentence. This is a sample sentence.
                </p>
            </div>
            <div class="scienceBox">
    			<p>This is a sample sentence.</p>
        		<ul>
        			<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
                    <li>This is a sample sentence.</li>
                    <li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
					<li>This is a sample sentence.</li>
                    <li>This is a sample sentence.</li>
                    <li>This is a sample sentence.</li>
        		</ul>
			</div>
            <div class="scienceBox">
    			<p>This is a sample sentence.</p>
        		<ul>
        			<li>This is a sample sentence.?
                   	  <ul class="special">
                        	<li>This is a sample sentence. This is a sample sentence. This is a sample sentence.
                            This is a sample sentence. This is a sample sentence. This is a sample sentence.
                            </li> 
                        </ul>
                    </li>
                    <li>This is a sample sentence.?
                    	<ul class="special">
                        	<li>This is a sample sentence.
                            </li> 
                        </ul>
                    </li>
					<li>Is This is a sample sentence.?
                    	<ul class="special">
                        	<li>This is a sample sentence. This is a sample sentence. This is a sample sentence.
                            This is a sample sentence. This is a sample sentence.
                            </li> 
                        </ul>
                    </li>
                    <liThis is a sample sentence.?
                    	<ul class="special">
                        	<li>This is a sample sentence. This is a sample sentence. This is a sample sentence.
                            This is a sample sentence. This is a sample sentence. This is a sample sentence.
                            </li> 
                        </ul>
                    </li>
                </ul>
                        				
			</div>
         </div>
         <div class="right">
         	<div class="headbox">
            	<h3>This is a sample sentence.</h3>
            </div>
            <div class="list">
                   <ul>
                       <li><a class="sactive" href="#">This is a sample sentence.</a></li>
                       <li><a href="#">This is a sample sentence.</a></li>
                   </ul>
            </div>
            <div class="headbox">
            	<h3>Important Informaiton</h3>
            </div>
            <div class="list2">
                   <ul>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                   </ul>
                   <ul>
                       <li>Address</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                   </ul>
                   <ul>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                       <li>This is a sample sentence.</li>
                   </ul>
                   
            </div>
         </div>
      </div>

另一种可能性是使用来自scikit-learn的io.imread

>>> import numpy as np
>>> import matplotlib.image as mpimg
>>> img = mpimg.imread('my_image.tif')
>>> img.shape
(52, 80, 4)
>>> img[0:2, 0:2]
array([[[255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255]]], dtype=uint8)

另一种方法是利用OpenCV中的imread函数。但在这种情况下,数据必须转换为32位整数:

>>> from PIL import Image
>>> img = np.asarray(Image.open('my_image.tif'))
>>> img.dtype
dtype('int32')
>>> img.shape
(52, 80)
>>> img[0:2, 0:2]
array([[8745, 8917],
       [8918, 9479]])

2 个答案:

答案 0 :(得分:2)

SciPy no longer supports imread (versions 1.3.0+)

相反,您可以将 imageio 用于科学图像:

import imageio

im = imageio.imread('imageio:astronaut.png') 

im.shape  # im is a numpy array

答案 1 :(得分:1)

根据this tutorial,使用matplotlib无法读取32位整数图像:

  

Matplotlib绘图可以处理float32和uint8,但是除PNG以外的任何格式的图像读/写都限于uint8数据。

作为参考,我发现了一个基于SciPy ndimage.imread的解决方法:

from scipy import ndimage
img = ndimage.imread('my_image.tif', mode='I')

基于tifffile的方法(@Warren Weckesser建议)也可以正常使用:

from tifffile import TiffFile

with TiffFile('my_image.tif') as tif:
    img = tif.asarray()