function [unfolded_im_cell, im_total] = Unfold_by_surf(Img_in, Traj_in, w)
%Unfold_by_surf Unfolds the image by taking the projection of the image
%with width of width_in pixels along the trajectory of Traj_in

unfolded_im_cell = cell(1);
for i=1:(size(Traj_in,1)-1),
   pix_1 = [floor(Traj_in(i, 1)), floor(Traj_in(i, 2))];
   pix_2 = [floor(Traj_in(i+1, 1)), floor(Traj_in(i+1, 2))];
   
   d1 = pix_1 + w/sqrt(sum((pix_1 - pix_2).^2))*[-(pix_2(2) - pix_1(2)), pix_2(1) - pix_1(1)];
   d2 = pix_2 + w/sqrt(sum((pix_1 - pix_2).^2))*[-(pix_2(2) - pix_1(2)), pix_2(1) - pix_1(1)];
   d3 = pix_2 + w/sqrt(sum((pix_1 - pix_2).^2))*[(pix_2(2) - pix_1(2)), -(pix_2(1) - pix_1(1))];
   d4 = pix_1 + w/sqrt(sum((pix_1 - pix_2).^2))*[(pix_2(2) - pix_1(2)), -(pix_2(1) - pix_1(1))];
   
   cut_x = [floor(min([d1(1), d2(1), d3(1), d4(1), d1(1)])), ceil(max([d1(1), d2(1), d3(1), d4(1), d1(1)]))];
   cut_y = [floor(min([d1(2), d2(2), d3(2), d4(2), d1(2)])), ceil(max([d1(2), d2(2), d3(2), d4(2), d1(2)]))];
   
   Img_mask = zeros(size(Img_in));
   
   Img_mask(round(d1(2)), round(d1(1))) = 1;
   Img_mask(round(d2(2)), round(d2(1))) = 1;
   Img_mask(round(d3(2)), round(d3(1))) = 1;
   Img_mask(round(d4(2)), round(d4(1))) = 1;
   
   im_cut = Img_in(cut_y(1):cut_y(2),cut_x(1):cut_x(2));
   im_mask_cut = Img_mask(cut_y(1):cut_y(2),cut_x(1):cut_x(2));
   
   th = atan2(pix_2(2) - pix_1(2), pix_2(1) - pix_1(1));
   
   im_cut_rot = imrotate(im_cut, th*180/pi);
   im_mask_cut_rot = imrotate(im_mask_cut, th*180/pi);
   
   [idx, idy] = find(im_mask_cut_rot);
   
   unfolded_im_cell{i} = im_cut_rot(min(idx):max(idx), min(idy):max(idy));
   
   
end


w_max = 1;len_v = [];
for i=1:length(unfolded_im_cell),
    if w_max < size(unfolded_im_cell{i}, 1),
        w_max = size(unfolded_im_cell{i}, 1);
    end
    len_v = [len_v, size(unfolded_im_cell{i}, 2)];
end

im_total = zeros(w_max, sum(len_v));

im_total(1:size(unfolded_im_cell{1}, 1), 1:len_v(1)) = unfolded_im_cell{1};
for i=2:length(unfolded_im_cell),
   im_total(1:size(unfolded_im_cell{i}, 1), (sum(len_v(1:(i-1)))+1):sum(len_v(1:i))) = unfolded_im_cell{i};
end



end

