我希望有一位Python專家能夠提供一些幫助,解決目前我正在使用內部函數,閉包和工廠函數時遇到的困惑。在尋找一個通用霍夫實現的例子變換,我發現這一點:Python - 內部函數,閉包和工廠函數 - 如何分解?
https://github.com/vmonaco/general-hough/blob/master/src/GeneralHough.py
我想轉換成C++這一點,似乎第一步是分解出在general_hough_closure的內部函數() :
def general_hough_closure(reference_image):
'''
Generator function to create a closure with the reference image and origin
at the center of the reference image
Returns a function f, which takes a query image and returns the accumulator
'''
referencePoint = (reference_image.shape[0]/2, reference_image.shape[1]/2)
r_table = build_r_table(reference_image, referencePoint)
def f(query_image):
return accumulate_gradients(r_table, query_image)
return f
我似乎被困在這個函數如何工作。 「f」似乎沒有被調用到任何地方,我不確定函數如何知道「query_image」是什麼?我嘗試了各種谷歌搜索,以找到有關內部函數,閉包和工廠功能的提示,例如this和一些類似的頁面,但我能找到的所有示例都更簡單,因此幫助不大。任何人都可以提供一些方向嗎?
此代碼__ *收益* A function__:
然後,您可以按以下方式使用它。你確定這就是你想要的嗎?我懷疑你想在C++中做什麼。 –
@Rawing,我知道函數返回一個函數,這是我想分解的部分,所以我可以翻譯成C++ – cdahms
刪除行'def f(query_image):',remove'return f ',給函數一個名爲'query_image'的第二個參數。這應該很容易翻譯。 –