Hi Dong,
To perform PCA on subpictures (blocks) of each image and prepare the data for SVM classification, you should first divide each image into 30 blocks, vectorize each block, and stack these vectors into a matrix for that image. For each image, apply PCA to its matrix of block vectors, extracting a fixed number of principal components per block (for example, 5). Flatten these PCA features into a single feature vector for the image. Repeat this process for all 640 images, storing each image’s feature vector as a row in a larger matrix. This matrix can then be used to train an SVM classifier, with each row representing an image and each column a PCA-derived feature. This approach captures the main information from each image’s blocks and makes it suitable for classification.
num_images = 640;
num_blocks = 30; % 6*5
block_feature_dim = 5; % Number of PCA features per block
all_features = zeros(num_images, num_blocks * block_feature_dim);
img_idx = 0; % Image counter
for i = 1:64
for j = 1:10
img_idx = img_idx + 1;
str = 'C:\Users\liu90\Desktop\FV_sample3\train\s_';
in1 = imread(strcat(str, num2str(i), '_', num2str(j), '.bmp'));
in1 = double(in1);
r = 6; c = 5;
[m, n] = size(in1);
mPartitionSize = floor(m / r);
nPartitionSize = floor(n / c);
blocks = [];
for ii = 1:r
for jj = 1:c
row_start = (ii-1)*mPartitionSize + 1;
if ii == r
row_end = m;
else
row_end = ii*mPartitionSize;
end
col_start = (jj-1)*nPartitionSize + 1;
if jj == c
col_end = n;
else
col_end = jj*nPartitionSize;
end
Sub = in1(row_start:row_end, col_start:col_end);
blocks = [blocks; Sub(:)'];
end
end
% Apply PCA to the 30 blocks of this image
[~, score, ~] = pca(blocks, 'NumComponents', block_feature_dim);
% Flatten and store as features for this image
all_features(img_idx, :) = score(:)';
end
end
% Suppose you have a label vector 'labels' (length 640)
SVMModel = fitcecoc(all_features, labels); % Multiclass SVM training
