-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetAdjacencyMatrix.m
More file actions
93 lines (71 loc) · 2.56 KB
/
Copy pathgetAdjacencyMatrix.m
File metadata and controls
93 lines (71 loc) · 2.56 KB
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
function [adjMatrixW, adjMatrixMW, adjMAsub, adjMBsub, adjMW, adjMmW, img] = getAdjacencyMatrix(inputImg)
szImg = size(inputImg);
img = zeros([szImg(1) szImg(2)+2]);
img(:,2:1+szImg(2)) = inputImg;
% update size of image
szImg = size(img);
% get vertical gradient image
[~,gradImg] = gradient(img,1,1);
gradImg = -1*gradImg;
% normalize gradient
gradImg = (gradImg-min(gradImg(:)))/(max(gradImg(:))-min(gradImg(:)));
% get the "invert" of the gradient image.
gradImgMinus = gradImg*-1+1;
%% generate adjacency matrix, see equation 1 in the refered article.
%minimum weight
minWeight = 1E-5;
neighborIterX = [1 1 1 0 0 -1 -1 -1];
neighborIterY = [1 0 -1 1 -1 1 0 -1];
% get location A (in the image as indices) for each weight.
adjMAsub = 1:szImg(1)*szImg(2);
% convert adjMA to subscripts
[adjMAx,adjMAy] = ind2sub(szImg,adjMAsub);
adjMAsub = adjMAsub';
szadjMAsub = size(adjMAsub);
% prepare to obtain the 8-connected neighbors of adjMAsub
% repmat to [1,8]
neighborIterX = repmat(neighborIterX, [szadjMAsub(1),1]);
neighborIterY = repmat(neighborIterY, [szadjMAsub(1),1]);
% repmat to [8,1]
adjMAsub = repmat(adjMAsub,[1 8]);
adjMAx = repmat(adjMAx, [1 8]);
adjMAy = repmat(adjMAy, [1 8]);
% get 8-connected neighbors of adjMAsub
% adjMBx,adjMBy and adjMBsub
adjMBx = adjMAx+neighborIterX(:)';
adjMBy = adjMAy+neighborIterY(:)';
% make sure all locations are within the image.
keepInd = adjMBx > 0 & adjMBx <= szImg(1) & ...
adjMBy > 0 & adjMBy <= szImg(2);
% adjMAx = adjMAx(keepInd);
% adjMAy = adjMAy(keepInd);
adjMAsub = adjMAsub(keepInd);
adjMBx = adjMBx(keepInd);
adjMBy = adjMBy(keepInd);
adjMBsub = sub2ind(szImg,adjMBx(:),adjMBy(:))';
% calculate weight
adjMW = 2 - gradImg(adjMAsub(:)) - gradImg(adjMBsub(:)) + minWeight;
adjMmW = 2 - gradImgMinus(adjMAsub(:)) - gradImgMinus(adjMBsub(:)) + minWeight;
% pad minWeight on the side
imgTmp = nan(size(gradImg));
imgTmp(:,1) = 1;
imgTmp(:,end) = 1;
imageSideInd = ismember(adjMBsub,find(imgTmp(:)==1));
adjMW(imageSideInd) = minWeight;
adjMmW(imageSideInd) = minWeight;
% build sparse matrices
adjMatrixW = [];%sparse(adjMAsub(:),adjMBsub(:),adjMW(:),numel(img(:)),numel(img(:)));
% build sparse matrices with inverted gradient.
adjMatrixMW = [];%sparse(adjMAsub(:),adjMBsub(:),adjMmW(:),numel(img(:)),numel(img(:)));
% %% http://tipstrickshowtos.blogspot.com/2010/02/fast-replacement-for-sub2ind.html
% function [r,c] = ind2subSimple(sz, idx)
%
% nrows = sz(1);
% r = rem(idx-1,nrows)+1;
% c = (idx-r)./nrows + 1;
% r = r';
% c = c';
%
% function idx = sub2indSimple( sz, rows, cols)
% nrows = sz(1);
% idx = rows + (cols-1)*nrows;