python调⽤matlab函数_从Python调⽤MATLAB函数的三种⽅
0. 实验环境Ubuntu 16.04
Matlab R2015b
1. 借助于 mlab 库
安装⽅法⾮常简单,只需⼀⾏命令 sudo pip install mlab 即可。
import numpy as np
from PIL import Image
leases import latest_release as matlab
image = Image.open('1.jpg')
image = np.array(image)
h, w = image.shape
print(image.shape) # (413, 295)
在上⾯的代码中,我们先读⼊⼀个图⽚,然后将其转化为⼀个 Numpy 数组。接下来,假如我们想通过调⽤ MATLAB 的 imresize 函数来对这幅图像进⾏ 4 倍上采样,那么我们要做的就是将这个 Numpy 数组传递到 MATLAB 中,然后调⽤相应的函数,最后再将结果返回到Python 中。
但是,MATLAB 并不⽀持将 Python 中的 Numpy 数组直接映射为矩阵,具体映射⽅式可参考 matlab.double(initializer=None,
size=None, is_complex=False) 构造函数
image = shape(-1, 1)
image = list()
image = matlab.double(image)
image = shape(image, (h, w))
resized_image = matlab.imresize(image, 4, 'bicubic')
print(resized_image.shape) # (1652, 1180)
如果我们想要调⽤⾃定义函数,⽐如下⾯这样的 m ⽂件。
function c = add(a, b)
c = a + b;
end
那么只需要传递相应的参数进去即可,这⾥ Python 中的浮点数会映射为 MATLAB 中的 double 类型。
result = matlab.add(2.0, 3.0)
print(result) # 5.0
但是,⽬前在我这边发现 mlab 不⽀持 Python 3,安装后会提⽰ ImportError: No module named 'mlabwrap' 之类的错误,暂时还没到解决⽅案。
2. 借助于 MATLAB ⾃带的引擎 API
⾸先,需要 /usr/local/MATLAB/R2015b/extern/engines/python,然后运⾏命令 sudo python setup.py install 即可。
由于我的 MATLAB 版本还⽐较低,⽬前只⽀持到 Python 3.4,更⾼的版本则会报错 OSError: MATLAB Engine for Python supports Python version 2.7, 3.3 and 3.4, but your version of Python is 3.5。
>>> ine
>>> eng = ine.start_matlab()
>>> import numpy as np
>>> image = np.random.randn(30, 30)
>>> image.shape
(30, 30)
>>> resized_image = eng.imresize(image, 4, 'bicubic')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/matlab/engine/matlabengine.py", line 79, in __call__
out=_stdout, err=_stderr)
python 定义数组TypeError: unsupported Python data type: numpy.ndarray
>>> image = shape(-1, 1)
>>> image = list()
>>> image = matlab.double(image)
>>> shape((30, 30))
>>> resize_image = eng.imresize(image, 4, 'bicubic')
>>> resize_image.size
(120, 120)
>>> eng.add(2.0, 3.0)
5.0
>>>
⽤法和第⼀种类似,但是在我这边测试发现只能运⾏在交互模式下,直接运⾏对应的 py ⽂件则会报错。
senius@HP:~/Downloads$ python2 test1.py
Traceback (most recent call last):
File "test1.py", line 3, in
ine
File "/usr/local/lib/python2.7/dist-packages/matlab/engine/__init__.py", line 60, in
raise EnvironmentError('The installation of MATLAB Engine for Python is '
EnvironmentError: The installation of MATLAB Engine for Python is corrupted.
Please reinstall it or contact MathWorks Technical Support for assistance.
上⾯两种⽅法都只能进⾏⼀些简单的调⽤,⽽且还需要在 Python 和 MATLAB 之间进⾏数据转化,⾮常不⽅便,下⾯介绍的第三种⽅法则⾮常简单有效。
3. 借助于 transplant 库
sudo pip3 install transplant。Python 中的列表会转化为 MATLAB 中的元胞数组,Numpy 数组会转化为 MATLAB 中的矩阵,更多详细信息可参阅
>>> import transplant
>>> import numpy as np
>>> matlab = transplant.Matlab(jvm=False, desktop=False)
< M A T L A B (R) >
Copyright 1984-2015 The MathWorks, Inc.
R2015b (8.6.0.267246) 64-bit (glnxa64)
August 20, 2015
For product information, visit www.mathworks.
>>> image = np.random.randn(30, 30)
>>> image.shape
(30, 30)
>>> resized_image = matlab.imresize(image, 4, 'bicubic')
>>> resized_image[0]
array([[ 0.78619134, 0.7167187 , 0.57147529, ..., -0.1314248 ,
-0.19615895, -0.22226921],
[ 0.69992414, 0.63668882, 0.50463212, ..., -0.10023177,
-0.16053713, -0.18483134],
[ 0.51579787, 0.46606682, 0.36253926, ..., -0.01897913,
-0.07015085, -0.09065985],
...,
[ 0.27508006, 0.27579099, 0.28756401, ..., -0.70385557,
-0.80006309, -0.84680474],
[ 0.23260259, 0.23527929, 0.25076149, ..., -0.75840955,
-0.80315767, -0.82872907],
[ 0.21943022, 0.2228168 , 0.2396492 , ..., -0.78075353,
-0.80353953, -0.8200766 ]])
>>> resized_image[0].shape
(120, 120)
>>>
针对如下所⽰的多个⾃定义函数存在互相调⽤的复杂情况,transplant 也可以轻松胜任。
⽐如,我们需要通过 Python 调⽤ NGmeet_DeNoising( N_Img, O_Img, nSig ) 这个函数,它有三个输⼊,N_Img 为长×宽×波段的三维噪声⾼光谱图像,O_Img 为对应的⼲净图像,⽽ nSig 为噪声等级。那么只需在 Python 中将两个 Numpy 数组和⼀个整数传给对应的函数即可。
clean = np.load('GT_crop.npy')
h, w, b = clean.shape
sigma = 25
noisy = clean + np.random.randn(h, w, b) * sigma / 255
print(cal_psnr(clean, noisy)) # 20.1707
c = matlab.NGmeet_DeNoising(255.0*noisy, 255.0*clean, sigma)
print(c.shape, c.dtype) # (200, 200, 191) float64
print(matlab.mpsnr(c/255, clean)) # 34.5712
如果报如下的错误,则因为 par_nSig 是将 Python 中的 sigma=25 转化为了 MATLAB 中的 int64,⽽ int64 数据不能与 double 类数据相乘。⽽解决办法也很简单,double(par_nSig) 将其数据转化为 double 类型即可。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。