Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用形状(200200)绘制一个Numpy数组,它显示一条垂直线而不是正方形_Python_Numpy_Matplotlib_Plot_Itk - Fatal编程技术网

Python 用形状(200200)绘制一个Numpy数组,它显示一条垂直线而不是正方形

Python 用形状(200200)绘制一个Numpy数组,它显示一条垂直线而不是正方形,python,numpy,matplotlib,plot,itk,Python,Numpy,Matplotlib,Plot,Itk,我刚刚开始学习Python 3.7.7 我试图用plot显示一个NIFTI图像,作为带有float32元素的numpy数组 这是显示图像的代码: import os import SimpleITK as sitk import numpy as np import matplotlib.pyplot as plt import cv2 def plot_grey_images_as_figure(img1, img2, title1, title2): """ Show im

我刚刚开始学习Python 3.7.7

我试图用plot显示一个NIFTI图像,作为带有float32元素的numpy数组

这是显示图像的代码:

import os
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt
import cv2

def plot_grey_images_as_figure(img1, img2, title1, title2):
    """
    Show img1 and img2 as a single figure using matplotlib.pylot.

    Show img1 and img2 as a single figure using matplotlib.pylot with the titles
    title1 for img1 and title2 for img2.

    Parameters:
    img1 (array-like or PIL image): Image to show first.
    img2 (array-like or PIL image): Image to show second.
    title1 (string) : Title for image 1.
    title2 (string) : Title for image 2.

    Returns:
    Nothing.

    """    

    plt.subplot(121), plt.imshow(img1, cmap = 'gray')
    plt.title(title1), plt.xticks([]), plt.yticks([])

    plt.subplot(122), plt.imshow(img2, cmap = 'gray')
    plt.title(title2), plt.xticks([]), plt.yticks([])

    plt.show()

    return
这是我在显示图像之前预处理图像的方式:

def preprocessing_array(array, rows_standard, cols_standard, debug): # array shape is (48, 240, 240)

    image_rows_Dataset = np.shape(array)[1]   
    image_cols_Dataset = np.shape(array)[2]

    num_rows_1 = ((image_rows_Dataset // 2) - (rows_standard // 2)) #  20
    num_rows_2 = ((image_rows_Dataset // 2) + (rows_standard // 2)) # 220
    num_cols_1 = ((image_cols_Dataset // 2) - (cols_standard // 2)) #  20
    num_cols_2 = ((image_cols_Dataset // 2) + (cols_standard // 2)) # 220

    array = array[:, num_rows_1:num_rows_2, num_cols_1:num_cols_2]


    ### ------ New Axis --------------------------------------------------------
    # Add a new axis to denote that this is a one channel image.
    array  = array[..., np.newaxis] 

    return array  # array shape is (48, 200, 200, 1)
要显示图像,请执行以下操作:

# D is a dataset with shape (960, 2, 200, 200, 1) with float32 elements.
print("Shape: ", D[:,0,:][0].shape)
nifti.plot_grey_images_as_figure(D[:,0,:][0][:,-1], D[:,1,:][0][:,-1], "mask", "output")
我得到这个输出:

为什么我得到的是直线而不是正方形


可能问题是我添加了一个新轴,然后删除了它,因为它不允许我用形状200、200、1打印图像。

图像切片不正确。您的绘图函数应该是

base = D[:,0,:]
img1 = base[0][:, :, 0] # Shape of (200, 200)
img2 = base[1][:, :, 0]
nifti.plot_grey_images_as_figure(img1, img2, "mask", "output")

图像切片不正确。您的绘图函数应该是

base = D[:,0,:]
img1 = base[0][:, :, 0] # Shape of (200, 200)
img2 = base[1][:, :, 0]
nifti.plot_grey_images_as_figure(img1, img2, "mask", "output")
使用numpy.sqeeze将尺寸标注为一维。问题在于将图像作为非2D数组传递。挤压可以帮助我们沿轴=2降低尺寸,使形状从2002001变为200200

将numpy作为np导入 np.queezeD[:,0,:][0],轴=2 200, 200 建议改进 选择索引i轴=0 i=0值0,1,…,960 提取遮罩并输出图像 img1=D[i,0,:,:,0]形状200200->mask img2=D[i,1,:,:,0]形状200,200->输出 nifti.plot\u灰色\u图像\u如图1、img2、遮罩、输出 参考 使用numpy.sqeeze将尺寸标注为一维。问题在于将图像作为非2D数组传递。挤压可以帮助我们沿轴=2降低尺寸,使形状从2002001变为200200

将numpy作为np导入 np.queezeD[:,0,:][0],轴=2 200, 200 建议改进 选择索引i轴=0 i=0值0,1,…,960 提取遮罩并输出图像 img1=D[i,0,:,:,0]形状200200->mask img2=D[i,1,:,:,0]形状200,200->输出 nifti.plot\u灰色\u图像\u如图1、img2、遮罩、输出 参考
你发送给绘图功能的图像的形状是什么?@Vishnudev多大的错误!!!D[:,0,:][0][:,-1]。形状为200,1。我想我需要更多地了解Numpy数组。它应该是D[:,0,:][0][:,:,0],它的形状为200200。现在,在更新后它才有意义:D.shape=960,2200,200,1我看到了。非常感谢。如果您将数据重塑为20、48、2200、200、1会怎么样?这将允许您沿患者的第一个dim轴输入数据。如果你想采用这种方法,请使用D.Reformae20、48、2200、200、1。你发送给绘图功能的图像的形状是什么?@Vishnudev多大的错误!!!D[:,0,:][0][:,-1]。形状为200,1。我想我需要更多地了解Numpy数组。它应该是D[:,0,:][0][:,:,0],它的形状为200200。现在,在更新后它才有意义:D.shape=960,2200,200,1我看到了。非常感谢。如果您将数据重塑为20、48、2200、200、1会怎么样?这将允许您沿患者的第一个dim轴输入数据。如果你想采用这个方法,请使用D.reformae20,48,2200,200,1。@VansFannel我认为你的代码可以在一些地方被缩短和简化。在解决方案中提出了一些建议。请看一看,如果您有任何问题,请告诉我。@VansFannel我认为您的代码可以在一些地方被缩短和简化。在解决方案中提出了一些建议。请看一下,如果你有任何问题,请告诉我。