How to convert images from matlab to python.

14 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by