
clear all;
clc;
obj = mmreader ('F:\ALIREZA\IYPT\Levitating Spinner\Movie_using\Omega Z\levitsmaller.avi');

n = obj.NumberOfFrames;
h = obj.Height;
w = obj.Width;
mov(n).cdata = 0;
%n=10;
l = 0;
collision = [0
    350
    425
    491
    540
    586
    626
    660  ]
for i = 1:n
    i
    mov(i).cdata = read(obj, i);
end 
    
start(2:8)= 0;
strat(1) =1; 
for i=2:n
    i
    frame = mov(i).cdata;
    I2 = rgb2gray (frame);
    g = graythresh (I2);
    I3 = im2bw (I2, g*1.2);
    I4 = bwareaopen(I3, 50);
    [h w  pp] = size (frame);
    c(i) = bwconncomp(I4);
    bw = bwboundaries(I4);
    nn = c(i).NumObjects;
    grain(1:h, 1:w) = 0;
    movv = repmat(uint8(I4).*255, [1 1 3]);
    movie(i) = immovie(movv);
    teta(i,2:nn) = 3.1416/2;

    
    for p=1:nn
        [y x] =  (ind2sub([h w], c(i).PixelIdxList{p}));
        for ss=1:length(x)
            X(1,ss) = x(ss,1);
            Y(1,ss) = y(ss,1);
        end
        X = X(1:ss); Y=Y(1:ss);
        [r m b] = regression(X, Y);
        if i>collision(p)
            teta(i, p) = atan(-m);
        else
            teta(i,p) = 3.1416/2;
        end
          
    end
    
  
end


 movie2avi (movie, 'F:\ALIREZA\IYPT\Domino\Video Processing\testprocess.avi', 'FPS', 10, 'compression', 'None', 'quality', 100);



