-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.m
More file actions
121 lines (93 loc) · 2.75 KB
/
match.m
File metadata and controls
121 lines (93 loc) · 2.75 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
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
warning('off','all');
% close all
%% Loading things
tic
%% Load in the Champion Data
if ~exist('champs','var')
imdir = './Champions/';
imf = dir(imdir);
imf = imf(3:end);
champs = cell([numel(imf) 1]);
for i = 1:numel(imf)
champs{i}.name = strrep(imf(i).name,'.png','');
champs{i}.image = imread(strcat(imdir,imf(i).name));
champs{i}.r_ch = champs{i}.image(:,:,1);
champs{i}.g_ch = champs{i}.image(:,:,2);
champs{i}.b_ch = champs{i}.image(:,:,3);
champs{i}.r_hist = imhist(champs{i}.r_ch);
champs{i}.g_hist = imhist(champs{i}.g_ch);
champs{i}.b_hist = imhist(champs{i}.b_ch);
end
end
%% Load in Picture in question
% testChar = 'Renekton';
% testChar = 'Vi';
% testChar = 'Thresh';
% testChar = 'Karthus';
% testChar = 'Nidalee';
% testChar = 'Syndra';
% testChar = 'Chogath';
% testChar = 'Gragas'; % bad
testChar = 'file'; % HIS PIC
for i = 1:numel(imf)
if (strcmp(champs{i}.name,testChar))
qq = i;
end
end
base = imread(strcat('./img/',testChar,'.png'));
BL = base(end/4:end,1:end/4,:);
%% FIND THE SQUARE
I = rgb2gray(BL);
th = graythresh(I);
I_th = im2bw(I,th);
% Ifill = imfill(I_th,'holes');
% Iarea = bwareaopen(Ifill,100);
Iarea = bwareaopen(I_th,100);
Ifinal = bwlabel(Iarea);
stat = regionprops(Ifinal,'boundingbox');
imshow(I); hold on;
bb = zeros([numel(stat) 4]);
for cnt = 1 : numel(stat)
bb(cnt,:) = stat(cnt).BoundingBox;
rectangle('position',bb(cnt,:),'edgecolor','r','linewidth',2);
end
% [x y w h] rectangle
sq_size = (bb(:,3).*bb(:,4));
sq_sqness = abs(1-max(bb(:,3:4),[],2)./min(bb(:,3:4),[],2));
loc_bias = bb(:,1)./size(Ifinal,2)+(size(Ifinal,1)-bb(:,2))./size(Ifinal,1);
sq = (sq_size.*(sq_sqness<0.2))./loc_bias;
[~,ind] = max(sq);
charBox = floor(bb(ind,:));
rectangle('position',bb(ind,:),'edgecolor','g','linewidth',2);
charImg = BL(charBox(2):charBox(2)+charBox(4),charBox(1):charBox(1)+charBox(3),:);
figure
imshow(charImg)
%% FIND THE CHAMP
% Resize to prepare to MSE
sz = size(champs{1}.image);
mm = min(size(charImg(:,:,1)));
sq_charImg = charImg(1:mm,1:mm,:);
scale = sz(1)/mm;
res_charImg = im2double(imresize(sq_charImg,scale));
res_charImg = res_charImg(1:sz,1:sz,:);
dist = zeros([1 numel(imf)]);
for i = 1:numel(imf)
dist(i) = sum(sum(sum(abs(res_charImg - im2double(champs{i}.image)))));
end
[~, winner] = min(dist);
fprintf('Champion: %s\n',champs{winner}.name)
figure
imshow(champs{winner}.image)
subplot(3,2,1)
imshow(abs(im2double(res_charImg) - im2double(champs{winner}.image)));
subplot(3,2,2)
imshow(abs(im2double(res_charImg) - im2double(champs{qq}.image)));
subplot(3,2,3)
imshow(champs{winner}.image);
subplot(3,2,4)
imshow(champs{qq}.image);
subplot(3,2,5)
imshow(res_charImg);
subplot(3,2,6)
imshow(res_charImg);
toc