Embed image in Matlab function

3 次查看(过去 30 天)
NvRain
NvRain 2016-2-8
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.

回答(2 个)

Walter Roberson
Walter Roberson 2016-2-8
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 个评论
NvRain
NvRain 2016-2-8
Does the image have to be grayscale? mat2str() is giving me an error saying the input must be 2-D.
Walter Roberson
Walter Roberson 2016-2-8
You could do the three panes individually and cat(3,R,G,B) the result to form an RGB image.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-2-9
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).

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by