4

I want to use the matlab function strel("line") in python

I found python library like scikit-learn / opencv / mahotas

but I can't find it.

finally, I found similar function in pymorph 'seline' but it is different to matlab strel function.


specifically, I want to use(or implement) strel("line") and rotate it.

like strel("line", length, degree)


Matlab example
a = strel("line",5,0)
b = strel("line",5,30)
c = strel("line",5,45)
output is

enter image description here

enter image description here

like this.

Anyone who knows the algorithm of matlab's strel("line",length,degree) function or python library equal to strel("line",length,degree), please let me know. thank you.

1 Answers1

3

For this kind of question you can always check the code provided by the project Octave.

Using this link you can see how the function strel is implemented in Octave.

The following code is entirely extracted from the Octave's strel function and correspond to the case 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
obchardon
  • 10,614
  • 1
  • 17
  • 33