- Experimental software: matlab
- The experiment content: (1) According to ABERDEEN face database and some face images in FERET face database, complete the extraction of image data, and randomly divide it into training sample Xtrain and test sample Xtest, and give the label Xlabel of training sample at the same time. If the image size is inconsistent, use imresize function to adjust to a uniform size; (2)When salt and pepper noise is present in the image, Xtrnoise of training samples and Xtenoise of test samples are obtained.
Tips
strcat(): Used as a string merge
Example:
size(): Gets the number of rows and columns of a matrix
(1)s=size(a),
When there is only one output parameter, a row vector is returned, the first element of which is the number of rows of the matrix and the second element is the number of columns of the matrix.
For example, first create a two-dimensional array a
Then output size(a)
Returns two numbers, the first representing rows and the second representing columns.
(2)[r, c]=size(a),
When there are two output parameters, the size function returns the number of rows of the matrix to the first output variable r and the number of columns of the matrix to the second output variable c.
(3) size(a, n) If a n additional n is added to the input parameter of the size function, size returns the number of rows or columns of the matrix, where r=size(a, 1) the number of rows in matrix A when the statement returns and c=size(a, 2) the number of columns in matrix A when the statement returns.
Example:
dir(): Displays folders and related information
Example:
parameter | Effect |
---|---|
dir ( ' . ' ) | List all subfolders and subfiles in the current D directory |
dir(' D:\... ') | List all subfolders and files in the specified directory |
dir( ' *.m' ) | List all files whose current directory suffix is.m file |
(),[],{}
{} braces, used for allocation or reference of cell-type arrays.
[] is called middle bracket, which stores matrices and vectors (vectors are also called containers in C++)
() parentheses, which refer to the elements of an array.
Key/Difficult Points
The difficulty of Experiment 2 is mainly in file access. When you start using matlab, you are used to viewing variables in the workspace, adjusting the code according to the variables, which involves traversal. Some parameters can be either iteratively updated or iteratively overwritten, so you need to understand the role of each line of code in order to understand the whole process. The overall idea of the experiment is for'adri'separately.An','alison'two people's pictures are counted, where wildcards are used to match information about similar information in folders and store it in pics (as shown below).
This is where the coverage was mentioned earlier, so only information about'alison'is available in the final workspace, and then the location pic_path where the pictures need to be read is recorded (if pictures are emitted out of order, there is another way to combine file paths, as you can see in the next article)., the read pictures are stored in im, and pictures are stored in a computer in three-channel storage (RGB), i.e. a three-dimensional array, where the pictures are reduced to one dimension (black and white) for ease of calculation and are formatted uniformly by imresize(), which will cause problems in later calculation if not, and the data is maintained in nnim. Finally, the nnim data is assigned to column t of D.(In machine learning, sample data is usually recorded as a unit), and pictures are labeled according to the current value of i, which makes it easy to calculate the later dichotomy.
Experimental Code
clear, clc % Empty Variables and Commands Window path = '...\Face database\ABERDEEN Face database\'; name = {'adrian', 'alison'}; t = 1;% Define a variable to size the set for i = 1:2 % Traverse twice to access'adrian', 'alison' data_path = strcat(path, name(i)); pics_path = strcat(data_path, '*.jpg'); pics = dir(pics_path{1}); for j = 1:size(pics) pic_path = strcat(data_path, int2str(j), '.jpg'); im = imread(pic_path{1}); num = size(im, 3); if num == 3 nim = rgb2gray(im); nnim = imresize(nim, [255, 255]); else end D(:,t) = nnim(:); if i == 1 Label(1, t) = 1; else Label(1, t) = -1; end t = t + 1; end end rand_adrian = randperm(5); rand_alison = randperm(3); X_train = D(:, [rand_adrian(1:3),rand_alison(1:2)+5]); Y_train = Label(:, [rand_adrian(1:3),rand_alison(1:2)+5]);% Y_train == trian_label X_test = D(:, [rand_adrian(4:5),rand_alison(3)+5]); Y_test = Label(:, [rand_adrian(4:5),rand_alison(3)+5]);% Y_test == test_label D_noise = imnoise(D,'salt & pepper',0.1); X_trnoise = D_noise(:, [rand_adrian(1:3),rand_alison(1:2)+5]); Y_trnoise = Label(:, [rand_adrian(1:3),rand_alison(1:2)+5]);% Y_trnoise == noise_trian_label X_tnoise = D_noise(:, [rand_adrian(4:5),rand_alison(3)+5]); Y_tnoise = Label(:, [rand_adrian(4:5),rand_alison(3)+5]);% Y_tnoise == noise_test_label