python - 为什么这个看似递归的代码在 skimage 的源代码中可以工作?

标签 python scikit-image

skimage的模块_denoise.py中我发现了以下代码:

def estimate_sigma(image, average_sigmas=False, multichannel=False):
        # some more code here
        sigmas = [estimate_sigma(image[..., c], multichannel=False)...
    return _sigma_est_dwt(detail_coeffs, distribution='Gaussian')

estimate_sigma里面有一个estimate_sigma?这是如何以及为何有效的?进口是

import scipy.stats
import numpy as np
from math import ceil
from .. import img_as_float
from ..restoration._denoise_cy import _denoise_bilateral, _denoise_tv_bregman
from .._shared.utils import skimage_deprecation, warn
import pywt
import skimage.color as color
import numbers

它似乎没有潜入任何新功能。

最佳答案

注意estimate_sigma的递归调用位于 if 子句内:

if multichannel:
    sigmas = [estimate_sigma(image[..., c], multichannel=False)...
...
return _sigma_est_dwt(detail_coeffs, distribution='Gaussian')

情况 A) 如果我们使用 multichannel=False 调用 estimate_sigma,该函数将不会进入 if 子句,因此不会调用自身并返回到达 body 的末端。

情况 B) 如果我们使用 multichannel=True 调用 estimate_sigma ,则条件将成功,因此 estimate_sigma 将调用自身。从上面的源代码中可以看出,当 estimate_sigma 调用自身时,它会将 multichannel 作为 False 传递。这意味着在递归调用期间将发生“情况A”。这次程序不会进入上面的if block ,递归结束,结束函数的执行并返回。

<小时/> 基本上的想法是:如果我们有多个 channel ,让我们将它们分成单独的 channel ,并对每个 channel 执行 sigma 估计

关于python - 为什么这个看似递归的代码在 skimage 的源代码中可以工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081248/

相关文章:

python-3.x - 如何在skimage中获得轮廓区域

python - 接收器何时在信号中被垃圾收集?

python - 如何打印前面有一定数量空格的整数?

python - 为什么 python 使用 .而不是/用于导入语句中的路径?

python - 谁能解释为什么 `skimage.measure.perimeter(img)` 返回这个结果?

opencv - 分水岭分割总是返回黑色图像

python-3.x - 获取每个超像素的RGB像素值列表

python - 如何在 python 脚本中导入绘图?

python - RaspiStill - 质量/尺寸未匹配 - 文件太大

image-processing - 查找图像中特定文本的坐标