Problems with 'uigetfile' and 'imread' in simple MATLAB Web App

조회 수: 21 (최근 30일)
Hi All,
I am trying to convert a MATLAB App Designer App to a Web App. After viewing this page on the limitations of a MATLAB Web App, I do not see any incompatibilites. However, I am having a problem with loading images with 'uigetfile' and being able to read them with 'imread'. To investigate this, I created a really simple test app. There is a push button and a UIAxes, and the push button allows the user to upload an image that will be displayed in the UI Axes. Here is the code for said push button.
function imageButtonPushed(app, event)
[files, path] = uigetfile("*.*", 'Multiselect', 'on');
imshow(imread(fullfile(path, files)), 'parent', app.UIAxes);
end
But when I upload a test .tif image, I consistently get this error.
When I navigate to the temporary folder where 'uigetfile' stores the image, I can not open them in any image viewing app. They seem to get corrupted in the process. I can not share the image because it is from a lab and I do not want to get in trouble, but I can guarantee that the image is not corrupted and the process works in an offline setting with the exact same image file. Am I missing something very obvious?
Thank you in advance to anyone who lends their help.
  댓글 수: 11
Walter Roberson
Walter Roberson 2021년 7월 24일
It looks to me as if every 0xc2 is missing in the modified png. I have no explanation -- it is just the  character, nothing special.
Jan
Jan 2021년 7월 24일
There are more differences: The "resultpng.png" starts with 0xc2 and some other 0xc2 are instered also. But e.g.
' 9E 4A F8 FC' becomes
'C2 9E 4A C3 B8 C3 BC'
% Or as bits:
'10011110' % Original
'01001010'
'11111000'
'11111100'
'11000010' % Uploaded
'10011110'
'01001010'
'11000011'
'10111000'
'11000011'
'10111100'
The upload has inserted 25 bytes and modified some existing bytes. It does not look like a confused UTF8 to UTF16 conversion.
Please contact MathWorks to ask for help. I assume that they can debug the problem.

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

채택된 답변

Christian Rouhana
Christian Rouhana 2021년 7월 26일
Hi all,
Thank you for your help. I contacted MATLAB support and this seems to be a bug found in MATLAB R2021a. I installed MATLAB R2020b and do not have this problem anymore.
  댓글 수: 2
Hitesh Jangir
Hitesh Jangir 2022년 2월 15일
Still getting the same error in R2020b? did you get any other solution?
Image Analyst
Image Analyst 2022년 2월 15일
@Hitesh Jangir I don't have that version. Can you upgrade to the latest version? Otherwise can you attach your .mlapp file so that if anyone has that version they can try your code. Attach it in a new question of your own, not here.

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

추가 답변 (4개)

Walter Roberson
Walter Roberson 2021년 7월 22일
My interpretation would be that libTiff on the server is not the same as libTiff on the online version.
In particular if you are using Linux on the server, then you probably need to install an older version of libTiff for MATLAB to use.

Image Analyst
Image Analyst 2021년 7월 24일
"If my goal is to have the user be able to upload one or more image(s) for analysis, is there a correct way to do this?" <== Waht I would do is to have your app load a listbox full of images they can select from so they can just easily click one or more to analyze, and then click the Analyze or Go button to do the batch processing. I'd also have a button that calls uigetdir() so the user can specify what folder's files get loaded into the listbox.
  댓글 수: 3
Image Analyst
Image Analyst 2021년 7월 24일
Wow, that's bad. How are web app users supposed to indicate where on their local computer the data resides? Hand type in the entire path?
Jan
Jan 2021년 7월 25일
You can use uigetfile() in a Web-App to choose files. This uploads the selected files to a local folder, which can be accessed by the web server. The idea is give the web server access only to the selected files. Everything else would be a security problem.
Now the problem of the OP is, that the local copy of the files is damaged by the uigetfile() processing: some bytes are inserted, some bits are modified.
There are more users withthis problem.

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


Artur Krukowski
Artur Krukowski 2022년 3월 15일
편집: Artur Krukowski 2022년 3월 15일
Hello everybody,
Actually the problem persists in all Matlab versions. I followed most of the threads, including suggestion to use R2020b. All those solutions have failed for me, so I went to look for a workaround solution.
The easiest way to realise what happens is to generate a sample binary file with numbers from 0 to 255. When you try to choose it using uigetfile function from your webapp, you will quickly realise that the uploaded file has a longer size than the original one.
When you then compare the content, you will notice that some bytes have been replaced with a combination of two bytes, whereby the first one is 0xC2 or 0xC3. The second byte is then either a copy of the original one (when the first one is 0xC2) or decreased by 64 (when the first one is 0xC3).
Afterwards the solution is piece easy. The most basic version of a function to unscramble such a file is attached.
In your webapp you can then simply:
  1. run uigetfile and make a note of the pathname and filename
  2. unscramble the file using the attached function (personally I exclude the path from the output filename)
  3. the new file is available for subsequent processing as usual
When processing images, you can use the following example approach:
% Display uigetfile dialog
filterspec = {'*.jpg;*.tif;*.png;*.gif','All Image Files'};
[f, p] = uigetfile(filterspec);
% Make sure user didn't cancel uigetfile dialog
if (ischar(p))
fname = [p f];
try
img = imread(fname);
catch ME
% If face problem reading image, unscramble it and reload
unscramble(fname_in, fname_out);
img = imread(fname_out);
end
end
This should work for all Matlab versions.
NOTE: examples provided do NOT fully check for all fault conditions so you may need to add some extra checks.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 3월 15일
The easiest way to realise what happens is to generate a sample binary file with numbers from 0 to 255.
Exactly how do you do that?
What you are describing is as-if the data has been encoded as UTF-8.
Note: MATLAB does not support a 'b' modifier for fopen().

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


Artur Krukowski
Artur Krukowski 2022년 3월 15일
You can use e.g.:
fin=fopen('test.bin','wb');fwrite(fin,0:255);fclose(fin);
Regarding feasibility of "wb" just check example in:
help fwrite
All the best.
Artur

카테고리

Help CenterFile Exchange에서 Package and Share Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by