TypeError:使用numpy和pydicom在0维数组上进行迭代

时间:2019-02-23 19:37:57

标签: python numpy matplotlib tkinter pydicom

我试图创建一个简单的DICOM查看器,在其中使用matplotlib绘制图像,并且希望在tkinter中显示相同的图(这是DICOM图像),但是在运行代码时出现此错误。请帮忙。当我尝试绘制a时会发生错误,但我认为它与声明x,y和p的值有关。

import pydicom
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import 
FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
from pydicom.data import get_testdata_files

filename = get_testdata_files('000000.dcm')
dataset = pydicom.dcmread('000000.dcm')
data = dataset.pixel_array

class mclass:
  def __init__(self, window):
    self.window = window
    self.button=Button(window,text="check",command=self.plot)
    self.button.pack()

  def plot (self):
      if 'PixelData' in dataset:
          rows = int(dataset.Rows)
          cols = int(dataset.Columns)
      y=np.array(rows)
      x=np.array(cols)
      p=np.array(data)
      fig = Figure(figsize=(6,6))
      a = fig.add_subplot(111)
      a.plot(p, range(2+max(y)))

      canvas = FigureCanvasTkAgg(fig, master=self.window)
      canvas.get_tk_widget().pack()
      canvas.draw()

window = Tk()
start = mclass (window)
window.mainloop()

1 个答案:

答案 0 :(得分:0)

从它的外观看,您的错误就在这里:

    apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.example.mohammad.kahgle"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}    
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.github.armcha:ElasticView:0.1.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.nightonke:boommenu:2.1.1'
implementation 'com.ramotion.foldingcell:folding-cell:1.2.2'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation "com.android.support:support-core-utils:28.0.0"
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

您要求输入y=np.array(rows) ... a.plot(p, range(2+max(y))) ,但是用于实例化max(y)ds.Rows的{​​{1}}和ds.Columns是标量值(并且要确定的是您使用x)。这意味着yint(ds.Rows)都将是一个0维数组,这将解释抛出的错误,大概是在x上。试试:

y