MATLAB code to python conversion

31 ビュー (過去 30 日間)
Nikhil Hegde
Nikhil Hegde 2016 年 3 月 8 日
コメント済み: Prb 2019 年 5 月 1 日
I have this piece of MATLAB code that I wanted to convert to Python. I used this online tool called OMPC to convert 90% of the code. There's this one block of code which I couldn't convert. Code:
function [fd, labels] = imFeretDiameter(img, varargin)
theta = 180;
if ~isempty(varargin)
var1 = varargin{1};
if isscalar(var1)
theta = var1;
varargin(1) = [];
elseif ndims(var1) == 2 && sum(size(var1) ~= [1 2]) ~= 0
theta = var1;
varargin(1) = [];
end
end
spacing = [1 1];
origin = [1 1];
calib = false;
if ~isempty(varargin) && sum(size(varargin{1}) == [1 2]) == 2
spacing = varargin{1};
varargin(1) = [];
calib = true;
origin = [0 0];
end
if ~isempty(varargin) && sum(size(varargin{1}) == [1 2]) == 2
origin = varargin{1};
end
nTheta = length(theta);
labels = [];
if ~isempty(varargin) && size(varargin{1}, 2) == 1
labels = varargin{1};
end
if isempty(labels)
labels = imFindLabels(img);
end
nLabels = length(labels);
fd = zeros(nLabels, nTheta);
for i = 1:nLabels
[y, x] = find(img==labels(i));
if isempty(x)
continue;
end
if calib
x = (x-1) * spacing(1) + origin(1);
y = (y-1) * spacing(2) + origin(2);
end
try
inds = convhull(x, y);
x = x(inds);
y = y(inds);
catch ME
end
x = x - mean(x);
y = y - mean(y);
for t = 1:nTheta
theta2 = -theta(t) * pi / 180;
x2 = x * cos(theta2) - y * sin(theta2);
xmin = min(x2);
xmax = max(x2);
dl = spacing(1) * abs(cos(theta2)) + spacing(2) * abs(sin(theta2));
fd(i, t) = xmax - xmin + dl;
end
end
The code is a function that I'm using. 'img' is a black and white image and varargin is an array
[0 90 180]
The part of code which I couldn't convert:
try
inds = convhull(x, y);
x = x(inds);
y = y(inds);
catch ME
Any help in converting this piece of code is appreciated. Thank you!
  2 件のコメント
Pratik Somaiya
Pratik Somaiya 2016 年 7 月 20 日
Since your Catch Me-end block contains nothing, you can use try & except for python conversion, scipy has convhull function you can give it a try!
Cheers!!
Prb
Prb 2019 年 5 月 1 日
hI what was the equivalent function for zeros in python
Shifts = zeros(length(Filters)-1,1,'int16');

サインインしてコメントする。

回答 (3 件)

Bo Li
Bo Li 2016 年 3 月 8 日
You may consider calling the MATLAB function directly from Python instead of converting it:
  1 件のコメント
Nikhil Hegde
Nikhil Hegde 2016 年 3 月 9 日
I'll be using the python code later on RPi. I'm new to RPi and Python so I'm not really sure if I can call matlab from RPi

サインインしてコメントする。


Luis
Luis 2016 年 9 月 30 日
編集済み: Walter Roberson 2016 年 9 月 30 日
You may also take a look to this more recent development: https://pypi.python.org/pypi/smop/0.26.2

Walter Roberson
Walter Roberson 2016 年 3 月 8 日
  2 件のコメント
Nikhil Hegde
Nikhil Hegde 2016 年 3 月 9 日
Thanks for replying. Like I said before I used this online tool called OMPC to convert my matlab code to python. I've no idea about python and I would be absolutely grateful if you could give me the python subsitute for that piece of code.
Walter Roberson
Walter Roberson 2016 年 9 月 30 日
scipy is Python code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by