clear
close all
dataDir = '/Users/marianaveloso/Desktop/MATLAB';
cd(dataDir)
video = VideoReader('P1070143_15.MOV');
get(video);
nFrames = video.NumFrames;
vidHeight = video.Height;
vidWidth = video.Width;
cgFrames=video.FrameRate;
n0= 3900
nf= 12610
Area = read (video, 1);
figure;
imshow(Area, []);
set(gcf, 'Position', get(0,'Screensize'));
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = drawfreehand();
binaryImage = hFH.createMask();
Background = read (video, 1);
h = fspecial('gaussian',7,3);
background_gray=rgb2gray(Background);
background_gray= imadjust (background_gray);
background_gray=imcomplement(background_gray);
background_bw=imbinarize(background_gray,0.36);
mat=zeros(size(background_bw));
n=1;
for i= n0:nf
test=read(video,i);
test_gray=rgb2gray(test);
test_gray= imadjust (test_gray);
test_gray=imcomplement(test_gray);
test_bw=imbinarize(test_gray,0.39);
test_dif=test_bw-background_bw;
test_dif(binaryImage<1)=0;
test_dif2=medfilt2(test_dif,[3 3]);
test_dif2(test_dif2<0)=0;
cc = bwconncomp(test_dif2);
stats = regionprops(cc, 'Area','centroid');
idx = find([stats.Area]==max([stats.Area]));
test_dif4= ismember(labelmatrix(cc), idx);
a = regionprops(test_dif4,'centroid','area');
co(n,:)=a(1,1).Centroid;
else
co(n,:)=[0 0];
end
areas=[];
hold on
for i=1:length(a)
plot(co(n,1),co(n,2),'ro');
end
hold off
mat=mat+test_dif4;
n=n+1
end