A question about Block PCA application

8 ビュー (過去 30 日間)
DongDong
DongDong 2017 年 3 月 24 日
回答済み: Aditya 2025 年 8 月 20 日
I have a problem in applying PCA method to block pictures.I mean,intending to apply PCA method to SubPictures.as the followed:
allsamples = []; Subs = []; for i = 1:64 for j = 1:10 str = 'C:\Users\liu90\Desktop\FV_sample3\train\s_'; in1 = imread(strcat(str,num2str(i),'_',num2str(j),'.bmp')); r = 6; c= 5; [m n] = size(in1); %计算分区的大小 mPartitionSize = floor(m / r); nPartitionSize = floor(n / c);
for ii = 1:r-1
for jj = 1:c-1
Sub = in1((ii-1)*mPartitionSize+1:ii*mPartitionSize,(jj-1)*nPartitionSize+1:jj*nPartitionSize);
Sub = double(Sub);
b=Sub(1:15*30);
b=double(b);
Subs=[Subs;b];
% allsamples = [allsamples;Subs];
end
end
% Subs = fastfPCA(Subs,5);
% Subs(:) =Subs';
allsamples = [allsamples;Subs];
end
end
the dataset contains 640 pictures,each picture will be divided into 6*5=30 SubPictures,how can I do PCA to each picture and then I want to use the data to train SVM to classify. I want the SubPictures of the same previous Pciture to make up a matrix then do PCA to it and use the results to make up a big matrix contains the main information of the 640 pictures,then classify them by SVM. How can I solve the problem?please help me!

回答 (1 件)

Aditya
Aditya 2025 年 8 月 20 日
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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by