繁体   English   中英

使用python解压缩的值太多

[英]Too many values to unpack with python

我对Python有一点问题。 我正在尝试为DCM标准编写一个应用程序,并对其进行切片并绘制最终模型。

这是我的代码:

from lar import *
from scipy import *
import scipy
import numpy as np
from time import time
from pngstack2array3d import pngstack2array3d

colors = 2
theColors = []
DEBUG = False
MAX_CHAINS = colors

# It is VERY important that the below parameter values 
# correspond exactly to each other !!
# ------------------------------------------------------------
MAX_CHUNKS = 75
imageHeight, imageWidth = 250,250  # Dx, Dy


# configuration parameters
# ------------------------------------------------------------

beginImageStack = 430
endImage = beginImageStack
nx = ny =  50
imageDx = imageDy =  50
count = 0

# ------------------------------------------------------------
# Utility toolbox
# ------------------------------------------------------------

def ind(x,y): return x + (nx+1) * (y + (ny+1) )

    def invertIndex(nx,ny):
        nx,ny = nx+1,ny+1
        def invertIndex0(offset):
            a0, b0 = offset / nx, offset % nx
            a1, b1 = a0 / ny, a0 % ny
            return b0,b1
        return invertIndex0

    def invertPiece(nx,ny):
        def invertIndex0(offset):
            a0, b0 = offset / nx, offset % nx
            a1, b1 = a0 / ny, a0 % ny
            return b0,b1
        return invertIndex0


    # ------------------------------------------------------------
    # computation of d-chain generators (d-cells)
    # ------------------------------------------------------------

    # cubic cell complex
    # ------------------------------------------------------------
    def the3Dcell(coords):
        x,y= coords
        return [ind(x,y),ind(x+1,y),ind(x,y+1),ind(x+1,y+1)]

    # construction of vertex coordinates (nx * ny )
    # ------------------------------------------------------------
    V = [[x,y] for y in range(ny+1) for x in range(nx+1) ]


    if __name__=="__main__" and DEBUG == True:
        print "\nV =", V

    # construction of CV relation (nx * ny)
    # ------------------------------------------------------------
    CV = [the3Dcell([x,y]) for y in range(ny) for x in range(nx)]

    if __name__=="__main__" and DEBUG == True:
        print "\nCV =", CV
        #hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,CV[:500]+CV[-500:])))
        #box = SKELETON(1)(BOX([1,2,3])(hpc))
        #VIEW(STRUCT([box,hpc]))

    # construction of FV relation (nx * ny )
    # ------------------------------------------------------------
    FV = []
    v2coords = invertIndex(nx,ny)
    for h in range(len(V)):
        x,y= v2coords(h)
        if (x < nx) and (y < ny): FV.append([h,ind(x+1,y),ind(x,y+1),ind(x+1,y+1)])


    if __name__=="__main__" and DEBUG == True:
        print "\nFV =",FV
        #hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,FV[:500]+FV[-500:])))
        #box = SKELETON(1)(BOX([1,2,3])(hpc))
        #VIEW(STRUCT([box,hpc]))

    # construction of EV relation (nx * ny )
    # ------------------------------------------------------------
    EV = []
    v2coords = invertIndex(nx,ny)
    for h in range(len(V)):
        x,y = v2coords(h)
        if x < nx: EV.append([h,ind(x+1,y)])
        if y < ny: EV.append([h,ind(x,y+1)])

    if __name__=="__main__" and DEBUG == True:
        print "\nEV =",EV
        #hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,EV[:500]+EV[-500:])))
        #box = SKELETON(1)(BOX([1,2,3])(hpc))
        #VIEW(STRUCT([box,hpc]))


    # ------------------------------------------------------------
    # computation of boundary operators (∂3 and ∂2s)
    # ------------------------------------------------------------

    """
    # computation of the 2D boundary complex of the image space 
    # ------------------------------------------------------------
    Fx0V, Ex0V = [],[]  # x == 0
    Fx1V, Ex1V = [],[]  # x == nx-1
    Fy0V, Ey0V = [],[]  # y == 0
    Fy1V, Ey1V = [],[]  # y == ny-1
    v2coords = invertIndex(nx,ny)
    for h in range(len(V)):
        x,y = v2coords(h)

        if (y == 0):
            if x < nx: Ey0V.append([h,ind(x+1,y)])
            if (x < nx):
                Fy0V.append([h,ind(x+1,y),ind(x,y)])
        elif (y == ny):
        if x < nx: Ey1V.append([h,ind(x+1,y)])
        if (x < nx):
            Fy1V.append([h,ind(x+1,y),ind(x,y)])

    if (x == 0):
        if y < ny: Ex0V.append([h,ind(x,y+1)])
        if (y < ny):
            Fx0V.append([h,ind(x,y+1),ind(x,y)])
    elif (x == nx):
        if y < ny: Ex1V.append([h,ind(x,y+1)])
        if (y < ny):
            Fx1V.append([h,ind(x,y+1),ind(x,y)])

 FbV = Fy0V+Fy1V+Fx0V+Fx1V
 EbV = Ey0V+Ey1V+Ex0V+Ex1V
"""

"""
if __name__=="__main__" and DEBUG == True:
    hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,FbV)))
    VIEW(hpc)
    hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,EbV)))
    VIEW(hpc)
"""

# computation of the ∂2 operator on the boundary space
# ------------------------------------------------------------
print "start partial_2_b computation"
#partial_2_b = larBoundary(EbV,FbV)
print "end partial_2_b computation"



# computation of ∂3 operator on the image space
# ------------------------------------------------------------
print "start partial_3 computation"
partial_3 = larBoundary(FV,CV)
print "end partial_3 computation"



# ------------------------------------------------------------
# input from volume image (test: 250 x 250 x 250)
# ------------------------------------------------------------
out = []
Nx,Ny = imageHeight/imageDx, imageWidth/imageDx
segFaces = set(["Fy0V","Fy1V","Fx0V","Fx1V"])

for inputIteration in range(imageWidth/imageDx):
    startImage = endImage
    endImage = startImage + imageDy
    xEnd, yEnd = 0,0
    theImage,colors,theColors = pngstack2array3d('SLICES2/', startImage, endImage, colors)
    print "\ntheColors =",theColors
    theColors = theColors.reshape(1,2)
    background = max(theColors[0])
    foreground = min(theColors[0])
    print "\n(background,foreground) =",(background,foreground)

    if __name__=="__main__" and DEBUG == True:
        print "\nstartImage, endImage =", (startImage, endImage)

    for i in range(imageHeight/imageDx):

        for j in range(imageWidth/imageDy):

            xStart, yStart = i * imageDx, j * imageDy
            xEnd, yEnd = xStart+imageDx, yStart+imageDy

            image = theImage[:, xStart:xEnd, yStart:yEnd]
            nx,ny = image.shape

            if __name__=="__main__" and DEBUG == True:
                print "\n\tsubimage count =",count
                print "\txStart, yStart =", (xStart, yStart)
                print "\txEnd, yEnd =", (xEnd, yEnd)
                print "\timage.shape",image.shape



            # ------------------------------------------------------------
            # image elaboration  (chunck: 50 x 50)
            # ------------------------------------------------------------

            """
            # Computation of (local) boundary to be removed by pieces
        # ------------------------------------------------------------


            if pieceCoords[0] == 0:  boundaryPlanes += ["Fx0V"]
            elif pieceCoords[0] == Nx-1:  boundaryPlanes += ["Fx1V"]
            if pieceCoords[1] == 0:  boundaryPlanes += ["Fy0V"]
            elif pieceCoords[1] == Ny-1:  boundaryPlanes += ["Fy1V"]

            """
            #if __name__=="__main__" and DEBUG == True:
                #planesToRemove = list(segFaces.difference(boundaryPlanes))
                #FVtoRemove = CAT(map(eval,planesToRemove))


            count += 1


            # compute a quotient complex of chains with constant field
            # ------------------------------------------------------------
            chains2D = [[] for k in range(colors)]
            def addr(x,y): return x + (nx) * (y + (ny))
            for x in range(nx):
                for y in range(ny):
                    if (image[x,y] == background):
                        chains2D[1].append(addr(x,y))
                    else:
                        chains2D[0].append(addr(x,y))

            #if __name__=="__main__" and DEBUG == True:
                #print "\nchains3D =\n", chains3D


            # compute the boundary complex of the quotient cell
            # ------------------------------------------------------------
            objectBoundaryChain = larBoundaryChain(partial_3,chains2D[1])
            b2cells = csrChainToCellList(objectBoundaryChain)
            sup_cell_boundary = MKPOLS((V,[FV[f] for f in b2cells]))

            # remove the (local) boundary (shared with the piece boundary) from     the quotient cell
            # ------------------------------------------------------------

            """
            cellIntersection = matrixProduct(csrCreate([FV[f] for f in     b2cells]),csrCreate(FVtoRemove).T)
            #print "\ncellIntersection =", cellIntersection
            cooCellInt = cellIntersection.tocoo()
            b2cells = [cooCellInt.row[k] for k,val in enumerate(cooCellInt.data)     if val >= 4]
            """


            # ------------------------------------------------------------
            # visualize the generated model  
            # ------------------------------------------------------------

            print "xStart, yStart =", xStart, yStart

            if __name__=="__main__":
                sup_cell_boundary = MKPOLS((V,[FV[f]  for f in b2cells]))
                if sup_cell_boundary != []:
                    out += [T([1,2])([xStart,yStart])    (STRUCT(sup_cell_boundary))]   
                if count == MAX_CHUNKS:
                    VIEW(STRUCT(out))


            # ------------------------------------------------------------
            # interrupt the cycle of image elaboration  
            # ------------------------------------------------------------
            if count == MAX_CHUNKS: break
        if count == MAX_CHUNKS: break
    if count == MAX_CHUNKS: break

这是终端的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-2e498c6090a0> in <module>()
    213 
    214                         image = theImage[:, xStart:xEnd, yStart:yEnd]
--> 215                         nx,ny = image.shape
    216 
    217                         if __name__=="__main__" and DEBUG == True:

ValueError: too many values to unpack

有人可以帮我解决这个问题????

基于这条线:

image = theImage[:, xStart:xEnd, yStart:yEnd]

image是一个3d数组,而不是一个2D数组(它看起来是图像的多个切片),第二个和第三个维分别代表xy 因此,如果您想获得它的尺寸,您需要将其解压缩为三维,例如:

nslice, nx, ny = image.shape

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM