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

27 views (last 30 days)
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 Comments
Jan
Jan on 24 Jul 2021
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.

Sign in to comment.

Accepted Answer

Christian Rouhana
Christian Rouhana on 26 Jul 2021
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 Comments
Image Analyst
Image Analyst on 15 Feb 2022
@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.

Sign in to comment.

More Answers (4)

Walter Roberson
Walter Roberson on 22 Jul 2021
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 on 24 Jul 2021
"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 Comments
Jan
Jan on 25 Jul 2021
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.

Sign in to comment.


Artur Krukowski
Artur Krukowski on 15 Mar 2022
Edited: Artur Krukowski on 15 Mar 2022
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 Comment
Walter Roberson
Walter Roberson on 15 Mar 2022
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().

Sign in to comment.


Artur Krukowski
Artur Krukowski on 15 Mar 2022
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

Community Treasure Hunt

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

Start Hunting!

Translated by