ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Matlab函数strel(“ line”)转换为python

2019-11-08 17:59:55  阅读:717  来源: 互联网

标签:image-processing image-morphology matlab python


我想在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

标签:image-processing,image-morphology,matlab,python
来源: https://codeday.me/bug/20191108/2009728.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有