0

我对 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

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

4

1 回答 1

2

基于行:

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

image是一个 3d 数组,而不是 2d 数组(它似乎是图像的多个切片),第 2 维和第 3 维分别表示xy。因此,如果你想获得它的维度,你需要将它解压缩成三个维度,例如:

nslice, nx, ny = image.shape
于 2013-06-12T17:57:20.027 回答