Function [motionvector, Count] = hexagonsearch (cur, refer, n)
Window = 7;
[H, w] = size (cur );
BH = H/N;
BW = W/N;
Motionvector = cell (BH, BW );
Count = zeros (BH, BW );
For M = 1: BH
For n = 1: bw
M, n
OI = (m-1) * n + 1; % starting coordinate of the block
OJ = (n-1) * n + 1;
Lower = max (Oi-window, 1 );
Upper = min (OI + window, H-(N-1 ));
Left = max (OJ-window, 1 );
Right = min (OJ + window, W-(N-1 ));
P = oi;
Q = OJ;
I = 1;
% 1 :( P-2, q-1)
If P-2> = Lower & q-1> = left
Value (I, :) = [sad (OI, OJ, P-2, Q-1, cur, refer, n), 1];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
% 2 :( P-2, q + 1)
If P-2> = Lower & q + 1 <= right
Value (I, :) = [sad (OI, OJ, P-2, q + 1, cur, refer, n), 2];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
% 3 :( P, q-2)
If q-2> = left
Value (I, :) = [sad (OI, OJ, P, q-2, cur, refer, n), 3];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
% 4 :( p, q)
Value (I, :) = [sad (OI, OJ, p, q, cur, refer, n), 4];
I = I + 1;
Count (m, n) = count (m, n) + 1;
% 5: (p, q + 2)
If q + 2 <= right
Value (I, :) = [sad (OI, OJ, p, q + 2, cur, refer, n), 5];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
% 6 :( P + 2, q-1)
If P + 2 <= Upper & q-1> = left
Value (I, :) = [sad (OI, OJ, P + 2, Q-1, cur, refer, n), 6];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
% 7: (p + 2, q + 1)
If P + 2 <= Upper & q + 1 <= right
Value (I, :) = [sad (OI, OJ, P + 2, q + 1, cur, refer, n), 7];
I = I + 1;
Count (m, n) = count (m, n) + 1;
End
[Y, Index] = min (value (:, 1 ));
While value (index, 2 )~ = 4
Switch value (index, 2)
Case 1
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
P = P-2;
Q = q-1;
% 1 :( P-2, q-1)
If P-2> = Lower & q-1> = left
Value (J, :) = [sad (OI, OJ, P-2, Q-1, cur, refer, n), 1];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 2 :( P-2, q + 1)
If P-2> = Lower & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P-2, q + 1, cur, refer, n), 2];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 3 :( P, q-2)
If q-2> = left
Value (J, :) = [sad (OI, OJ, P, q-2, cur, refer, n), 3];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Case 2
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
P = P-2;
Q = q + 1;
% 1 :( P-2, q-1)
If P-2> = Lower & q-1> = left
Value (J, :) = [sad (OI, OJ, P-2, Q-1, cur, refer, n), 1];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 2 :( P-2, q + 1)
If P-2> = Lower & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P-2, q + 1, cur, refer, n), 2];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 5: (p, q + 2)
If q + 2 <= right
Value (J, :) = [sad (OI, OJ, p, q + 2, cur, refer, n), 5];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Case 3
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
Q = q-2;
% 1 :( P-2, q-1)
If P-2> = Lower & q-1> = left
Value (J, :) = [sad (OI, OJ, P-2, Q-1, cur, refer, n), 1];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 3 :( P, q-2)
If q-2> = left
Value (J, :) = [sad (OI, OJ, P, q-2, cur, refer, n), 3];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 6 :( P + 2, q-1)
If P + 2 <= Upper & q-1> = left
Value (J, :) = [sad (OI, OJ, P + 2, Q-1, cur, refer, n), 6];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Case 5
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
Q = q + 2;
% 2 :( P-2, q + 1)
If P-2> = Lower & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P-2, q + 1, cur, refer, n), 2];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 5: (p, q + 2)
If q + 2 <= right
Value (J, :) = [sad (OI, OJ, p, q + 2, cur, refer, n), 5];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 7: (p + 2, q + 1)
If P + 2 <= Upper & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P + 2, q + 1, cur, refer, n), 7];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Case 6
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
P = P + 2;
Q = q-1;
% 3 :( P, q-2)
If q-2> = left
Value (J, :) = [sad (OI, OJ, P, q-2, cur, refer, n), 3];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 6 :( P + 2, q-1)
If P + 2 <= Upper & q-1> = left
Value (J, :) = [sad (OI, OJ, P + 2, Q-1, cur, refer, n), 6];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 7: (p + 2, q + 1)
If P + 2 <= Upper & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P + 2, q + 1, cur, refer, n), 7];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Case 7
Clear value;
J = 1;
Value (J, :) = [y, 4];
J = J + 1;
P = P + 2;
Q = q + 1;
% 5: (p, q + 2)
If q + 2 <= right
Value (J, :) = [sad (OI, OJ, p, q + 2, cur, refer, n), 5];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 6 :( P + 2, q-1)
If P + 2 <= Upper & q-1> = left
Value (J, :) = [sad (OI, OJ, P + 2, Q-1, cur, refer, n), 6];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
% 7: (p + 2, q + 1)
If P + 2 <= Upper & q + 1 <= right
Value (J, :) = [sad (OI, OJ, P + 2, q + 1, cur, refer, n), 7];
J = J + 1;
Count (m, n) = count (m, n) + 1;
End
Otherwise
End
[Y, Index] = min (value (:, 1 ));
End
K = 1;
If P-1> = lower
Final (K, :) = [sad (OI, OJ, P-1, Q, cur, refer, n), 1];
K = k + 1;
Count (m, n) = count (m, n) + 1;
End
If q-1> = left
Final (K, :) = [sad (OI, OJ, P, Q-1, cur, refer, n), 2];
K = k + 1;
Count (m, n) = count (m, n) + 1;
End
If q + 1 <= right
Final (K, :) = [sad (OI, OJ, p, q + 1, cur, refer, n), 4];
K = k + 1;
Count (m, n) = count (m, n) + 1;
End
If p + 1 <= upper
Final (K, :) = [sad (OI, OJ, p + 1, Q, cur, refer, n), 5];
K = k + 1;
Count (m, n) = count (m, n) + 1;
End
Final (K, :) = [y, 3];
[Y, Index] = min (final (:, 1 ));
Switch final (index, 2)
Case 1
Vi = p-1-oi;
Vj = Q-OJ;
Case 2
Vi = p-Oi;
Vj = q-1-oj;
Case 3
Vi = p-Oi;
Vj = Q-OJ;
Case 4
Vi = p-Oi;
Vj = q + 1-oj;
Case 5
Vi = P + 1-oi;
Vj = Q-OJ;
Otherwise
End
Clear final
Motionvector {m, n} = [vi, VJ];
Clear Value
End
End