【发布时间】:2017-04-23 11:11:21
【问题描述】:
我希望 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 和一些类似的页面,但我能找到的所有示例都更加简化,因此没有太大帮助。有人可以提供一些方向吗?
【问题讨论】:
-
这段代码返回一个函数。你确定那是你想要的吗?我怀疑这是你想在 C++ 中做的事情。
-
@Rawing,我知道该函数返回一个函数,这是我想排除的部分,所以我可以翻译成 C++
-
删除
def f(query_image):这一行,删除return f,并给函数一个名为query_image的第二个参数。这应该很容易翻译。
标签: python