日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/19 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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,交互的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。