How do I change my program to recognizes red images from a video and puts a red box around it to green, yellow, blue, and orange

1 view (last 30 days)
function redObjectTrack()
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
cam = webcam('Logitech Camera');
vid = videoinput('macvideo', camera_id, format);
%Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1; %the app grabs a frame every this number of frames
%start the video aquisition here
% Set a loop that stop after 100 frames of aquisition
% Get the snapshot of the current frame
data = getsnapshot(vid);
cropData = imcrop(data, [950, 470, 550, 400]);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(cropData(:,:,1), rgb2gray(cropData));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = imbinarize(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
%plot shows the red i think
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
% the yellow is the color of the text
hold off
% Both the loops end here.
% Stop the video aquisition.
% Flush all the image data stored in the memory buffer. flushdata(vid); % Clear all variables clear all sprintf('%s','Program has now ended') %the getCameraInfo class is this
function [camera_name, camera_id, resolution] = getCameraInfo(a)
camera_name = 'Logitech Camera';
camera_info = imaqhwinfo('macvideo');
camera_id = 2;
resolution = 'YCbCr422_1920x1080';

Answers (2)

xiao on 10 May 2018
what do you mean red image? can you give a instance

Image Analyst
Image Analyst on 12 May 2018
You simply need to keep your camera still and look in the known places where the spots will be. Then you can measure the mean RGB in those spots and determine what reference color it's closest to, for example compute distance in RGB color space with the sqrt() function
  1 Comment
Hershey Wiseman
Hershey Wiseman on 14 May 2018
Could you give me an example of this please I'm not sure that I know how to look for a specific color in the same space. I have created a rectangle that I display now to line up the projector at the same area every time to capture the same result, but in terms of capturing a color there I am not sure how to get the information from the matrix with the RGB info

Sign in to comment.


Find more on Image Preview and Device Configuration 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!