-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVideoSumFCM.m
131 lines (115 loc) · 2.95 KB
/
VideoSumFCM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
global d numFramesWritten11 FileName framerate1;
n=numFramesWritten11-1;
fd=FrameDef.empty;
dataset=zeros(n,1);
for i=1:n
%accessing first frames of each segment
name=strcat(num2str(i),'.png');
name=strcat('C:\VideoSumm\snaps\',name);
im1=imread(name);
%histograms
im1=rgb2gray(im1);
h=imhist(im1);
value=0;
for t=1:256
value=value+t*h(t);
end;
dataset(i)=value;
fd(i)=FrameDef(name,value,0);
end;
d.setValue(0.4);
opts = [nan;nan;nan;0];
[center,U,obj_fcn] = fcm(dataset,k,opts);
idx=zeros(n,1);
for i=1:n
[a,idx(i)]=max(U(:,i));
end
clust=zeros(n,k);
final=zeros(l,1);
for j=1:k
for o=1:n
if idx(o)==j
clust(o,j)=dataset(o,1);
end
end
end
d.setValue(0.5);
rr=1;
ss=1;
clust=sort(clust,'descend');
temp=1;
while true
if(ss>l)
break;
end
if (clust(temp,rr)~=0)
pos=checkValue(clust(temp,rr),n,fd); %find the value in object array
if(fd(pos).flag==0) %flag to avoid repeatability of frames
final(ss)=clust(temp,rr);
fd(pos).flag=1;
rr=mod(rr,k)+1;
ss=ss+1;
temp=1;
else
temp=temp+1;
end;
else
temp=temp+1;
continue;
end
end
finalpos=zeros(1,l);
for i=1:l
finalpos(i)=checkValue(final(i),n,fd);
end
sortedpos=sort(finalpos);
d.setValue(0.6);
opFolder = 'C:\VideoSumm\snaps2';
%if not existing
if ~exist(opFolder, 'dir')
%make directory & execute as indicated in opfolder variable
mkdir(opFolder);
end
ttt=1;
for i=1:l
count=((sortedpos(i)-1)*k*framerate1)+1;
count2=(count+k*framerate1);
for m = count:count2
%numFramesWritten11 = numFramesWritten11 + 1;
currFrame = read(mov, m); %reading individual frames
opBaseFileName = sprintf('%d.png', ttt);
opFullFileName = fullfile(opFolder, opBaseFileName);
imwrite(currFrame, opFullFileName, 'png'); %saving as 'png' file
ttt=ttt+1;
end
end
d.setValue(0.9);
%storing snaps2 folder images in imagesNames to create a video
workingDir = 'C:\VideoSumm';
imageNames = dir(fullfile(workingDir,'snaps2','*.png'));
imageNames = {imageNames.name};
%sorting the imagesNames according to numeric names of images
for i = 1:length(imageNames)
out(i) = cellfun(@(x)str2double(regexp(x,'\d*\.\d*','match')),imageNames(i));
end
out=sort(out);
for i = 1:length(imageNames)
imageNames(i)=cellstr(strcat(num2str(out(i)),'.png'));
end
%assign name to output file
FileName=strcat(FileName,'_summarize.avi');
outputVideo = VideoWriter(fullfile(workingDir,FileName));
outputVideo.FrameRate = framerate1;
open(outputVideo)
for ii = 1:length(imageNames)
img = imread(fullfile(workingDir,'snaps2',imageNames{ii}));
writeVideo(outputVideo,img)
end
d.setValue(1);
close(outputVideo)
d.dispose();
msgbox('Processing Finished');
%remove directories
rmdir('C:\VideoSumm\snaps','s');
rmdir('C:\VideoSumm\snaps2','s');
analysisOfProcess;