-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreateAdjacencyMatrix.m
135 lines (124 loc) · 5.78 KB
/
createAdjacencyMatrix.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
function [ adjMatrix ] = createAdjacencyMatrix(img, match, context, mask, x_t, y_t)
s = size(context);
w = s(1);
h = s(2);
% context = context - (mask-1).^2;
totalPoints=0;
tX=0;
tY=0;
contextMap = zeros(w,h);
count = 1;
for i = 1:w
for j = 1:h
if context(i,j) == 1
contextMap(i,j) = count;
count = count + 1;
end
if mask(i,j) == 0
totalPoints = totalPoints + 1;
tX = tX + i;
tY = tY + j;
end
end
end
iMat = zeros(count,1);
jMat = zeros(count,1);
sMat = zeros(count,1);
centerOfGravity = [tX/totalPoints, tY/totalPoints];
DISTANCE_FACTOR = .3;
% adjMatrix = zeros(count+1,count+1);
originalImage = count;
hole = count+1;
cform = makecform('srgb2lab');
lab1 = double(applycform(img,cform));
lab2 = double(applycform(match,cform));
count = 0;
for i = 1:w
for j = 1:h
if context(i,j) == 1
count = count+1;
% test pixel to the left
if i > 1 && context(i-1,j) == 1
% create edge p->q
iMat(count) = contextMap(i,j);
jMat(count) = contextMap(i-1,j);
sMat(count) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i-1,j, lab1, lab2, x_t, y_t);
% adjMatrix(contextMap(i,j),contextMap(i-1,j)) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i-1,j, lab1, lab2, x_t, y_t);
elseif i > 1 && mask(i-1,j) == 0
% create edge p->hole
iMat(count) = contextMap(i,j);
jMat(count) = hole;
sMat(count) = Inf;
% adjMatrix(contextMap(i,j),hole) = Inf;
else
% create edge orig->p
iMat(count) = originalImage;
jMat(count) = contextMap(i,j);
sMat(count) = Inf;
% adjMatrix(originalImage, contextMap(i,j)) = Inf;
end
% test pixel above
if j > 1 && context(i, j-1) == 1
% create edge p->q
iMat(count) = contextMap(i,j);
jMat(count) = contextMap(i,j-1);
sMat(count) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i,j-1, lab1, lab2, x_t, y_t);
% adjMatrix(contextMap(i,j),contextMap(i,j-1)) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i,j-1, lab1, lab2, x_t, y_t);
elseif j > 1 && mask(i,j-1) == 0
% create edge p->hole
iMat(count) = contextMap(i,j);
jMat(count) = hole;
sMat(count) = Inf;
% adjMatrix(contextMap(i,j),hole) = Inf;
else
% create edge orig->p
iMat(count) = originalImage;
jMat(count) = contextMap(i,j);
sMat(count) = Inf;
% adjMatrix(originalImage, contextMap(i,j)) = Inf;
end
% test pixel to the right
if i < w && context(i+1, j) == 1
% create edge p->q
iMat(count) = contextMap(i,j);
jMat(count) = contextMap(i+1,j);
sMat(count) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i+1,j, lab1, lab2, x_t, y_t);
% adjMatrix(contextMap(i,j),contextMap(i+1,j)) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i+1,j, lab1, lab2, x_t, y_t);
elseif i < w && mask(i+1,j) == 0
% create edge p->hole
iMat(count) = contextMap(i,j);
jMat(count) = hole;
sMat(count) = Inf;
% adjMatrix(contextMap(i,j),hole) = Inf;
else
% create edge orig->p
iMat(count) = originalImage;
jMat(count) = contextMap(i,j);
sMat(count) = Inf;
% adjMatrix(originalImage, contextMap(i,j)) = Inf;
end
% test pixel below
if j < h && context(i, j+1) == 1
% create edge p->q
iMat(count) = contextMap(i,j);
jMat(count) = contextMap(i,j+1);
sMat(count) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i,j+1, lab1, lab2, x_t, y_t);
% adjMatrix(contextMap(i,j),contextMap(i,j+1)) = DISATANCE_FACTOR*(sqrt((i-centerOfGravity(1))^2 + (j-centerOfGravity(2))^2)) + getGradientMagnitude(i,j,i,j+1, lab1, lab2, x_t, y_t);
elseif j < h && mask(i,j+1) == 0
% create edge p->hole
iMat(count) = contextMap(i,j);
jMat(count) = hole;
sMat(count) = Inf;
% adjMatrix(contextMap(i,j),hole) = Inf;
else
% create edge orig->p
iMat(count) = originalImage;
jMat(count) = contextMap(i,j);
sMat(count) = Inf;
% adjMatrix(originalImage, contextMap(i,j)) = Inf;
end
end
end
end
adjMatrix = sparse(iMat,jMat,sMat);
end