-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetW2.asv
62 lines (51 loc) · 2 KB
/
getW2.asv
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
% this function generates the weight matrix
% the input is: image
% the output is weight matrix W
function W = getW2(image)
r = 1;
[height, width] = size(image);
mask = ones(1,height*width);
OnesinMask = length(find(mask == 1));
Image2 = zeros(height+2*r, width+2*r);
Image2(r+1:height+r,r+1:width+r) = image;
ImageAfterTravel = im2col(Image2,[2*r+1 2*r+1],'sliding');
CentralPoints = ImageAfterTravel(round((2*r+1)^2/2),:);
CentralPointsRep = repmat(CentralPoints,(2*r+1)^2,1);
% calculate the part1 of the equation(11)
ValueResult = abs(ImageAfterTravel-CentralPointsRep);
% calculate the coordinates
X = repmat([1:2*r+1]',1,2*r+1);
Y = repmat([1:2*r+1],2*r+1,1);
% calculate the part2 of the equation(11)
distance = floor(sqrt((X-(r+1)).^2+(Y-(r+1)).^2));
part2 = distance/1);
part2Vector = reshape(part2,[],1);
part2Matrix = repmat(part2Vector,1,height*width);
% multiply part1 and part2 into the whole result
ValueResult = ValueResult.*part2Matrix;
% deal with the index
% index = [1:height*width];
index = zeros(1,height*width);
index(mask==1) = [1:OnesinMask];
indexMatrix = reshape(index,height,width);
indexMatrix2 = zeros(height+2*r, width+2*r);
indexMatrix2(r+1:height+r,r+1:width+r) = indexMatrix;
IndexAfterTravel = im2col(indexMatrix2,[2*r+1 2*r+1],'sliding');
CentralIndex = IndexAfterTravel(round((2*r+1)^2/2),:);
CentralIndexRep = repmat(CentralIndex,(2*r+1)^2,1);
% adding mask
IndexAfterTravel = IndexAfterTravel(:,mask == 1);
CentralIndexRep = CentralIndexRep(:,mask == 1);
ValueResult = ValueResult(:,mask == 1);
% X index
IndexAfterTravelVector = reshape(IndexAfterTravel,[],1);
% IndexAfterTravelVector(IndexAfterTravelVector==0) = (height*width)^2+1;
% Y index
CentralIndexRepVector = reshape(CentralIndexRep,[],1);
ValueResultVector = reshape(ValueResult,[],1);
%remove the zero coordinates
CentralIndexRepVector(IndexAfterTravelVector==0) = 1;
IndexAfterTravelVector(IndexAfterTravelVector==0) = 1;
W = sparse(IndexAfterTravelVector,CentralIndexRepVector,ValueResultVector,OnesinMask,OnesinMask);
W(1,1) = 1;
end