首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matlab函数strel("line")到python

matlab函数strel("line")到python
EN

Stack Overflow用户
提问于 2018-07-26 06:29:28
回答 1查看 2.5K关注 0票数 4

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

我发现了python库,比如scikit-learn / opencv / mahotas

但我找不到。

最后,我在中发现了类似的函数,但它不同于matlab函数。

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

像条纹(“线”,长度,)

Matlab实例

代码语言:javascript
复制
a = strel("line",5,0)
b = strel("line",5,30)
c = strel("line",5,45)

输出是

就像这样。

任何人知道matlab的strel(“线”,长度,度)函数的算法或python库等于一条(“线”,长度,度),请告诉我。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 09:19:28

对于这类问题,您可以始终检查项目Octave提供的代码。

使用此链接,您可以看到strel函数是如何在Octave实现的。

下面的代码完全是从八度的strel函数中提取出来的,并对应于大小写strel('line')

代码语言:javascript
复制
## 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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51532316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档