How to convert images from matlab to python.
조회 수: 22 (최근 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
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.
답변 (1개)
Ayush
2023년 10월 4일
I understand that you want to convert your matlab data into numpy array in python.
Here are few resources which may be helpful.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!