我在python和spyder中使用oct2py作为IDE编写了代码。
我的代码在几次迭代中运行良好,但如果我增加迭代后出现消息:
WindowsError:[错误145]目录不为空:某些临时文件的路径
编辑:这是我的代码
import cv2
import numpy as np
from scipy import signal,ndimage
from oct2py import octave
img=cv2.imread("circle.jpg")
clone=img.copy()
clone=np.uint8(img)
img=ndimage.filters.gaussian_filter(img,1.0)
refpt=[]
refpt=[(133, 20),
(170, 34),
(200, 50),
(220, 90),
(236, 130),
(210, 190),
(180, 220),
(110, 230),
(53, 210),
(20, 170),
(10, 120),
(20, 93),
(30, 60),
(50, 40),
(80, 20),
(130, 20)]
n=len(refpt)
octave.push('n',n)
t=octave.eval("1:n;")
ts=octave.eval("1:0.1:n;")
xys=octave.spline(t,refpt,ts)
xs=xys[0,:,None]
ys=xys[1,:,None]
for i in range(len(xs)-1):
cv2.line(clone,(xs[i],ys[i]),(xs[i+1],ys[i+1]),(255,0,0),2)
cv2.imshow("img", clone)
cv2.waitKey(0)
print '...Calculating Gradient map \n'
# Parameters
NoOfIter=20 #iterations
smth=img[:,:,0] #Smooth image
alpha=0.8
beta=0.20
gamma=1.0
kappa=0.1
wl=0.0
we=0.40
wt=0.0
[row,col]=smth.shape#Size of image
#Computing external forces
eline=smth
gradx,grady=np.gradient(smth)
eedge=-1*np.sqrt(np.multiply(gradx,gradx)+np.multiply(grady,grady))
# masks for taking various derivatives
m1=[[-1,1]]
m2=[[-1],[1]]
m3=[[1,2,1]]
m4=[[1,-2,1]]
m5=[[1,-1],[-1,1]]
cx=signal.convolve2d(smth,m1,mode='same')
cy=signal.convolve2d(smth,m2,mode='same')
cxx=signal.convolve2d(smth,m3,mode='same')
cyy=signal.convolve2d(smth,m4,mode='same')
cxy=signal.convolve2d(smth,m5,mode='same')
eeterm=[]
for i in range(0,row):
for j in range(0,col):
#eterm as defined in Kass snakes paper
eeterm.append(((cyy[i,j]*cx[i,j]*cx[i,j]-2*cxy[i,j]*cx[i,j]*cy[i,j]+cxx[i,j]*cy[i,j]*cy[i,j]))/((1+cx[i,j]*cx[i,j]+cy[i,j]*cy[i,j])**1.5))
eeterm=np.resize(eeterm,(row,col))
eext=(wl*eline+we*eedge-wt*eeterm)
fx,fy=np.gradient(eext)
#Initialize the snake
[m,n]=xs.shape
(mm,nn)=fx.shape
#populating penta diagonal matrix
b=[]
b.append(beta)
b.append(- (alpha + 4 * beta))
b.append(2 * alpha + 6 * beta)
b.append(b [1])
b.append(b [0])
octave.push('m',m)
octave.push('b',b)
octave.eval("A = b (1) * circshift (eye (m), 2);")
octave.eval("A = A + b (2) * circshift (eye (m), 1);")
octave.eval("A = A + b (3) * circshift (eye (m), 0);")
octave.eval("A = A + b (4) * circshift (eye (m), - 1);")
octave.eval("A = A + b (5) * circshift (eye (m), - 2);")
octave.push('gamma',gamma)
octave.eval("[L U]=lu(A+gamma.*eye(m,m));")
Ainv=octave.eval("inv(U)*inv(L);")
xflat=xs.flatten()
yflat=ys.flatten()
Ainv=np.asmatrix(Ainv)
xs=np.asmatrix(xs)
ys=np.asmatrix(ys)
# Moving snake in each iteration
for j in range(0,NoOfIter):
f1=octave.interp2(fx,xs,ys)
f2=octave.interp2(fy,xs,ys)
ssx= gamma*xs-kappa*f1
ssy= gamma*ys-kappa*f2
xs=Ainv*ssx
ys=Ainv*ssy
#Displaying snake in its new position
xs=np.asarray(xs)
ys=np.asarray(ys)
pt=zip(xs,ys)
pt=filter(lambda a: a != (0,0), pt)
while (1):
cv2.imshow("imanew",clone)
key = cv2.waitKey(1) & 0xFF
for i in range(len(pt)-1):
cv2.line(clone,pt[i],pt[i+1],(0,0,255),2)
cv2.line(clone,pt[len(pt)-1],pt[0],(0,0,255),2)
if key==27:
break
cv2.destroyAllWindows()