問題:
現在需要給一個視頻序列插幀,插幀演算法要求每次只能由兩幀輸入插值得到其中間幀,如果現在需要給一個視頻做 4 倍(或者更高的 8,16 倍等類似)的插幀,則一個插幀的思路是當前視頻每相鄰幀之間插入 3 幀,即:假設插幀前視頻幀序號是 0,4,8,12…,則插幀時補充相鄰幀跨過的 3 個序號,得到插幀后的視頻幀序號為 0,1,2,3,4,5,6,.. 即可,具體順序為 由 0,4 插幀得 2,再由 0,2 插幀得到 1、由 2,4 插幀得到 3,
現在要解決的問題,簡單來說就是,需要撰寫一個確定插幀順序的代碼,要求 (1)新的幀只能由原有的或者已生成幀插值得到,(2)插幀只能得到給定兩幀的中間幀,
方案:
這個問題其實本質上用到的演算法是遞回,因為滿足“原來的問題,可以轉化為更小的同一問題”,即“由 0,4 插幀得 2” 和 后續的 “由 0,2 插幀得到 1、由 2,4 插幀得到 3” 本質都是從外往內找中點,
演算法代碼如下,輸入是原視頻相鄰幀的序號,輸出是插幀順序,如 end_index=4, start_index=0 時,輸出為 [[0, 4, 2], [0, 2, 1], [2, 4, 3]] 內層list代表 [已有幀1,已有幀2,新生成幀],
def interpIndexOrder(end_index, start_index=0):
"""
determine the interpolate index generating order for a given start_index and end_index
the rule is: the new interpolate index can only be generated as the middle point of the existing indexes
for example: start_index=0 and end_index=4, the interpolate index generating order is: [0, 4->2], [0, 2->1], [2, 4->3]
return: [[existing_index1, existing_index2, generated_new_index], ...
"""
x = []
def recur_interp(start_index, end_index):
if end_index-start_index == 1:
return # stop criterion
assert (start_index +
end_index) % 2 == 0, 'start_index + end_index should be even'
mid_index = (start_index + end_index) // 2
x.append([start_index, end_index, mid_index])
# sub-problem
recur_interp(start_index, mid_index)
recur_interp(mid_index, end_index)
recur_interp(start_index, end_index)
return x
另外需要注意的是,為了在遞回的時候把插幀次序記錄下來,下面的函式使用了兩層,第一層主函式里面的變數 x 對于子函式 recur_interp() 是處于嵌套作用域,可以被訪問,
參考:
- 這個問題的來源和最終應用完整代碼 link
本文來自博客園,作者:凌晗,轉載請注明原文鏈接:https://www.cnblogs.com/dawnlh/p/17330024.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/550447.html
標籤:其他
