How to convert images from matlab to python.

22 vues (au cours des 30 derniers jours)
Ruihai Wang
Ruihai Wang le 1 Fév 2022
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 commentaires
Joshua Knicely
Joshua Knicely le 1 Fév 2022
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 le 1 Fév 2022
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.

Connectez-vous pour commenter.

Réponses (1)

Ayush
Ayush le 4 Oct 2023

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by