0
이 코드는 이미지를 취해 에너지 배열을 생성 한 다음 원하는 이미지를 통해 수직 이음새를 계산합니다. find_vertical_seam_dynamic
의 정의 상단에서 초기화 한 비용 및 솔기 목록을 인쇄하는 데 문제가 있습니다.추가 목록 인쇄 정의
img = skimage.io.imread('mandrill.jpg')
def energy(image):
dy = np.array([-1, 0, 1])[:,None,None]
dx = np.array([-1, 0, 1])[None,:,None]
energy_img = convolve(image, dx)**2 + convolve(image, dy)**2
return np.sum(energy_img, axis=2)
def find_vertical_seam_dynamic(energy_array):
#initiate empty lists for total cost and seam position
total_cost = []
seam = []
min_cost = np.amin(energy_array[0][:])
total_cost.append(min_cost)
position_array = np.where(energy_array[0][:] == min_cost)
col = ((position_array[0]).tolist())[0] #converting numpyarray to int...
seam.append(col)
for row in range(energy_array.shape[0]): #loops over rows
col = seam[-1] #Column position is the last added position to the vertical seam list
print ("Row:", row)
print ("Column:",col)
cost = total_cost[-1]
print ("Cost:",cost)
if row == len(range(energy_array.shape[0]))-1: #Exit before checking beyond last row.
return
else:
if col < 0 : #bounded on the left side
middle = energy_array[row+1,col]
right = energy_array[row+1,col+1]
#middle neighbour is lowest
if middle < right:
min_cost = energy_array[row+1,col]
total_cost.append(min_cost)
col = col
seam.append(col)
#right neighbour is lowest
else:
min_cost = energy_array[row+1,col+1]
total_cost.append(min_cost)
col = col+1
seam.append(col)
if col >= len(range(energy_array.shape[1])):
left = energy_array[row+1,col-1]
middle = energy_array[row+1,col]
#left neighbour is lowest
if left < middle:
min_cost = energy_array[row+1,col-1]
total_cost.append(min_cost)
col = col-1
seam.append(col)
#middle neighbour is lowest
else:
min_cost = energy_array[row+1,col]
total_cost.append(min_cost)
col = col
seam.append(col)
else:
#Get energy levels for the next row
left = int(energy_array[row+1,col-1])
middle = int(energy_array[row+1,col])
right = int(energy_array[row+1,col+1])
print ("\n")
print ("Left",left)
print ("middle",middle)
print ("right",right)
lowest_cost = min(left, middle, right)
#left neighbour is lowest
if left == lowest_cost:
min_cost = energy_array[row+1,col-1]
total_cost.append(min_cost)
col = col-1
seam.append(col)
#middle neighbour is lowest
if middle == lowest_cost:
min_cost = energy_array[row+1,col]
total_cost.append(min_cost)
col = col
seam.append(col)
#right neighbour is lowest
if right == lowest_cost:
min_cost = energy_array[row+1,col+1]
total_cost.append(min_cost)
col = col+1
seam.append(col)
return total_cost, seam
energy_array = energy(img)
find_vertical_seam_dynamic(energy_array)
print (total_cost[:])
print (seam[:])
코드 시작 부분에서 초기화 된 목록을 인쇄하려고하는 마지막 절에서 오류가 발생합니다. 이것은 오류 모양입니다.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-214-d447830fdced> in <module>()
122 find_vertical_seam_dynamic(energy_array)
123
--> 124 print (total_cost[:])
125 print (seam[:])
NameError: name 'total_cost' is not defined
여기서 내가 잘못되어 가고 있는지 잘 모르겠습니다. 다른 팁도 감사하겠습니다. 감사.