how i can create a traffic light image using matlab code?
11 views (last 30 days)
Show older comments
imean create 3 colored circle inside a black box
Answers (1)
Image Analyst
on 7 Dec 2018
Try this code:
% Demo code to build a traffic light.
clc; % Clear the command window.
clearvars;
close all
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Make the background as a rounded rectangle
% with pure black ('k') color.
rectangle(...
'Position', [0 0 2 4], ...
'Curvature', 0.2, ...
'FaceColor', 'k')
axis equal
hold on;
% Define dark gray colors - color when that light is off.
darkGray = [0.2, 0.2, 0.2];
% Define the green, yellow, and red lamp colors.
% Colors need to be in the range 0-1, so if you get them
% from Photoshop or somewhere in uint8 values, besure to divide by 255.
% Or you can get them from this web site:
% Ref: https://www.e-paint.co.uk/Lab_values.asp
% Look up "Traffic Green", "Traffic Yellow", and "Traffic Red"
green = [0, 132, 80] / 255;
yellow = [239, 183, 0] / 255;
red = [184, 29, 19] / 255;
% Put up the three dark gray "off" lights.
pos = [0.5 2.75 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5 1.5 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
pos = [0.5 0.25 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)
% uiwait(helpdlg('This where all are "off"'));
% Place 3 circles. Normally only one is on.
% Pick the one you want to be on, and
% don't call rectangle for the ones you want to be off.
pos = [0.5 2.75 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)
pos = [0.5 1.5 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', yellow)
pos = [0.5 0.25 1 1];
rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)
title('Traffic Lights', 'FontSize', 24);
xlim([-0.5, 2.5]);
ylim([-0.5, 4.5]);

0 Comments
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!