Matlab函数strel(“ line”)转换为python
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Matlab函数strel(“ line”)转换为python,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1951字,纯文字阅读大概需要3分钟。
内容图文
![Matlab函数strel(“ line”)转换为python](/upload/InfoBanner/zyjiaocheng/669/5a6bb302546447838554f8d56e896771.jpg)
我想在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)的算法,请告诉我.谢谢.
解决方法:
对于此类问题,您始终可以检查项目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
内容总结
以上是互联网集市为您收集整理的Matlab函数strel(“ line”)转换为python全部内容,希望文章能够帮你解决Matlab函数strel(“ line”)转换为python所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。