欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python和matlab交互_MATLAB调用python,交互

发布时间:2023/12/19 python 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python和matlab交互_MATLAB调用python,交互 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

MATLAB调用python

1. 先配置 路径

2. 在调用 numpy cv2时,经常会出现问题

Solutions: open matlab in Anaconda Prompt, as shown in the following Figure.

this solution is inspired by https://blog.csdn.net/Yana_Zeng/article/details/103574555

3. my first py script called by matlab, shown as below

however , it always gets error when showing cv2 windows and plt.

it still can not show the window,

maybe I can convert the py script to exe file by pyinstaller as the following blog does: https://blog.csdn.net/qq_27280237/article/details/81561898

import numpy as np

print('not imported np')

def add(a,b):

return a+b

import cv2

import matplotlib.pyplot as plt

print('imported cv2')

import numpy as np

print('imported np')

def cv_test():

img = cv2.imread('pred_01_test.png')

cv2.imshow("test", img)

cv2.waitKey()

print('after key')

cv2.destroyWindow('test')

print('destroy window')

def pltshow():

img = plt.imread('pred_01_test.png')

plt.imshow(img)

plt.show()

#cv_test()

#pltshow()

here is the matlab script that calling the py. It should be noticed that I tried different ways to call py functions.

%%

clc

% pyenv('Version','E:\software\programming\Anaconda\python.exe')

% pyversion('E:\software\programming\Anaconda\python.exe')

thisModule = py.importlib.import_module('pyadd')

py.importlib.reload(thisModule)

py.print('Hello, Python!')

% py.pySkel.test()

py.pyadd.add(2,3)

import py.pyadd.*

add(4,5)

cv_test()

thisModule = py.importlib.import_module('pySkel')

4. matlab 处理 =numpy =的方法:

using double to transform numpy to double mat

int cannot do this

thisModule = py.importlib.import_module('pySkel')

py.importlib.reload(thisModule)

skel_img = py.pySkel.skel_fun();

imshow(double(skel_img))

5. how to pass matlab data to python?

see Pass Data to Python in documentation

I pass double to Python because matlab will pass it as float-memoryview to py without data transformation.

1. Scalar values Only

2. 1-by-N vector

3. Passing *Matrices and Multidimensional Arrays

MATLAB :When you pass real numeric or logical arrays to a Python function, MATLAB automatically converts the data to a Python memoryview object. If the output of a Python function implements the Python buffer protocol and is real numeric or logical, then MATLAB displays:

The actual Python type

The underlying data

The corresponding MATLAB conversion function. Use this function to fully convert the Python object to a MATLAB array.

e.g. 1 提示使用 double 转换

For example, suppose that you call a Python function in a module pyModule that returns a variable of type pyType with these values:

p =

Python pyType:

8 1 6

3 5 7

4 9 2

Use details function to view the properties of the Python object.

Use **double** function to convert to a MATLAB array.

e.g. 2 提示使用 logical 转换为MATLAB array

MATLAB的调用部分

thisModule = py.importlib.import_module('pySkel');

thisModule = py.importlib.reload(thisModule);

% thisModule.skel_fun2( py.array.array( 'd', (eye(10)) ) )

skel_img = logical( thisModule.skel_fun2( eye(10) ) );

imshow(skel_img)

我的py部分代码

def skel_fun2(im):

print(type(im))

im = np.array(im,'bool')

image_remove_small_obj = morphology.remove_small_objects(im ,connectivity=im.ndim, min_size=10)# min_size 是连通区域的像素数量 connectivity=ndim of image by default

# connectivity 只有在 input为bool时才有用, 当为 int时, 0-1 中的 1 会全部被认为是连接的。 soga

skeleton_Large =morphology.skeletonize(image_remove_small_obj)

return skeleton_Large

print(type(im)) 输出为:

原文链接:https://blog.csdn.net/qq_20538071/article/details/107207001

总结

以上是生活随笔为你收集整理的python和matlab交互_MATLAB调用python,交互的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。