【问题标题】:Print addended list from Definition打印定义中的附加列表
【发布时间】:2018-04-22 02:24:30
【问题描述】:

此代码获取图像,生成能量数组,然后计算所需图像的垂直接缝。我在打印我在 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

我不确定我在哪里出错了。任何其他提示将不胜感激。谢谢。

【问题讨论】:

    标签: python loops numpy seam


    【解决方案1】:

    那是因为您在函数 find_vertical_seam_dynamic 中定义了 total_cost,而在函数之外无法访问。

    当您从函数返回所需的 (total_cost & seam) 值时,您应该执行以下操作:

    total_cost, seam = find_vertical_seam_dynamic(energy_array)
    
    print (total_cost[:]) # also, you don't need [:]
    print (seam[:]) # same here
    

    阅读this 了解有关变量范围的更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多