"Too Many Output Arguments" Error Calling imwrite from Python engine

조회 수: 1 (최근 30일)
E Swan
E Swan 2015년 6월 12일
댓글: E Swan 2015년 6월 16일
I have a MATLAB function get_mask defined like so:
function M = get_mask(path)
I = imread(path);
M = im2bw(I, graythresh(I));
end
The image I'm passing in is a 16bpp grayscale TIFF LZW compressed image. I call it from Python using the Python engine:
eng = matlab.engine.start_matlab()
result = eng.get_mask('some_path');
I then pass the result to imwrite:
eng.imwrite(result, r'ml_result.tif', nargout=0)
eng.quit()
The call to imwrite takes ages (3-4 minutes, a separate issue...) before failing with a "Too Many Output Arguments" error. I double checked the type of 'result' and it is indeed a 1040x1392 logical as I expected. However, if I do this instead:
eng.imwrite(matlab.logical([[0, 1, 0], [1, 0, 1]]), r'C:\ml_result.tif', nargout=0)
It works, no problem. I don't understand what the difference is here.

답변 (2개)

E Swan
E Swan 2015년 6월 12일
And, I just figured it out on my own. I lied a bit in my example (shame on me) as I was actually creating one engine instance to call get_mask, stopping it, and then creating another for the call to imwrite. Apparently that is a no-no and, for whatever reason, resulted in this error.
It still takes a while to run, but that's a separate topic.

Bo Li
Bo Li 2015년 6월 15일
By default, the number of output arguments is 1 in Python Engine. nargout needs to specified if more or less results are expected:
  댓글 수: 1
E Swan
E Swan 2015년 6월 16일
Thanks. As you can see in the example, I did set nargout to 0. The problem was using data obtained from one instance of the engine and passing it to another after quitting the first.

댓글을 달려면 로그인하십시오.

카테고리

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