vbpds.m

来自「Vision based pedestrian detection system」· M 代码 · 共 1,156 行 · 第 1/4 页

M
1,156
字号
                for ImageNumber = 1:obj.Number_of_Images
                     try
                         [obj,Images, Images_Name, Err] = obj.LoadImage(ImageNumber);
                         obj.error_code = '02-02';
                         if strcmp(Err,'loaded')
                             stream(:,:,ImageNumber) = Images(:,:);
                             All_Images_Name(ImageNumber,:) = char(Images_Name(ImageNumber,:)).';
                         else
                             stream(:,:,ImageNumber) = zeros(ImagesDimension(1),ImagesDimension(2));
                             All_Images_Name(ImageNumber,:) = '';
                         end
                     catch e
                        if strcmp(obj.Show_Message, 'true')
                            %warning('VBPDS:LoadStream', 'Can''t load the Image "%s" because %s.\n', All_Images_Name(ImageNumber,:),e.message);
                            disp(['VBPDS:LoadStream : ', ' error_code  : ' obj.error_code]);
                        end
                     end
                end
                obj.error_code = '02-03';
                
                save 'Images_Data' 'stream' -append
                save 'Images_Data' 'All_Images_Name' -append
                
                obj.stream = stream;
                obj.Images_Name = All_Images_Name;
            else
                obj.error_code = '02-04';
                load 'Images_Data' 'stream';
                load 'Images_Data' 'All_Images_Name';
                
                obj.stream = stream;
                obj.Images_Name = All_Images_Name;
            end;    
        end
        function [obj,Image,Images_Name,Err] = LoadImage(obj,ImageNumber)
            %% Load Image
            try
                obj.error_code = '03-01';
                imgNum = strcat('0000',num2str(2 * (obj.Gap_size*(ImageNumber-1)) +1));
                Images_Name(ImageNumber,:) = cellstr(strcat(obj.Images_Path,'img_', ...
                                                  imgNum(length(imgNum)-4:1:length(imgNum)),...
                                                  '.bmp'));

                Image = imread(char(Images_Name(ImageNumber,:)));
                Image = Image(:,:,1);
                obj.error_code = '03-02';
                Err = 'loaded';
            catch e
                if strcmp(obj.Show_Message,  'true')
                    warning('VBPDS:LoadImage', 'Can''t load the image "%s".\n', Images_Name(:,ImageNumber).');
                    disp(['VBPDS:LoadImage', 'error_code  : ' obj.error_code]);
                end
                Images_Name(:,ImageNumber) = cellstr('');
                Image = zeros(obj.ImagesDimension(1),obj.ImagesDimension(2));
                Err = 'error';
                
            end        
        end


        function obj = PSO(obj)
        %%% ####################################################################
        %%% ####                       PSO Algorithm                        ####
        %%% ####                  Naser Ghasemi Jul., 2008                  ####
        %%% ####################################################################
        %% Particle Swarm Optimization
            try
                obj.error_code = '04-01';

                obj = obj.PSO_Initialise;

                obj.error_code = '04-02';
                for count = 1:obj.max_iterations 
                    %disp('find the fitness of each particle at iteration ')
                    %disp(count)
                    obj.current_iteration = count;
                    obj.current_fitness = [];
                    for count_x = 1:obj.no_of_particles
                        
                        obj.current_particle = count_x;
                        
                        obj.error_code = '04-03';
                        obj = obj.Return_Frame;
                        
                        obj.error_code = '04-04';
                        obj = obj.Compute_Fitness;

                        % program control
                            %if current_fitness(count_x) >= Termination_Fitness 
                            %    disp(['fitness >= ' num2str(Termination_Fitness)])
                            %    disp(current_frame_phi)
                            %    disp(All_Patterns_Phi(:,MIN_Delta_phi_index))
                            %end
                            %if count_x == 5
                            %    disp('for control')
                            %end
                        % end of program control

                        %disp([count count_x])
                    end
                    %disp('End of find the fitness of each particle')

                    %% decide on p_best etc for each particle
                    %disp('decide on p_best etc for each particle at iteration')
                    %disp(count)
                    obj.error_code = '04-05';
                    for count_x = 1:obj.no_of_particles
                        if obj.current_fitness(count_x) > obj.p_best_fitness(count_x) % best fitness is minimum
                            obj.p_best_fitness(count_x) = obj.current_fitness(count_x);
                            for count_y = 1:obj.dimensions
                                obj.p_best(count_x,count_y) = obj.particle_position(count_x,count_y);
                            end
                        end
                    end

                    %decide on the global best among all the particles
                    [g_best_val,g_best_index] = max(obj.current_fitness); % best fitness is minimum

                    %g_best contains the position of the global best
                    obj.error_code = '04-06';
                    if g_best_val > obj.g_best_fitness % best fitness is minimum
                        obj.g_best_fitness = g_best_val;
                        for count_y = 1:obj.dimensions
                            obj.g_best(count_y) = obj.particle_position(g_best_index(1),count_y);
                        end
                    end


                    %Tracking Particles
                    obj.error_code = '04-07';
                    for count_x = 1:obj.no_of_particles
                        %obj.result(count,count_x,:) = obj.p_best(count_x,:);
                        %obj.result_fitness(count,count_x) = obj.p_best_fitness(count_x);
                        obj.result(count,count_x,:) = obj.particle_position(count_x,:);
                        obj.result_fitness(count,count_x) = obj.current_fitness(count_x);
                    end

                    % Check Termination Condition
                    obj.error_code = '04-08';
                    if obj.g_best_fitness >= obj.Termination_Fitness % best fitness is minimum
                        break;
                    end

                    %disp(['go to' num2str(g_best_index)])
                    %disp(['particle_position at' num2str(count)])
                    %disp(particle_position)

                    %update the position and velocity compponents
                    for count_x = 1:obj.no_of_particles
                        for count_y = 1:obj.dimensions
                            obj.error_code = '04-09';
                            obj.particle_velocity(count_x,count_y) = ...
                               fix(rem((obj.w * obj.particle_velocity(count_x,count_y) + ...
                                        obj.c1*rand*(obj.p_best(count_x,count_y) - obj.particle_position(count_x,count_y)) + ...
                                        obj.c2*rand*(obj.g_best(count_y)         - obj.particle_position(count_x,count_y))),10));

                            %if particle_velocity(count_x,count_y)<0
                            %    disp([count_x count_y particle_velocity(count_x,count_y)]);
                            %end

                            obj.error_code = '04-10';
                            obj.particle_position(count_x,count_y) = obj.particle_position(count_x,count_y)  + ...
                                                                obj.particle_velocity(count_x,count_y);
                            % Boundry Control
                            if obj.particle_position(count_x,1) <= 0 ;
                                obj.particle_position(count_x,1)=1;
                            end
                            if obj.particle_position(count_x,1) > obj.Images_Dimension(1);
                                obj.particle_position(count_x,1)=obj.Images_Dimension(1);
                            end
                            if obj.particle_position(count_x,2) <= 0 ;
                                obj.particle_position(count_x,2)=1;
                            end
                            if obj.particle_position(count_x,2) > obj.Images_Dimension(2);
                                obj.particle_position(count_x,2)=obj.Images_Dimension(2);
                            end
                        end
                    end
                    %disp(['change particle_position at' num2str(count)])
                    %disp(particle_position)

                    %disp('End of decide on p_best etc for each particle at iteration')
                    disp(['    End of iteration  :  ' num2str(count)])

                end

                %Tracking Particles
                obj.error_code = '04-11';
                obj.result(obj.max_iterations+1,obj.no_of_particles,:) = fix(abs(obj.g_best));
                obj.result_fitness(obj.max_iterations+1,obj.no_of_particles) = obj.g_best_fitness;


            %     % Set Ouputs
            %     for no_result = 1:no_of_particles
            %         if p_best_fitness(no_result) > 80
            %             result(no_result,:) = p_best(no_result,:);
            %             result_fitness(no_result) = p_best_fitness(no_result);
            %         else
            %             result(no_result,:) = [0 0];
            %             result_fitness(no_result) = 0;
            %         end
            %     end
            %     result(no_result+1,:) = fix(abs(g_best));
            %     result_fitness(no_result+1) = g_best_fitness;

                iterations = count;
            catch e
                if strcmp(obj.Show_Message,'true')
                    warning( 'VBPDS:PSO', '  Error in PSO : "%s".\n',e.message);
                    disp(['VBPDS:PSO  ', 'error_code  : ' obj.error_code]);
                end
            end
        end
        function obj = PSO_Initialise(obj)
            %% initialise the parameters
            try
                obj.error_code = '05-01';
%                 obj.particle_position(1,1) = 20;
%                 obj.particle_position(1,2) = 20;
% 
%                 obj.particle_position(2,1) = 20;
%                 obj.particle_position(2,2) = 290;
% 
%                 obj.particle_position(3,1) = 205;
%                 obj.particle_position(3,2) = 290;
% 
%                 obj.particle_position(4,1) = 195;
%                 obj.particle_position(4,2) = 20;
% 
%                 obj.particle_position(5,1) = 120;%84;%136;%
%                 obj.particle_position(5,2) = 160;%112;%198;%

%                 obj.particle_position(6,1) = 1;%156;%84;
%                 obj.particle_position(6,2) = 160;
% 
%                 obj.particle_position(7,1) = 120;
%                 obj.particle_position(7,2) = 303;
% 
%                 obj.particle_position(8,1) = 205;
%                 obj.particle_position(8,2) = 160;
% 
%                 obj.particle_position(9,1) = 120;
%                 obj.particle_position(9,2) = 1;

                for count_x = 1:obj.no_of_particles
                    obj.Color(count_x,:) = [0,0,1];
                    for count_y = 1:obj.dimensions
                        obj.particle_position(count_x,count_y) = abs(ceil(obj.Images_Dimension(count_y)*rand))+1;
                        obj.particle_velocity(count_x,count_y) = ceil(10*rand);
                        obj.p_best(count_x,count_y) = obj.particle_position(count_x,count_y);
                        obj.g_best(count_y) = obj.p_best(count_x,count_y);
                    end
                end
                obj.error_code = '05-02';

                % initialize the p_best_fitness and g_best_fitness
                obj.g_best_fitness = -1000;% best fitness is minimum
                for count = 1:obj.no_of_particles
                    obj.p_best_fitness(count) = -1000;% best fitness is minimums
                end
                obj.error_code = '05-03';

                % Learn Neural Network
                %load 'Global_varibales' 'Neural_Network'

                obj.Current_Image_Number = 1;
                obj.error_code = '05-04';

                obj.Current_Image = obj.stream(:,:,obj.Current_Image_Number);
                %obj.Current_Image = double(im2bw(obj.Current_Image,.4));
                %obj.Current_Image = edge(obj.Current_Image,'canny',[.04 .1]);

                %disp(mean(mean(Current_Image)));
            %     for i = 1:ImagesDimension(1)
            %         for j =1:ImagesDimension(2)
            %             tmp(i,j) = Current_Image(i,j) - 30;
            %         end
            %     end
            catch e
                if strcmp(obj.Show_Message,'true')
                    warning( 'VBPDS:PSO_Initialise', '  Error in PSO_Initialise : "%s".\n',e.message);
                    disp(['VBPDS:PSO_Initialise  ', 'error_code  : ' obj.error_code]);
                end
            end
        end
        function [obj,frame] = Return_Frame(obj)
        %% Cut a frame from image
            try
                obj.error_code = '06-01';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?