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

标签 python matlab image-processing image-morphology

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

我发现像 scikit-learn/opencv/mahotas 这样的 python 库

可是我找不到。

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


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

like strel("line", length, degree)


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

enter image description here

enter image description here

像这样。

谁知道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

关于python - matlab 函数 strel ("line") 到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532316/

相关文章:

python - 删除和/或更新系统的 Python 时虚拟环境的后果

matlab - 如何在神经网络Matlab中将训练数据制作为4D数组 - 输入数据的正确方法

python - 性能 - 为什么具有范围的素数生成算法比使用素数列表快得多?

MATLAB - 动态更新线句柄的 XData 和 YData 的最佳方式?

c++ - 矩阵求逆 GPU MATLAB

image - 通过图像减法优化分割算法

python-3.x - 如何使用 OpenCV Python 遮挡圆圈外的区域?

c# - 比较 2 个 16x16 像素图像的相似度

python - 我怎样才能等到一个元素获得或失去一个类别?

javascript - 如何从 Python 调用 Javascript 函数?