clear Scan; close all;
 
clear all
clc
 
arduino=serial('COM11','BaudRate',9600);
 
fopen(arduino);
 
pause(2);
 
% Connect to the webcam.
cam = webcam(1);
 
% Preview Video Stream
preview(cam);
 
% Acquire a Frame
img = snapshot(cam);
 
for i = 1:200
    %img(:,:,:,i) = snapshot(cam);
    ScanTemp = snapshot(cam);
    Scan(:,:,i) = imcomplement(im2single(ScanTemp(:,:,1)));
    fprintf(arduino,'A');
    pause(0.1);
    %filename = ['Scan', num2str(i-1),'.tiff'];
    %imwrite(img(:,:,:,i),filename)
end
 
clear cam;
 
fclose(arduino);
 
% for i=0:199
% filename = ['Scan', num2str(i),'.tiff'];
% ScanTemp = imread(filename, 'tif');
% Scan(:,:,i+1) = imcomplement(im2single(ScanTemp(:,:,1)));
% end
 
Scan = rot90(Scan, 1);
 
addpath('C:\Users\Deanasaur\Documents\MATLAB\Add-Ons\Toolboxes\3D Cone beam CT (CBCT) projection backprojection FDK, iterative reconstruction Matlab examples\code\bin');
 
%% Parameter setting %%
 
% % % % % % Confirm your parameters % % % % % % %
 
param.nx = 256; % number of voxels
param.ny = 256;
param.nz = 256;
 
param.sx = 5; % mm (real size)
param.sy = 5; % mm
param.sz = 5; % mm
 
%The real detector panel pixel density (number of pixels)
param.nu = 640;     % number of pixels
param.nv = 480;
 
% Detector setting (real size)
param.su = 640; % mm (real size)
param.sv = 480;     % mm
 
% X-ray source and detector setting
param.DSD = 10000;    %  Distance source to detector 
param.DSO = 100;    %  X-ray source to object axis distance
 
% angle setting
param.dir = 1;   % gantry rotating direction (clock wise/ counter clockwise)
param.dang = 1.8; % angular step size (deg)
param.deg = 0:param.dang:360; % total degrees covered
param.deg = param.deg*param.dir;
param.nProj = length(param.deg)-1;
 
param.parker = 0; % data with 360 deg -> param.parker = 0 , data less than 360 deg -> param.parker=1 
 
% % % % % % Confirm your parameters % % % % % % %
 
% filter='ram-lak','cosine', 'hamming', 'hann' 
param.filter='ram-lak'; % high pass filter
 
param.dx = param.sx/param.nx; % single voxel size
param.dy = param.sy/param.ny;
param.dz = param.sz/param.nz;
param.du = param.su/param.nu;
param.dv = param.sv/param.nv;
 
param.off_u = 0; param.off_v = 0; % detector rotation shift (real size)
 
% % % Geometry calculation % % %
param.xs = [-(param.nx-1)/2:1:(param.nx-1)/2]*param.dx;
param.ys = [-(param.ny-1)/2:1:(param.ny-1)/2]*param.dy;
param.zs = [-(param.nz-1)/2:1:(param.nz-1)/2]*param.dz;
 
param.us = (-(param.nu-1)/2:1:(param.nu-1)/2)*param.du + param.off_u;
param.vs = (-(param.nv-1)/2:1:(param.nv-1)/2)*param.dv + param.off_v;
 
param.interptype = 'linear'; % 'linear', 'nearest'
 
% % % % % % Confirm your parameters % % % % % % %
% Only for Matlab version above 2013b with parallel computing toolbox: Speed dependent on your GPU
% You don't need to install CUDA, but install latest graphics driver.
% only Nvidia GPU cards can use this. otherwise please "param.gpu=0"
% This option is semi-GPU code using built-in Matlab GPU functions: several times faster than CPU
param.gpu = 1;
 
%% Recon case 1 - Analytic reconstruction: filtered backprojection
% filter='ram-lak','shepp-logan','cosine', 'hamming', 'hann' : (ramp + additional filter)
param.filter='hann'; 
 
proj_filtered = filtering(Scan,param);
Reconimg = CTbackprojection(proj_filtered, param);
 
% for i=1:param.nz
%     figure(2); imagesc(max(Reconimg(:,:,i),0)); axis off; axis equal; colormap gray; colorbar;
%     title(num2str(i));
%     pause(0.01);
% end
 
viewer3d(Reconimg);