-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtexture_flattening.m
91 lines (79 loc) · 2.72 KB
/
texture_flattening.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
border_img = imread('boy.jpg');
figure,imshow(border_img);
h = imfreehand;
position = wait(h);
mask = double(createMask(h));
gray_img = rgb2gray(border_img);
edge_img = edge(gray_img,'canny');
[m, n, j] = size(border_img);
H = [0 -1 0; -1 4 -1; 0 -1 0];
border_img = double(border_img);
grad_img = imfilter(border_img,H);
num_pixels = nnz(mask);
I = zeros(num_pixels);
J = zeros(num_pixels);
index_matr = zeros(m,n);
count=1;
for i=1:m
for j=1:n
if mask(i,j)==1
I(count) = i;
J(count) = j;
index_matr(i,j) = count;
count = count+1;
end
end
end
% create sparse matrix for each pixel
Coeff_matr = spalloc(num_pixels,num_pixels,5*num_pixels);
B = zeros(num_pixels,3);
for i = 2:m-1
for j = 2:n-1
if mask(i,j) == 1
for delta = -1:2:1
if mask(i,j+delta) == 1
Coeff_matr(index_matr(i,j),index_matr(i,j+delta)) = -1;
else
for chnl=1:3
B(index_matr(i,j),chnl) = B(index_matr(i,j),chnl) + double(border_img(i,j+delta,chnl));
end
end
if mask(i+delta,j) == 1
Coeff_matr(index_matr(i,j),index_matr(i+delta,j)) = -1;
else
for chnl = 1:3
B(index_matr(i,j),chnl) = B(index_matr(i,j),chnl) + double(border_img(i+delta,j,chnl));
end
end
end
if edge_img(i,j) == 1
for chnl=1:3
B(index_matr(i,j),chnl) = B(index_matr(i,j),chnl) + double(grad_img(i,j,chnl));
end
else
for delta=-1:2:1
if edge_img(i+delta,j) == 1
for chnl=1:3
B(index_matr(i,j),chnl) = B(index_matr(i,j),chnl) + border_img(i,j,chnl) - border_img(i+delta,j,chnl);
end
end
if edge_img(i,j+delta) == 1
for chnl=1:3
B(index_matr(i,j),chnl) = B(index_matr(i,j),chnl) + border_img(i,j,chnl) - border_img(i,j+delta,chnl);
end
end
end
end
Coeff_matr(index_matr(i,j),index_matr(i,j)) = 4;
end
end
end
final_img = double(border_img);
solns = Coeff_matr\B;
for channel = 1:3
for k = 1:num_pixels
final_img(I(k),J(k),channel) = solns(k,channel);
end
end
final_img = uint8(final_img);
figure,imshow(final_img);