aboutsummaryrefslogtreecommitdiff
path: root/matlab/traffic_lights.m
blob: 880477880d2cffdab3efd997ee4fc8e9cd163ee8 (plain)
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
clf
clc
hold on;

x = imread('010.jpg');
x = imrotate(x, 180);
o = x;
x = rgb2hsv(x);

t = x(:,:,3);
t = imadjust(t);
t = t < 0.15;

[lmap, lcount] = bwlabel(t);

imshow(label2rgb(lmap, 'jet', 'black'));
imshow(o);
for i = 1:lcount
    s = regionprops(lmap == i, 'BoundingBox').BoundingBox;
    sx = s(1);
    sy = s(2);
    lx = s(1) + s(3);
    ly = s(2) + s(4);
    width = lx - sx;
    height = ly - sy;
    area = width * height;
    if area < 450
        continue
    end
    aspect = height / width;
    % stoplichten zullen wel een verhouding van ongeveer 2.2 hebben
    if abs(aspect - 2.2) > 0.5
        continue
    end

    red_light = [round(sx + width / 2), round(sy + 0.2 * height)];
    yellow_light = [round(sx + width / 2), round(sy + 0.5 * height)];
    green_light = [round(sx + width / 2), round(sy + 0.8 * height)];

    light_status = 0; % 0 = geen lamp, 1 = rood, 2 = geel, 3 = groen
    
    % x(red_light(2), red_light(1), :)
    if (light_status == 0) && ...
       (abs(x(red_light(2), red_light(1), 1) - 0.5) > 0.4) && ...
       (x(red_light(2), red_light(1), 2) > 0.4)
        light_status = 1;
        plot(red_light(1), red_light(2), '.');
    end
    if (light_status == 0) && ...
       (abs(x(yellow_light(2), yellow_light(1), 1) - 0.1) < 0.1) && ...
       (x(yellow_light(2), yellow_light(1), 2) > 0.4)
        light_status = 2;
        plot(yellow_light(1), yellow_light(2), '.');
    end
    if (light_status == 0) && ...
       (abs(x(green_light(2), green_light(1), 1) - 0.4) < 0.1) && ...
       (x(green_light(2), green_light(1), 2) > 0.4)
        light_status = 3;
        plot(green_light(1), green_light(2), '.');
    end

    if light_status == 0
        continue
    end
    
    rectangle('Position', [s(1) s(2) s(3) s(4)], 'EdgeColor', 'green');
    status = ["rood" "geel" "groen"]; status(light_status)
end