2013-06-12 81 views
0

我有一個Python的小問題。 我試圖爲DCM標準編寫一個應用程序,他們可以切片並繪製最終模型。太多的值來解開python

這是我的代碼:

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 

有人可以幫我解決這個問題????基於該線

+0

什麼是在該代碼的'image.shape'? (另外,將數百行代碼發佈到StackOverflow是個不錯的主意,因爲大多數回答者不會想要通讀它們。通常最好提出一個簡短的,最好可重現的示例來展示您的問題) –

+0

@DavidRobinson我想使圖像作爲二維數組,像image = theImage [xStart:xEnd,yStart:yEnd]並且執行nx,ny = image.shape,但它返回相同的錯誤,需要解壓縮的值太多以至於無法打包 – Fabio

回答

2

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

image是一個三維陣列,而不是一個二維數組(它似乎是一個圖像的多個切片),與第二和第三維度分別代表xy。因此,如果你想獲得它的尺寸,你需要將它解包成三維,如:

nslice, nx, ny = image.shape 
+0

在二維中,這是一個二維線性algebric rapresentator for python,它顯示了切片的二維模型 – Fabio