transform_all_hyuna.m 6.31 KB
clear;

path_name = '/media/hyuna/F4722E97722E5F1C/21.nturgb+d_skeletons/';
fileID = fopen('/media/hyuna/F4722E97722E5F1C/sk.txt','r');
formatSpec = '%s';
sizeA = [20 Inf];
perfect_list = fscanf(fileID,formatSpec,sizeA);
perfect_list = perfect_list.';
fclose(fileID);

L = length(perfect_list);

for K = 1:L
    
    file_name = char(perfect_list(K,:));
    name = strcat(path_name,file_name(1:20),'.skeleton');
    disp(name);
    [token,remainder] = strtok(file_name,'A');
 
    class = str2num(remainder(2:4));
    %class=remainder(2:4);
    
    if class == 7
        bodyinfo = read_skeleton_file(name);
        frame_num = size(bodyinfo,2);   
    
    
        try
            %initialize
            cur_subject_x = zeros(frame_num, 25);
            cur_subject_y = zeros(frame_num, 25);
            cur_subject_z = zeros(frame_num, 25);

            tot_x = zeros(frame_num,25);
            tot_y = zeros(frame_num,25);
            tot_z = zeros(frame_num,25);

            joint_5 = zeros(1,3);
            joint_9 = zeros(1,3);
            joint_1 = zeros(1,3);
            joint_3 = zeros(1,3);

            %get total joints information
            for FN = 1:frame_num
                cur_body = bodyinfo(FN).bodies(1);
                joints = cur_body.joints;

                for JN = 1:25
                    tot_x(FN,JN) = joints(JN).x;
                    tot_y(FN,JN) = joints(JN).y;
                    tot_z(FN,JN) = joints(JN).z;
                end
            end

            %Orientation normalization 1 : in space
            %get median values
            M_x = median(tot_x);
            M_y = median(tot_y);
            M_z = median(tot_z);

            %set 3 points for make plane
            joint_5 = [M_x(5) M_y(5) M_z(5)];
            joint_9 = [M_x(9) M_y(9) M_z(9)];
            joint_1 = [M_x(1) M_y(1) M_z(1)];
            joint_3 = [M_x(3) M_y(3) M_z(3)];

            %find RIGID TRNASFORMATION matrix
            d1 = joint_1 - joint_5;
            d2 = joint_1 - joint_9;
            n1 = cross(d1,d2); % because we will parallel transform, don't need to find belly
            u1 = n1/norm(n1);
            u2 = [0 0 1];
            cs1 = dot(u1,u2)/norm(u1)*norm(u2);
            ss1 = sqrt(1-cs1.^2);
            v1 = cross(u1,u2)/norm(cross(u1,u2));

            R1 = [v1(1)*v1(1)*(1-cs1)+cs1 v1(1)*v1(2)*(1-cs1)-v1(3)*ss1 v1(1)*v1(3)*(1-cs1)+v1(2)*ss1];
            R1(2,:) = [v1(1)*v1(2)*(1-cs1)+v1(3)*ss1 v1(2)*v1(2)*(1-cs1)+cs1 v1(2)*v1(3)*(1-cs1)-v1(1)*ss1];
            R1(3,:) = [v1(1)*v1(3)*(1-cs1)-v1(2)*ss1 v1(2)*v1(3)*(1-cs1)+v1(1)*ss1 v1(3)*v1(3)*(1-cs1)+cs1];

            %1-3 number tolls to parallel x axis. Rigid transformation on plane surface
            %Z axis coords oyler angle transform

            t = joint_3 - joint_1;
            d3 = R1(1,:) * t.';
            d3(1,2) = R1(2,:) * t.';
            d3(1,3) = R1(3,:) * t.';

            u3 = d3(1:2)/norm(d3(1:2));
            v3 = [u3(1) -u3(2)];
            v3(2,:) = [u3(2) u3(1)];
            u4 = [1 0].';

            csss = v3\u4;
            cs2 = csss(1);
            ss2 = csss(2);

            R2 = [cs2 -ss2 0];
            R2(2,:) = [ss2 cs2 0];
            R2(3,:) = [0 0 1];


            %apply rigid transformation
            for FN = 1:frame_num
                cur_body = bodyinfo(FN).bodies(1);
                joints = cur_body.joints;

                for JN = 1:25
                    a = R1(1,:) * [joints(JN).x joints(JN).y joints(JN).z].';
                    b = R1(2,:) * [joints(JN).x joints(JN).y joints(JN).z].';
                    c = R1(3,:) * [joints(JN).x joints(JN).y joints(JN).z].';

                    cur_subject_x(FN,JN) = R2(1,:) * [a b c].';
                    cur_subject_y(FN,JN) = R2(2,:) * [a b c].';
                    cur_subject_z(FN,JN) = R2(3,:) * [a b c].';

                end
            end

            %orientation normalize 2 in plane surface
            if cur_subject_x(1,4) < cur_subject_x(1,1)
                cur_subject_x = 0 - cur_subject_x;
            end

            if cur_subject_y(1,9) > cur_subject_y(1,5)
                cur_subject_y = 0 - cur_subject_y;
            end

            % for save origin subjects before data augment
            clear_subject_x = cur_subject_x;
            clear_subject_y = cur_subject_y;
            clear_subject_z = cur_subject_z;

            % Left <-> Right Change : 2option
            for LR = 1:2
                if LR == 1
                    augment_y = clear_subject_y;
                else
                    augment_y = 0 - clear_subject_y;
                end

                %Height change : 3option
                for HE = 1:3
                    if HE == 1
                        augment_x = clear_subject_x.* 1.2;
                    elseif HE==2
                        augment_x = clear_subject_x.* 1.0;
                    else
                        augment_x = clear_subject_x.* 0.8;
                    end

                    %Give Gaussian Random Variable : 0.01 - 6times
                    for RV = 1:6
                        %3. Gaussian Random filter 0.1
                        cur_subject_x = augment_x + 0.01.*randn(frame_num,25);
                        cur_subject_y = augment_y + 0.01.*randn(frame_num,25);
                        cur_subject_z = clear_subject_z + 0.01.*randn(frame_num,25);

                        % NORMALIZATION
                        cur_subject_x = cur_subject_x - min(cur_subject_x(:));
                        max_tall = max(cur_subject_x(:));
                        cur_subject_x = cur_subject_x ./ max_tall;

                        cur_subject_y = cur_subject_y - min(cur_subject_y(:));
                        cur_subject_y = cur_subject_y ./ max_tall;

                        cur_subject_z = cur_subject_z - min(cur_subject_z(:));
                        cur_subject_z = cur_subject_z ./ max_tall;


                        %Write image
                        motionpatch = cur_subject_x;
                        motionpatch(:,:,2) = cur_subject_y;
                        motionpatch(:,:,3) = cur_subject_z;

                        new_file_name = strcat('/home/hyuna/Documents/actionGAN_work/24/24_kicking',file_name(1:20),'_',num2str(LR),num2str(HE),num2str(RV),'.png');
                        imwrite(motionpatch,new_file_name);

                end
            end
        end
        
    catch
        name
    end
    end
end