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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
function redObjectTrack()
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
cam = webcam('Logitech Camera');
snapshot(cam);
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
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=500)
% 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
imshow(cropData)
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;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
%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
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% 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';
0 commentaires
Réponses (2)
Image Analyst
le 12 Mai 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
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!