How to convert images from matlab to python.

20 ビュー (過去 30 日間)
Ruihai Wang
Ruihai Wang 2022 年 2 月 1 日
回答済み: Ayush 2023 年 10 月 4 日
I wrote a function that is to convert the picture data from Matlab to python. This function is work well in Matlab, but when I packaged this function and used it in python, there is a wrong that occurred in python: Unable to resolve the name py.numpy.reshape.
function [result] = mat2nparray( matarray )
%mat2nparray Convert a Matlab array into an nparray
% Convert an n-dimensional Matlab array into an equivalent nparray
data_size=size(matarray);
if length(data_size)==1
% 1-D vectors are trivial
result=py.numpy.array(matarray);
elseif length(data_size)==2
% A transpose operation is required either in Matlab, or in Python due
% to the difference between row major and column major ordering
transpose=matarray';
% Pass the array to Python as a vector, and then reshape to the correct
% size
result=py.numpy.reshape(transpose(:)', int32(data_size));
else
% For an n-dimensional array, transpose the first two dimensions to
% sort the storage ordering issue
transpose=permute(matarray,[length(data_size):-1:1]);
% Pass it to python, and then reshape to the python style of matrix
% sizing
result=py.numpy.reshape(transpose(:)', int32(fliplr(size(transpose))));
end
end
  2 件のコメント
Joshua Knicely
Joshua Knicely 2022 年 2 月 1 日
My guess is that python needs numpy imported.
To be clear, are you taking code written in MATLAB and putting it into Python code and trying to run it with Python, or are you calling this MATLAB function (and MATLAB as a whole) through Python? If the first one, that's going to cause lots of problems.
Ruihai Wang
Ruihai Wang 2022 年 2 月 1 日
Thank you for your reply. Actually, I want to call the Matlab function in python to process the image. But I found that this takes a lot of time in data transformation, for example using Matlab's rotate function in python would take 38 seconds. My purpose in writing the mat2nparray function is to reduce the time, it would be great if there are other ways to reduce the time.

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

回答 (1 件)

Ayush
Ayush 2023 年 10 月 4 日

カテゴリ

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