Code

Pipeline
                    
                    imageList = ["3d-glasses.png", "abstract-013.png", "abstract-024.png", "abstract-068.png", "airplane.png", "alarm-clock.png", "aquarium.png"];

                    % upsample image
                    %newImg = imresize(img64, 64);

                    % number of projections
                    global numberOfProjections;
                    numberOfProjections = 5;
                    % A matrices
                    global As;
                    % b matrices
                    global bs;
                    % number of rays
                    numberOfRays = 32;
                    % runtimes
                    runtimes = zeros(size(imageList,2),1);
                    % save directory
                    saveDir = './results-ga/';
                    % errors
                    errors = zeros(size(imageList,2),1);

                    for i = 1:size(imageList,2)
                        imgName = char(imageList(i));
                        imgPath = ['dataset/', imgName];
                        img512 = imread(imgPath);
                        img64 = img512(1:8:end,1:8:end);
                        %img32 = img512(1:16:end,1:16:end);
                        %img16 = img512(1:32:end,1:32:end);
                        %img8 = img512(1:64:end,1:64:end);
                        
                        % use this subsampled image
                        img = img64;
                        
                        % angles to project (in radians)
                        angles = linspace(0,pi,numberOfProjections);
                        
                        % center in pixel coordinates
                        center = [size(img,1)/2; size(img,1)/2];
                        
                        As = zeros([numberOfRays*2+1 size(img,1).^2 numberOfProjections]);

                        bs = zeros([numberOfRays*2+1 1 numberOfProjections]);
                        
                        for j = 1:numberOfProjections
                            angle = angles(j);
                            [matrixA, matrixb]=projectionmatrix(img, angle, center, numberOfRays);
                            As(:,:,j) = matrixA;
                            bs(:,:,j) = matrixb;
                        end
                        
                        disp("setup done.");
                        
                        R = zeros(size(bs,1),size(bs,3));

                        for j = 1:size(bs,3)
                            R(:,j) = bs(:,1,j); 
                        end

                        imgFbp = iradon(R,angles/pi*180,"nearest","Ram-Lak",1,size(img,1));

                        u0 = rescale(getVecFromImage(imgFbp));
                        
                        disp("initial vector found.");
                        
                        numberOfVariables = size(img,1)*size(img,2);
                        lb = zeros(numberOfVariables,1);
                        ub = ones(numberOfVariables,1);
                        
                        %options = optimoptions('ga',"Display","iter",'InitialPopulationMatrix',u0',
                        'FunctionTolerance',1e-6,'PopulationSize',size(img,1)^2,'HybridFcn','fmincon','FitnessLimit',80,'PlotFcn','gaplotbestf');
                        options = optimoptions('fmincon','Algorithm','interior-point','SpecifyObjectiveGradient',true,"Display","iter",'PlotFcn','optimplotfval');

                        tic;
                        %imgOpt = iradon(R,angles/pi*180,"nearest","Ram-Lak",1,size(img,1));
                        %uOpt = rescale(getVecFromImage(imgOpt));
                        %[uOpt, finalCost, flag, status, population, scores] = ga(@costFunc,numberOfVariables,[],[],[],[],lb,ub,[],options);
                        [uOpt,finalCost,exitflag,status,lambda,grad,hessian] = fmincon(@objecitveFunc,u0,[],[],[],[],lb,ub,[],options);
                        dt = toc;
                        runtimes(i) = dt;
                        
                        imgOpt = getImageFromVec(uOpt);
                        imgOptBin = imgOpt>0.5;

                        savePath = [saveDir, imgName];
                        imwrite(imgOptBin,savePath);
                        
                        error = sum(sum(abs(imgOptBin-img)));
                        errors(i) = error;
                        
                        figure;
                        imgBig = imresize(img, 512/size(img,1));
                        imshow(imgBig);

                        figure;
                        imgOptBig = imresize(imgOptBin, 512/size(img,1));
                        imshow(imgOptBig);
                        
                        pause;
                    end

                    
                  
Home