diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2023-06-08 12:47:37 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2023-06-08 12:47:37 +0200 |
commit | 9709007a3ba8f6d50c738262618b4eccfc83ff8d (patch) | |
tree | fdd8857ffc8016823ad29cfd782afe9ba3bf3106 /matlab | |
parent | b072aa16c3808523aaefc1dfacc8703f9c635fb5 (diff) | |
parent | a6ea4db7c5b4fe942eb7bed4deee19efa6789b44 (diff) |
Merge branch 'dev' of https://github.com/heavydemon21/avans-dui into dev
Diffstat (limited to 'matlab')
-rw-r--r-- | matlab/invpers.m | 72 | ||||
-rw-r--r-- | matlab/traffic_lights.m | 69 |
2 files changed, 141 insertions, 0 deletions
diff --git a/matlab/invpers.m b/matlab/invpers.m new file mode 100644 index 0000000..e881ed8 --- /dev/null +++ b/matlab/invpers.m @@ -0,0 +1,72 @@ +clf +clc + +WIDTH = 480; +HEIGHT = 320; +MAX_AREA = WIDTH * HEIGHT / 10; + +HORIZON = 140; +STRETCH = 105; +SQUEEZE = 145; + +movingPoints = [STRETCH HORIZON; (WIDTH-STRETCH) HORIZON; WIDTH HEIGHT; 0 HEIGHT]; +fixedPoints = [0 0;WIDTH 0; (WIDTH-SQUEEZE) HEIGHT; SQUEEZE HEIGHT]; +t = fitgeotrans(movingPoints,fixedPoints,'projective'); + +x = imread('00021.jpg'); +x = imrotate(x, 180); +o = x; + +r = imref2d(size(x),[1 size(x,2)],[1 size(x,1)]); +x = imwarp(x,r,t,'OutputView',r); + +x = imgaussfilt(x, 3); +x = rgb2hsv(x); + +x = x(:,:,3); +x = imadjust(x); +x = x > 0.8; + +[lmap, lcount] = bwlabel(x); + +subplot(2,1,1); +hold on; +imshow(o); +plot([(WIDTH/2) (WIDTH/2)], [0 HEIGHT], 'Color', '#888', 'LineWidth', 2); + +subplot(2,1,2); +hold on; +imshow(label2rgb(lmap, 'jet', 'black')); + +sum = 0; +count = 0; +for i = 1:lcount + props = regionprops(lmap == i, 'BoundingBox', 'Area'); + s = props.BoundingBox; + sx = s(1); + sy = s(2); + lx = s(1) + s(3); + ly = s(2) + s(4); + width = lx - sx; + height = ly - sy; + area_weight = 40 + min(MAX_AREA, width * height); + horizontal_pos = (sx + width/2) / WIDTH; + sum = sum + horizontal_pos * area_weight; + count = count + area_weight; + rectangle('Position', [s(1) s(2) s(3) s(4)], 'EdgeColor', 'green'); +end + +avg = sum / count; +avg = avg * 2 - 1; +avg = max(-1, min(1, avg)); +avg + +subplot(2,1,1); +quiver(WIDTH/2,HEIGHT/2,avg*WIDTH/2,0,0,'linewidth',3,'color','r') + +if abs(avg) < 0.1 + "straight ahead" +else + temp = ["left", "right"]; + temp((sign(avg) + 1) / 2 + 1) +end
\ No newline at end of file diff --git a/matlab/traffic_lights.m b/matlab/traffic_lights.m new file mode 100644 index 0000000..8804778 --- /dev/null +++ b/matlab/traffic_lights.m @@ -0,0 +1,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 + |