-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform_board.m~
87 lines (83 loc) · 3.4 KB
/
transform_board.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
function im_result = transform_board(im_color, im_board_golden)
[height, width, ~] = size(im_color);
im_board_orig_color = padarray(im_color, [max(height,width)-height,max(height,width)-width, 0]);
im_board_orig = rgb2gray(im_board_orig_color);
board_mask = generate_board_mask(im_board_orig);
[T, corners, corners_golden] = find_homography_transformation(board_mask);
im_result = align_and_crop(im_board_orig_color, im_board_golden, T, corners, corners_golden);
end
function board_mask = generate_board_mask(im_board)
[height, width] = size(im_board);
thresh = graythresh(im_board);
im_board_only_bw = im2bw(im_board, thresh);
figure;
subplot(2,5,1);
imshow(im_board);
subplot(2,5,2);
imshow(im_board_only_bw);
im_board_only_bw = imfill(im_board_only_bw, 'holes');
subplot(2,5,3);
imshow(im_board_only_bw);
se = strel('disk', fix(height/500), 4);
im_board_only_bw = imerode(im_board_only_bw, se);
subplot(2,5,4);
imshow(im_board_only_bw);
STATS=regionprops(im_board_only_bw,'Area','Centroid','BoundingBox');
area=cat(1,STATS.Area);
im_board_only_bw = bwareaopen(im_board_only_bw,fix(max(area(:))*0.5));
subplot(2,5,5);
imshow(im_board_only_bw);
se = strel('disk', fix(height/500), 4);
im_board_only_bw = imdilate(im_board_only_bw, se);
subplot(2,5,6);
imshow(im_board_only_bw);
se = strel('disk', fix(height/50), 4);
im_board_only_bw = imdilate(im_board_only_bw, se);
subplot(2,5,7);
imshow(im_board_only_bw);
im_board_only_bw = imfill(im_board_only_bw, 'holes');
subplot(2,5,8);
imshow(im_board_only_bw);
se = strel('disk', fix(height/50), 4);
im_board_only_bw = imerode(im_board_only_bw, se);
subplot(2,5,9);
imshow(im_board_only_bw);
STATS=regionprops(im_board_only_bw,'Area','Centroid','BoundingBox');
area=cat(1,STATS.Area);
im_board_only_bw = bwareaopen(im_board_only_bw,fix(max(area(:))*0.2));
subplot(2,5,10);
imshow(im_board_only_bw.*im_board);
board_mask = im_board_only_bw;
end
function [T, corners, corners_golden] = find_homography_transformation(board_mask)
corners = find_corners(board_mask);
corners_golden = load('golden_corners.mat');
corners_golden = corners_golden.corners;
v=homography_solve(corners',corners_golden');
T=projective2d(v');
end
function im_result = align_and_crop(im_board_orig_color, im_board_golden, T, corners, corners_golden)
im_board_orig_h = imwarp(squeeze(im_board_orig_color),T,'nearest');
figure;
imshow(im_board_orig_h);
im_board_orig = rgb2gray(im_board_orig_color);
k1_im = double(zeros(size(im_board_orig)));
k1_im(fix(corners(1,2)),fix(corners(1,1))) = 1;
k1_im_h = imwarp(k1_im,T);
[~, idx] = max(k1_im_h(:));
[k1_y,k1_x] = ind2sub(size(k1_im_h),idx);
im_board_orig_h_t = imtranslate(im_board_orig_h,[corners_golden(1,1)-k1_x, corners_golden(1,2)-k1_y]);
[height_golden, width_golden] = size(im_board_golden);
[height_h, width_h, ~] = size(im_board_orig_h_t);
diff_height = 0;
diff_width = 0;
if height_h < height_golden
diff_height = height_golden-height_h;
end
if width_h < width_golden
diff_width = width_golden-width_h;
end
im_board_orig_h_t = padarray(im_board_orig_h_t, [diff_height diff_width], 'post');
im_board_orig_h_t = im_board_orig_h_t(1:height_golden,1:width_golden,:);
im_result = im_board_orig_h_t;
end