【问题标题】:matlab function strel("line") to pythonmatlab 函数 strel("line") 到 python
【发布时间】:2019-01-03 01:33:39
【问题描述】:

我想在python中使用matlab函数strel("line")

我找到了类似 scikit-learn / opencv / mahotas

之类的 python 库

但我找不到它。

最后,我在 pymorph 'seline' 中找到了类似的函数,但它与 matlab 的 strel 函数不同。


具体来说,我想使用(或实现)strel("line") 并旋转它。

像 strel("line", length, degree)


Matlab 示例
a = strel("line",5,0)
b = strel("line",5,30)
c = strel("line",5,45)
输出是

像这样。

谁知道matlab的strel("line",length,degree)函数或者python库等于strel("line",length,degree)的算法,请告诉我。谢谢。

【问题讨论】:

    标签: python matlab image-processing image-morphology


    【解决方案1】:

    对于此类问题,您可以随时查看 Octave 项目提供的代码。

    使用this link可以看到函数strel是如何在Octave中实现的。

    以下代码完全摘自Octave的strel函数,对应案例strel('line')

    ## Parameters (degrees and linelenght)
    degrees = 30
    linelen = 5
    
    
    ## Line length are always odd, to center strel at the middle of the line.
    ## We look it as a diameter of a circle with given slope
    deg90 = mod (degrees, 90);
    if (deg90 > 45)
       alpha = pi * (90 - deg90) / 180;
    else
       alpha = pi * deg90 / 180;
    endif
       ray = (linelen - 1)/2;
    
    ## We are interested only in the discrete rectangle which contains the diameter
    ## However we focus our attention to the bottom left quarter of the circle,
    ## because of the central symmetry.
    c = round (ray * cos (alpha)) + 1;
    r = round (ray * sin (alpha)) + 1;
    
    ## Line rasterization
    line = false (r, c);
    m = tan (alpha);
    x = [1:c];
    y = r - fix (m .* (x - 0.5));
    indexes = sub2ind ([r c], y, x);
    line(indexes) = true;
    
    ## We view the result as 9 blocks.
    # Preparing blocks
    linestrip = line(1, 1:c - 1);
    linerest = line(2:r, 1:c - 1);
    z = false (r - 1, c);
    
    # Assemblying blocks
    SE.nhood =  vertcat (
        horzcat (z, linerest(end:-1:1,end:-1:1)),
        horzcat (linestrip, true, linestrip(end:-1:1,end:-1:1)),
        horzcat (linerest, z(end:-1:1,end:-1:1))
        );
    
    # Rotate/transpose/flip?
    sect = fix (mod (degrees, 180) / 45);
    switch (sect)
        case 1, SE.nhood = transpose (SE.nhood);
        case 2, SE.nhood = rot90 (SE.nhood, 1);
        case 3, SE.nhood = fliplr (SE.nhood);
        otherwise, # do nothing
    endswitch
    
    SE
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2010-12-15
      • 2015-06-19
      • 2012-07-15
      相关资源
      最近更新 更多