ValueError:无法将输入数组从形状(256256,3)广播到形状(256256)

2024-09-29 23:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试读取一些DICOM文件并将它们转换为numpy数组。但当我去存储图像的原始数据时,会出现以下错误:

Traceback (most recent call last): File "C:/Users/Matheus/PycharmProjects/mestrado/AreaDeInteresse/testando3.py", line 47, in d_array[:, :, dcms.index(dcm)] = np.array(b.pixel_array) ValueError: could not broadcast input array from shape (256,256,3) into shape (256,256)

我的代码是对代码的重读: https://gist.github.com/somada141/8dd67a02e330a657cf9e

from tkinter import *
from PIL import ImageTk, Image
from os import listdir
from os.path import isfile, join
import cv2
import numpy as np
###################
import os
import pydicom as dicom
from matplotlib import pyplot as plt
#%matplotlib inline
import numpy as np
###################

drawing = False
point1 = ()
point2 = ()

image_axial=[]
image_sagital=[]
image_coronal=[]

root=Tk()

root.title('viewer das image')

root_dir = 'D:\\Tomografias UNIOESTE Odontologia\\Neusa Maria Scionte Fermino\\IMGDATA'
dcms = []
for d, s, fl in os.walk(root_dir):
    for fn in fl:
        if ".dcm" in fn.lower():
            dcms.append(os.path.join(d, fn))

print(dcms[0])
d = dicom.read_file(dcms[0])
print(d)
print(d.pixel_array.shape)
ref_dicom = dicom.read_file(dcms[0])
d_array = np.zeros((ref_dicom.Rows, ref_dicom.Columns, len(dcms)), dtype=ref_dicom.pixel_array.dtype)

######

######

for dcm in dcms:
    b = dicom.read_file(dcm)
    d_array[:, :, dcms.index(dcm)] = np.array(b.pixel_array)

for numero, image in enumerate(d_array):
    print(numero)
    image_axial.append(ImageTk.PhotoImage(Image.fromarray(d_array[numero, :, :])) )
    image_sagital.append(ImageTk.PhotoImage(Image.fromarray(d_array[:, numero, :])))
    if numero<len(dcms):
        image_coronal.append(ImageTk.PhotoImage(Image.fromarray(d_array[:, :, numero])))

有人能帮我解决这个错误吗


Tags: infromimageimportosasnparray

热门问题