-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhlac_main2.m
158 lines (131 loc) · 3.82 KB
/
hlac_main2.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
%% 2値HLACを利用した間違い探し検出コード
close all;
clc;
%% フィルタの準備(cell形式)
hlac_filters = { ...
[0 0 0; 0 1 0; 0 0 0], ...
[0 0 0; 0 1 1; 0 0 0], ...
[0 0 1; 0 1 0; 0 0 0], ...
[0 1 0; 0 1 0; 0 0 0], ...
[1 0 0; 0 1 0; 0 0 0], ...
[0 0 0; 1 1 1; 0 0 0], ...
[0 0 1; 0 1 0; 1 0 0], ...
[0 1 0; 0 1 0; 0 1 0], ...
[1 0 0; 0 1 0; 0 0 1], ...
[0 0 1; 1 1 0; 0 0 0], ...
[0 1 0; 0 1 0; 1 0 0], ...
[1 0 0; 0 1 0; 0 1 0], ...
[0 0 0; 1 1 0; 0 0 1], ...
[0 0 0; 0 1 1; 1 0 0], ...
[0 0 1; 0 1 0; 0 1 0], ...
[0 1 0; 0 1 0; 0 0 1], ...
[1 0 0; 0 1 1; 0 0 0], ...
[0 1 0; 1 1 0; 0 0 0], ...
[1 0 0; 0 1 0; 1 0 0], ...
[0 0 0; 1 1 0; 0 1 0], ...
[0 0 0; 0 1 0; 1 0 1], ...
[0 0 0; 0 1 1; 0 1 0], ...
[0 0 1; 0 1 0; 0 0 1], ...
[0 1 0; 0 1 1; 0 0 0], ...
[1 0 1; 0 1 0; 0 0 0] ...
};
%% 画像読み込み
% Wikipediaの'Spot_the_difference.png'はインデックス付きの画像
% カラーマップの復元が必要となる
[img,cmap] = imread('./img/Spot_the_difference.png');
img = (ind2rgb(img, cmap)); % rgbに変換
%img = imread('./img/saize3.jpg');
% 浮動小数点イメージデータに変換する
img = im2double(img);
% サイズ取得
colsize = size(img,1); % 縦サイズ
rowsize = size(img,2); % 横サイズ
% 画像を左右に2等分する
ref_img = img(1:colsize, 1:uint16(rowsize/2), :);
tar_img = img(1:colsize, uint16(rowsize/2)+1:rowsize, :);
% グレースケールへ
% gray = R .* 0.3 + G .* 0.59 + B .* 0.11
ref_gray = ref_img(:,:,1) .* 0.3 + ref_img(:,:,2) .* 0.59 + ref_img(:,:,3) .* 0.11;
tar_gray = tar_img(:,:,1) .* 0.3 + tar_img(:,:,2) .* 0.59 + tar_img(:,:,3) .* 0.11;
% 2値化する(OTSU)
gthresh = my_graythresh(ref_gray); %graythresh(ref_gray);
ref_bin = ref_gray > gthresh ;
tar_bin = tar_gray > gthresh ;
%% HLAC特徴量を求める
nx = 20;
ny = 20;
ref_hlac = extract_batchwise_hlac(ref_bin,hlac_filters, nx, ny);
tar_hlac = extract_batchwise_hlac(tar_bin,hlac_filters, nx, ny);
% グラフ描画用パラメータの用意
ref_X = 1:size(ref_hlac, 2);
ref_Y = 1:size(ref_hlac, 1);
ref_Z = ref_hlac(ref_Y,ref_X);
tar_X = 1:size(tar_hlac, 2);
tar_Y = 1:size(tar_hlac, 1);
tar_Z = tar_hlac(tar_Y,tar_X);
%% 内積を求める
hlac_angles = zeros(size(ref_Y));
for i=ref_Y
ha = vector_angle(ref_hlac(i,:), tar_hlac(i,:), 1e-6);
hlac_angles(i) = ha;
end
%% 描画
tiledlayout(2,5);
nexttile
contourf(ref_X,ref_Y,ref_Z);
title('reference');
nexttile
contourf(tar_X,tar_Y,tar_Z);
title('target');
nexttile
contourf(tar_X,tar_Y,abs(tar_Z-ref_Z));
title('difference');
nexttile(7)
imshow(ref_bin);
title('reference');
nexttile(8)
imshow(tar_bin);
title('target');
nexttile(6)
plot(ref_Y,real(hlac_angles));
nexttile(4,[2 2])
%colsize; % 縦サイズ
%rowsize; % 横サイズ
ax = gca;
ax.XDir = 'normal';
ax.YDir = 'reverse';
img = tar_img ;
batches = split_into_baches(img, nx, ny);
x_each = size(batches,2);
y_each = size(batches,1);
x_lim = x_each * nx;
y_lim = y_each * ny;
ax.XLim = [1 x_lim];
ax.YLim = [1 y_lim];
im = image('CData',img,'XData',[1 ax.XLim],'YData',[1 ax.YLim]);
im.AlphaData = 0.5;
hold on
p = 1;
th = 0.04;
for y=1:y_each:y_lim
for x=1:x_each:x_lim
angle = real(hlac_angles(p));
if angle > th
if(angle <= 1 )
r = rectangle('Position',[x y x_each y_each]);
r.FaceColor = [0 angle 0 0.7];
r.EdgeColor = 'b';
r.LineWidth = 1;
else
% r = rectangle('Position',[x y x_each y_each]);
% r.FaceColor = [0 1 0 0.7];
% r.EdgeColor = 'r';
% r.LineWidth = 1;
end
end
p = p + 1;
end
end
hold off
% figure(1);
% tiledlayout(1,2);