How to convert jpg image files to mat files
18 views (last 30 days)
Show older comments
Hello, I have been give a task to convert multiple jpg image files to matlab mat files each having 5 fields. I tried using the import data tab but was only given one field. But when the matlab function 'load(filename,'-mat')' was used it produced a mat file with 5 field names. Please could anyone help me with a code to convert multiple jpg images to give same result. I have attached 3 of the jpg images in question. Thanks.
1 Comment
Image Analyst
on 25 Sep 2015
First you read the image(s) into MATLAB with imread(). Now, how many image arrays go into each .mat file? One? If so, then one field would be the image array itself, but what do you want the other 4 fields to be?
Answers (1)
Star Strider
on 25 Sep 2015
No conversion necessary. Just save them in the .mat file as images.
For example:
img_str = {sprintf('IR_%04d.jpg\n', 6710:6715)}; % Generate Image Filenames To Save
img_names = regexp(img_str{:}(1:end-1), '\n', 'split'); % Cell Array Of Them
filename = 'IR_Files01.mat'; % Filename For ‘.mat’ File
save(filename, img_names) % Save To File
I tested only the first two lines (for ‘img_str’ and ‘img_names’), so I leave the rest for you to experiment with. You may have to create one or two loops to automate the filename and image name start number creation.
12 Comments
arun ghosh
on 17 Feb 2021
Dear Madura, The file name"IM000086.jpg" taken by digital camera hp715 & stored in pictures album in my Laptop.I want to transform into dot m(.m) & process as ususal. Would you please help me with step-by-step procedure. Arun ghosh.
Walter Roberson
on 17 Feb 2021
That can be done, but what benefit do you see from generating a file of code that hold the content of the image? Your HP715 camera has a maximum resolution of 2048 x 1536 , and is RGB, so that would be over 6000 lines of text that looked like
255,217,218,47,48,53,0,1,0,0,0,3,0,0,58,122,247 (on for a total of 1536 entries)
and would require approximately 2048*1536*3*4 characters, roughly 37 megabytes.
What are your purposes that you want the image converted to .m file?
Wouldn't it be easier to just read in the file every time you needed it?
IM86 = imread('~/Pictures/IM000086.jpg');
I am assuming here from your refrence to "pictures album" that you are using MacOS and the file is stored directly in your Pictures directory... and not, for example, instead stored in your iPhotos album.
See Also
Categories
Find more on Convert Image Type 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!