Created
November 17, 2019 11:18
-
-
Save k1mny/60c531b91a19c610c3ef901a95271df1 to your computer and use it in GitHub Desktop.
UCFデータにあるArcheryのPCA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% load video and culc PCA | |
data_dir = "./Archery_avi/"; | |
num_video = 7; | |
video_list = cell(num_video, 1); | |
for i_v = 1 : num_video | |
video_list{i_v} = data_dir + "v_Archery_g02_c0" + num2str(i_v); | |
end | |
pc_range = 9; % 考慮するPCの範囲 | |
for itr = 1 : num_video | |
v = VideoReader(video_list{itr} + ".avi"); | |
all_img_vecs = []; | |
all_img_data = cell(2, 1); | |
idx = 1; | |
img_size = []; | |
while hasFrame(v) | |
video = readFrame(v); | |
img_data = im2double(rgb2gray(video)); % grayスケールで処理 | |
img_data_resize = imresize(img_data, 0.2); | |
img_vec = reshape(img_data_resize, [size(img_data_resize, 1)*size(img_data_resize,2), 1]); % ベクトル化 | |
if isempty(img_size) | |
img_size = size(img_data_resize); | |
end | |
all_img_vecs = [all_img_vecs, img_vec]; | |
all_img_data{idx} = img_data_resize; | |
idx = idx + 1; | |
end | |
all_img_vecs = zscore(all_img_vecs); % ゼロ平均化 | |
[coef, score, latent] = pca(all_img_vecs); | |
curr_pc = cell(pc_range, 1); | |
for i_pc = 1 : pc_range | |
curr_pc_vec = zeros(size(all_img_vecs, 1), 1); | |
for i_img = 1 : length(latent) | |
cur_img = all_img_data{i_img}; | |
curr_pc_vec = curr_pc_vec + coef(i_img, i_pc) .* all_img_vecs(:,i_img); | |
end | |
curr_pc{i_pc} = curr_pc_vec ./ length(latent); | |
end | |
% plot PC #1~9 | |
for i_pc = 1 : pc_range | |
f = figure('visible', 'off'); | |
imagesc(reshape(curr_pc{i_pc}, img_size)) | |
colormap gray | |
axis off | |
% pause | |
saveas(gcf, "./PC_img/"+extractAfter(video_list{itr}, data_dir)+"/pc_"+num2str(i_pc)+".png") | |
close(f) | |
end | |
f = figure; | |
plot(latent(1:10) ./ length(latent), 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'r') | |
hold on | |
plot(latent(1:10) ./ length(latent), '-', 'Color', 'k') | |
saveas(gcf, "./PC_img/"+extractAfter(video_list{itr}, data_dir)+"/pc_contribute.png") | |
close(f) | |
% plot data in PC #1~3, 4~6, 7~9 | |
f = figure('Position', [0, 100, 1900, 500], 'visible', 'off'); | |
for i_plot = 1 : 3 | |
subplot(1,3,i_plot) | |
p = plot3(score(:, 1+(i_plot-1)*3), score(:, 2+(i_plot-1)*3), score(:, 3+(i_plot-1)*3), 'LineWidth', 1); | |
n = length(score); | |
cd = [uint8(jet(n)*255) uint8(ones(n,1))].'; | |
drawnow | |
set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd) | |
end | |
saveas(gcf, "./PC_img/"+extractAfter(video_list{itr}, data_dir)+"/data_on_pc.png") | |
close(f) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment