Embed image in Matlab function

9 views (last 30 days)
NvRain
NvRain on 8 Feb 2016
Answered: Image Analyst on 9 Feb 2016
Is there a way to embed an image in Matlab code so I don't have to use a separate file in the directory to load? I'm trying to insert an image into an UI but would like to keep it as one file instead of code and the needed image.

Answers (2)

Walter Roberson
Walter Roberson on 8 Feb 2016
You can use mat2str() on your image to get text equivalent to the array (last decimal place not guaranteed for floating point I suspect.) Copy / paste, make it an assignment, and remember to uint8() it if appropriate.
  2 Comments
NvRain
NvRain on 8 Feb 2016
Does the image have to be grayscale? mat2str() is giving me an error saying the input must be 2-D.
Walter Roberson
Walter Roberson on 8 Feb 2016
You could do the three panes individually and cat(3,R,G,B) the result to form an RGB image.

Sign in to comment.


Image Analyst
Image Analyst on 9 Feb 2016
You can use imread() to read in your variable. Then type the name of the variable on the command line. It will spew tons of pixel values into the window. Hopefully your image is small enough so that it doesn't run off the top of your command window.
>> myImage
(tons of numbers show up -- copy them to the clipboard)
Now, in your script, create a variable in your code and past in those numbers. Close with a bracket and semicolon.
myImage = [ (now paste in all the numbers) ];
imshow(myImage);
Now you're defining the variable in your script instead of reading it in from a file. If it's color, you might have to do it a color channel at a time. Personally, I'd not worry about it and just use two files - the script and the image file (PNG or GIF or whatever).

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!