我希望有一位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'的第二个参数。这应该很容易翻译。 –