How to convert images from matlab to python.

조회 수: 22 (최근 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 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