-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimageEffectiveAreaInterception.m
executable file
·84 lines (76 loc) · 1.76 KB
/
imageEffectiveAreaInterception.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
% 参考http://blog.csdn.net/dengxf01/article/details/53374014
% 参考http://blog.csdn.net/wd1603926823/article/details/45672741
% 张伟等 《鱼眼图像校正算法研究》
function [img_valid, R] = imageEffectiveAreaInterception(img, T)
% input
% img: rgb image
% T: gray threshold
% output
% img_valid: effective image area
% R: effective image area radius
% rgb to gray
% gray = 0.299r + 0.587g + 0.114b
img_gray = rgb2gray(img);
% image size
[m, n, k] = size(img_gray);
% scan from top to bottom
for i = 1:m
flag = 0;
for j = 1:n
if(img_gray(i, j) >= T)
if(img_gray(i+1, j) >= T)
top = i;
flag = 1;
break;
end
end
end
if flag == 1; break; end
end
% scan from bottom to top
for i = m:-1:top
flag = 0;
for j = 1:n
if(img_gray(i, j) >= T)
if(img_gray(i-1, j) >= T)
bottom = i;
flag = 1;
break;
end
end
end
if flag == 1; break; end
end
% scan from left to right
for j = 1:n
flag=0;
for i = top:bottom
if(img_gray(i, j) >= T)
if(img_gray(i, j+1) >= T)
left = j;
flag = 1;
break;
end
end
end
if flag == 1; break; end
end
% scan from right to left
for j = n:-1:left
flag = 0;
for i = top:bottom
if(img_gray(i, j) >= T)
if(img_gray(i, j-1) >= T)
right = j;
flag = 1;
break;
end
end
end
if flag == 1; break; end
end
% effictive area radius
R = max((right - left)/2, (bottom - top)/2);
% create effictive area
img_valid = imcrop(img, [left, top, 2*R, 2*R]);
end