How to load all files from a directory of two different types in MATLAB - image

I know that it is possible to load all files of type .gif by using:
files = dir('C:\myfolder\*.gif');
However, my problem is that I want to load all files of type .gif and .jpg. What would be a good way of doing this?

You can simply search for both .gif and .jpg files then load and process the images one by one.
Just invoke dir twice - one for each type of image and store the results in two separate structures. Next, concatenate all of the file names to one structure, then go ahead and do your processing for all of the images.
Something like this:
%// Specify the folder where your images are stored
folder = fullfile('path', 'to', 'your', 'folder');
%// Specify search pattern for JPEG and GIF files
jpgFileFolder = fullfile(folder, '*.jpg');
gifFileFolder = fullfile(folder, '*.gif');
%// Invoke dir for both types of images
d1 = dir(jpgFileFolder);
d2 = dir(gifFileFolder);
%// Concatenate both dir structures together into a single structure
d = [d1; d2];
%// For each image we have...
for idx = 1 : numel(d)
%// Get full path to file
f = fullfile(folder, d(idx).name);
%// Read in the image
im = imread(f);
%// Do something with this image
%//...
%//...
end
fullfile allows you to create a directory string that is OS independent. Simply take each subdirectory that is part of your string and place them as separate string arguments into fullfile and it should work fine.

Related

Control the Pandoc word document output size / test image sizes

My client wants to convert markdown text to word and we'll be using Pandoc. However, we want to control malicious submissions (e.g., a Markdown doc with 1000 externally hosted images each being 10 MB) that can stress/break the server when attempting to produce the output.
options are to regex the image patterns in the Markdown and test their size (or even limit the number) or even disallow external images entirely, but I wonder if there's a way to abort Pandoc if the produced docx exceeds a certain size?
Or is there a simple way to get the images and test their size?
Pandoc normally fetches the images while writing the output file, but you can take control of that by using a Lua filter to fetch the images yourself. This allows to stop fetching as soon as the combined size of the images becomes too large.
local total_size_images = 0
local max_images_size = 100000 -- in bytes
-- Process all images
function Image (img)
-- use pandoc's default method to fetch the image contents
local mimetype, contents = pandoc.mediabag.fetch(img.src)
-- check that contents isn't too large
total_size_images = total_size_images + #contents
if total_size_images > max_images_size then
error('images too large!')
end
-- replace image path with the hash of the image's contents.
local new_filename = pandoc.utils.sha1(contents)
-- store image in pandoc's "mediabag", so it won't be fetched again.
pandoc.mediabag.insert(new_filename, mimetype, contents)
img.src = new_filename
-- return the modified image
return img
end
Please make sure to read the section "A note on security" in the pandoc manual before publishing the app.

Access file inside folder, matlab mac

I have about 300 files I would like to access and import in Matlab, all these files are inside 300 folders.
The first file lie in the directory users/matt/Documents/folder_1 with the filename line.csv the 2nd file lie in users/matt/Documents/folder_2 with filename line.csv
So I would like to import the data from the 300 line.csv files in Matlab so I can take the average value. Is this possible? I am using mac osx btw.
I know what do with the .csv file, but I have no clue how to access them efficiently.
This should work: All we are doing is generating the string for every file path using sprintf and the loop index i, and then reading the csv file using csvread and storing the data in a cell array.
for i = 1:300 % Loop 300 times.
% Full path pointing to the csv file.
file_path = sprintf('users/matt/Documents/folder_%d/line.csv', i);
% Read data from csv and store it in a cell array.
data{i} = csvread(file_path);
end
% Do your computations here.
% ...
Remember to replace the 300 by the actual number of folders that you have.

Cutting data appended to a .jpg file and save as mpg file

The background of my problem is that I want to extract the video data of Motion Photos (taken by my Samsung S7). Manually it is easy but time consuming. Just open the .jpg file in a HexEditor and extract all data after the line "MotionPhoto_Data". The first part is the image and the second part is the video.
My current code is
im = 'test.jpg'
with open(im, 'rb') as fin:
data = fin.read()
data_latin = data.decode('latin1')
fin.close()
position = data_latin.find('MotionPhoto_Data')
data_pic = data[:position]
data_mpg = data[position:]
My problem now is that I can´t figure out how to save these strings in a way that data_pic is saved as a working jpg and data_mpg as a working video.
I tried
with open('test_pic.jpg', 'a') as fin:
fin.write(str(data_pic))
fin.close()
But this didn´t worked. I think there is a basic issue on how I try to handle/save my data but I can´t figure out how to fix this.
I assume you use python 3 as it is tagged that way.
You should not decode with 'data.decode('latin1'). It is binary data.
data = fin.read()
Then later write it also as binary data:
with open('test_pic.jpg', 'ab') as fout:
fout.write(data_pic)
fout.close()

Iterate through directories and compare two images [MATLAB]

I want to compute the image quality metric VIF with MATLAB. Therefore I downloaded vifp_mscale.m. Now I could use the function vif = vifvec(img1, img2). But I have two directories (including subdirectories) where I have a bunch of images. How can I loop through these folders and compare the images properly?
Use dir:
ImageFolder1 = dir([pwd '/subfolder1/*.png']) % Or whatever file extension
ImageFolder2 = dir([pwd '/subfolder2/*.png'])
Now you can loop over the contents of the structures ImageFolder1 and ImageFolder2.

Turning a list of images into a movie

I have a folder of jpg files and I want to make them into a movie. I am using this script:
% Create video out of list of jpgs
clear
clc
% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;
% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'\*.jpg'));
% Sort by date from the datenum information.
S = [jpegFiles(:).datenum];
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.
% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'\MyVideo');
writerObj = VideoWriter(VideoFile);
% Define the video frames per second speed (fps)
fps = 1;
writerObj.FrameRate = fps;
% Open file for writing video data
open(writerObj);
% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
Frame = imread(strcat(ImagesFolder,'\',jpegFilesS(t).name));
writeVideo(writerObj,im2frame(Frame));
end
% Close the file after writing the video data
close(writerObj);
(Courtesy of http://imageprocessingblog.com/how-to-create-a-video-from-image-files/)
But it gives me this error:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 289
In Movie_jpgCompilation at 37
I'm sure my jpg files are fine, and they are in the folder I specify. What is the problem?
(This is my first post ever, so I hope it helps).
If you're on Linux, don't the backslashes need to be forward slashes? When I ran it on my Mac, my jpegFiles was an empty Struct. When I changed them around it worked:
% Create video out of list of jpgs
clear
clc
% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;
% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'/*.jpg'));
% Sort by date from the datenum information.
S = [jpegFiles(:).datenum];
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.
% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'/MyVideo.avi');
writerObj = VideoWriter(VideoFile);
% Define the video frames per second speed (fps)
fps = 1;
writerObj.FrameRate = fps;
% Open file for writing video data
open(writerObj);
% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
Frame = imread(strcat(ImagesFolder,'/',jpegFilesS(t).name));
writeVideo(writerObj,im2frame(Frame));
end
% Close the file after writing the video data
close(writerObj);
Edit: You can also use filesep so that the file separator is OS-specific. http://www.mathworks.com/help/matlab/ref/filesep.html
It would be simpler to use Windows Movie Maker [windows] or iMovie [mac]. For your purposes though you should use PowerPoint.

Resources