-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.m
109 lines (102 loc) · 2.17 KB
/
final.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
vid = videoinput('winvideo', 2, 'YUY2_640x480');
preview(vid)
pause(1)
aur=arduino('COM1');
pinMode(aur,12,'output');
pinMode(aur,13,'output');
pinMode(aur,11,'output');
pinMode(aur,10,'output');
while(1)
im=getsnapshot(vid);
im=ycbcr2rgb(im);
r_c=im(:,:,1);
g_c=im(:,:,2);
b_c=im(:,:,3);
%subplot(1,3,1);imshow(r_c);subplot(1,3,2);imshow(g_c);subplot(1,3,3);imshow(b_c);
out=0;
r_out=r_c<5;
g_out=g_c<5;
b_out=b_c<5;
%figure(2);
%subplot(1,3,1);imshow(r_out);subplot(1,3,2);imshow(g_out);subplot(1,3,3);imshow(b_out);
out=r_out;
s=strel('disk',2);
out=imdilate(out,s);
se=strel('disk',10);
out=imerode(out,se);
a=zeros(480,640);
a(129,:)=1;
a(301,:)=1;
a(:,179)=1;
a(:,401)=1;
R1=zeros(480,640);
R2=zeros(480,640);
R3=zeros(480,640);
R4=zeros(480,640);
for x=1:179
for y=130:299
R1(y,x)=1;
end
end
for x=180:400
for y=130:299
R2(y,x)=1;
end
end
for x=400:640
for y=130:299
R3(y,x)=1;
end
end
for x=180:400
for y=300:480
R4(y,x)=1;
end
end
R1_o=and(out,R1);
R2_o=and(out,R2);
R3_o=and(out,R3);
R4_o=and(out,R4);
%subplot(1,4,1);imshow(R1_o);subplot(1,4,2);imshow(R2_o);subplot(1,4,3);imshow(R3_o);subplot(1,4,4);imshow(R4_o);
R1_c=size(find(R1_o(:)==1));
R2_c=size(find(R2_o(:)==1));
R3_c=size(find(R3_o(:)==1));
R4_c=size(find(R4_o(:)==1));
M1=max(R1_c(1,1),R2_c(1,1));
M2=max(R3_c(1,1),R4_c(1,1));
M=max(M1,M2);
if R1_c(1,1)==M
out=R1;
digitalWrite(aur,12,1);
digitalWrite(aur,13,0);
digitalWrite(aur,11,0);
digitalWrite(aur,10,0);
disp('Left')
elseif R2_c(1,1)==M
out=R2;
digitalWrite(aur,12,0);
digitalWrite(aur,13,0);
digitalWrite(aur,11,0);
digitalWrite(aur,10,0);
elseif R3_c(1,1)==M
out=R3;
digitalWrite(aur,10,1);
digitalWrite(aur,11,0);
digitalWrite(aur,12,0);
digitalWrite(aur,13,0);
disp('Right')
elseif R4_c(1,1)==M
out=R4;
digitalWrite(aur,10,1);
digitalWrite(aur,11,0);
digitalWrite(aur,12,1);
digitalWrite(aur,13,0);
disp('Forward')
else
out=0;
end
imshow(out);
pause(1)
%close all
clear im
end