-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesorting.m
140 lines (134 loc) · 3.16 KB
/
mergesorting.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
132
133
134
135
136
137
138
139
%% sort inside the cell
function newcell=mergesorting(cell)
for i=1:size(cell,2)
newcell{i}=reorder(cell,i);
end
end
function array=reorder(cell,n)
%%
label=[];
bcasttotal=[];
count=0;
for i=1:size(cell{n},2)
filename=sprintf('..\\%d.jpg',cell{n}(i));
patch=imread(filename);
graypatch=rgb2gray(patch);
graypatch=double(graypatch);
total = graypatch(400:435,50:120);
%total = imresize(graypatch,[60,80]);
count=count+1;
bcasttotal(:,count)=reshape(total, size(total,1)*size(total,2),1);
label(count)=cell{n}(i);
end
% we choose corrcoef coefficient as the criteria of similarity
% covu3=corrcoef(bcastu3);
% covtotal=corrcoef(bcasttotal);
% cov2=corrcoef(sort);
% cov=covtotal+cov2;
% cellar=greedy(cov,cov2,label,id,count);
%here we choose clockcity as the criteria of simialarity
bcasttotal=bcasttotal./255;
covtotal=zeros(size(bcasttotal,2),size(bcasttotal,2));
for i=1:size(bcasttotal,2)
for j=1:i
covtotal(i,j)=norm(bcasttotal(:,i)-bcasttotal(:,j),1)/size(bcasttotal,1);
covtotal(j,i)=covtotal(i,j);
end
end
cov=covtotal;
link(1,1)=1;
link(1,2)=1;
for i=1:size(cov,2)-1
t1=cov(:,link(i,1));
t2=cov(:,link(i,2));
min1=inf;
min2=inf;
link1=1;
link2=1;
for j=1:size(cov,2)
if i==1
if t2(j)<min2 && j~=1
link2=j;
min2=t2(j);
end
else
if t2(j)<min2 && j~=link(i,2)
link2=j;
min2=t2(j);
end
%link(i,:) means at this moument the head is 1 and end is 2 then we should consider cov(link(i,1))
%and cov(link(i,2)) and find the best
if t1(j)<min1 && j~=link(i,1)
link1=j;
min1=t1(j);
end
end
end
if min2 <= min1
link(i+1,2)=link2;
link(i+1,1)=link(i,1);
end
if min1 < min2
link(i+1,1)=link1;
link(i+1,2)=link(i,2);
end
cov(link(i+1,1),:)=1;
cov(link(i+1,2),:)=1;
end
array=[];
frame=1;
for i=size(link,1):-1:1
if (frame>1)
if link(i,1)~=array(frame-1)
array(frame)=link(i,1);
frame=frame+1;
end
else
array(frame)=link(i,1);
frame=frame+1;
end
end
for i=1:size(link,1)
if link(i,2)~=array(frame-1)
array(frame)=link(i,2);
frame=frame+1;
end
end
mincost=0;
cost=0;
for i=1:size(array,2)-1
mincost=mincost+covtotal(array(i),array(i+1));
end
tarray=array;
% front search
for i=3:size(link,1)
newarray=[tarray(2:i-1),tarray(1),tarray(i:end)];
cost=0;
for j=1:size(newarray,2)-1
cost=cost+covtotal(newarray(j),newarray(j+1));
end
if cost < mincost
mincost=cost;
array=newarray;
end
end
tarray=array;
mincost=0;
cost=0;
for i=1:size(array,2)-1
mincost=mincost+covtotal(array(i),array(i+1));
end
% back search
for i=size(link,1)-1:-1:2
newarray=[tarray(1:i-1),tarray(end),tarray(i:end-1)];
cost=0;
for j=1:size(newarray,2)-1
cost=cost+covtotal(newarray(j),newarray(j+1));
end
if cost < mincost
mincost=cost;
array=newarray;
end
end
array=label(array);
end