Nick Howe's Matlab Exercises

From CSclasswiki
Jump to: navigation, search

Exercises

These can be completed on a single script.

Working With Cells

1. Read images from the current directory into a cell array imList (Hint: dir, cell, imread)

2. Create a new cell array imCol of column sums (Hint: sum, cellfun)

3. Create a new cell array imRow of row sums (Hint: sum, @, cellfun)

4. Create a new matrix imRow2 of all rows from imCol (Hint: cat)

Logic

5. Create a logical variable imchoice that is a random combination of half zeros and half ones, with the same size as image array imlist (Hint: size)

6. Flip all images that are 1's (Hint: flipls, cellfun)

7. Make 2 smaller cell arrays: one with all the 0 images and the other with all the 1 images (Hint: indexing, ~)

Indexing

8. Create a random permutation of indices imoOrder from 1...#images (Hint: rand)

9. Display the images two at a time after the user clicks, according to its imOrder (Hint: subplot, imshow, waitforbuttonpress)

10. Make 2 smaller cell arrays, one with even indices of imOrder, and one with odd (Hint: rem)

Beyond

11. Create a structure array im4D with two fields, fname and img and fill in appropriately (Hint: struct)

12. Make a 4D array by stacking all images from the structural array im4D (Hint: cat)

13. Display the average image (Hint: mean)

Answers

1.

%% Get list of all jpeg files in current directory (I have 4 images)
imageFiles = dir('*.jpeg');   % Put all jpg files into a structure
nFiles = numel(imageFiles);   % Number of files found
imList = cell(1,nFiles);      % Create empty cell

for j = 1:nFiles                        % For every file
   currentFileName = imageFiles(j).name;    % Get file name
   currentImage = imread(currentFileName);  % Read image from graphics file
   imList{j} = currentImage;                % Put image into cell
end

2.

%% Compute column sums of every matrix in cell array imList
imCol = cellfun(@sum, imList, 'UniformOutput',false);  % Output is not scalar, so
                                                       % set UniformOutput to false

3. UniformOutput = uni; false = 0

%% Compute row sums of every matrix in cell array imList
imRow = cellfun(@(x)sum(x,2), imList, 'UniformOutput', false);

or

imRow = cellfun(@(x)sum(x,2), imList, 'uni', 0);


4. If you don't know the length of imCol

%% Create matrix of all rows of imCol
imRow2 = [];                        % Initialize empty matrix
for j = 1:numel(imColumnCell);     % For every matrix in cell array
   imRow2 = horzcat(imRow, imColumnCell{j});
end

If you know the size of imCol (ex. imCol is length 3)

imRow = cellfun(@horzcat, imCol{1}, imCol{2}, imCol{3}, 'UniformOutput', false);

5.

%% Create logical variable of 50/50 zeros & ones, same size as imList
imChoice  = rand(1, numel(imList)) > 0.5;

6.

%% Flip all images that are 1's
imList(imChoice) = cellfun(@fliplr, imList(imChoice), 'UniformOutput', false);

7.

%% Make 2 smaller cell arrays
imOne = imList(imChoice);       % Contains images of variable 1 in imChoice
imZero = imList(~imChoice);     % Contains images of variable 0 in imChoice

8.

%% Create a random permutation of indices, 1...#ofImages
imOrder = randperm(numel(imList));

9.

%% Display the images two at a time, according to its order
nImages = numel(imList);        % The number of images in imList
nRows = ceil(nImages/2);        % # of rows in subplot
subplot(nRows,2,1);             % Create subplot with 2 columns, start at position 1
for x = 1:2:nImages
   disp('Click for the next two images')
   stop = waitforbuttonpress;             %  blocks the caller's execution
                                          %  stream until the function detects 
                                          %  that the user has clicked a mouse 
                                          %  button or pressed a key while the 
                                          %  figure window is active.
   if stop == 0    % When user clicks
   end             % Proceed

   subplot(nRows,2,x)           % Move to next position in subplot
   imshow(imList{imOrder==x})   % Display next image at current position
   subplot(nRows,2,x+1)
   imshow(imList{imOrder==x+1})
end

10.

%% Make 2 smaller cell arrays, one with even indices of imorder, one with odd
imEven = imOrder(rem(imOrder,2)==0);
imOdd = imOrder(~rem(imOrder,2)==0);

11.

%% Make a struct array with two fields, fname and img, fill in appropriately
imStruct = struct('fname',{},'img',{});       % Create empty structural array
for j = 1:nFiles                          % For every file
   currentFileName = imageFiles(j).name;      % Get file name
   currentImage = imread(currentFileName);    % Read image from graphics file
   imStruct(j).fname = currentFileName;       % Record file name
   imStruct(j).img = currentImage;
end

12. Make sure all of your images are of the same size/resolution before you concatenate

%% Make a 4D array by stacking all images from struct; I have 4 images
im4D = cat(4, imStruct(1).img, imStruct(2).img, imStruct(3).img, imStruct(4).img);

13.

%% Display the average image
imMean = mean(im4D, 4);
imshow(imMean)