File Exchange

image thumbnail

How to Detect and Track Red, Green and Blue Colored Object in LIVE Video

version 1.4.0.0 (2.6 KB) by Arindam Bose
This code can detect and track Red, Green and Blue colored objects in LIVE video.

36 Downloads

Updated 30 Jun 2016

View License

This code can detect and track Red, Green and Blue colored objects in LIVE video. In this code objects can be segmented using color detection and segmentation.
For detail documentation: http://arindambose.com/?p=597
For video tutorial:
http://youtu.be/QPzynkdsuW8

Cite As

Arindam Bose (2020). How to Detect and Track Red, Green and Blue Colored Object in LIVE Video (https://www.mathworks.com/matlabcentral/fileexchange/40154-how-to-detect-and-track-red-green-and-blue-colored-object-in-live-video), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (57)

ibra

install Image acquisition toolbox to have imaq

Shi Chen Na

>> RGB_Detection_and_Tracking
Undefined variable "imaq" or class "imaq.VideoDevice".

Error in RGB_Detection_and_Tracking (line 14)
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY 2_640x480', ... % Acquire input video stream

Hi everyone, I've error reflected on top while attempting to run the program. Could anyone assist me to fix it? Thank you so much

potato chip

Sarah Hui

Well written code, easy to understand, and really helpful with the thesis I'm writing. Thanks so much!!

Sarah Hui

@Mohamed Ashraf , Are you able to run the code normally? Why did my aprace the following error:
Undefined variable "vision" or class "vision.TextInserter".

Error in ROOD (line 25)
htextins = vision.TextInserter('Text', 'Number of White Object(s): %2d', ...
% Set text for number of blobs

How did you define the "vision" object? Because it gave error asking to define "vision".

How did you define the "vision" object? Because it gave error asking to define "vision".

First of all, great job!
I have a question, how did you determine the threshold for the three colors?

anil kumar

Error in ==> RGB_Detection_and_Tracking at 14
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video
stream
im getting like this what shall doo

sarang nair

Sayan Seth

Mukundettan

Thank you

HI how do we find out thresholds of other colors like white what would be the threshold red is 0.24 ..how do you find their trheshold amount

HI how do we find out thresholds of other colors like white what would be the threshold red is 0.24 ..how do you find their trheshold amount

How can we detect different colors of the map image by coding in matlab?

Pavan Singh

Hi,

How can you use this for still images with any need for tracking, just want to be able to count number of blocks of certain colours on a still image.

Thanks

Anil kumar

this code not working

Anil kumar

this code not working

Geart job Arindam, It helped me so much..

Adithya MD

awesome job

nmc

Do you happen to know how to track the first identified colored object in a video when other objects will appear with the same color without referring to geometry?

vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_1280x720', ... % Acquire input video stream 'ROI', [1 1 640 480], ... 'ReturnedColorSpace', 'rgb'); i have an error in code it says undefined variable "imaq" or class "imaq.videoDevice"
i am using R2010a version

Hello,

I've been trying to run this on 2015b and I get this error and can't seem to figure it out:

Error using matlab.system.StringSet/checkValues
The empty string is not a valid StringSet string.

Error in matlab.system.StringSet/setValues

Error in matlab.system.StringSet

Error in RGB_Tracking (line 29)
htextinsRed = vision.TextInserter('Text', 'Red : %2d', ... % Set text for number of blobs

Any help would be appreciated.

Thanks,
Del

Hi..sir Arindam Bose please understand me about this intensity formula and threshold for finding green intensity....

r=center(:,:,1);
g=center(:,:,2);
b=center(:,:,3);
green=g-(r/2)-(b/2);
bw=(green>30);

in this formula why use /2...and why g is place before r/2 and b/2..and also why use green>30...why use 30 in this...sir pls ans ASAP

Hi..sir Arindam Bose please understand me about this intensity formula and threshold for finding green intensity....

r=center(:,:,1);
g=center(:,:,2);
b=center(:,:,3);
green=g-(r/2)-(b/2);
bw=(green>30);

in this formula why use /2...and why g is place before r/2 and b/2..and also why use green>30...why use 30 in this...sir pls ans ASAP

A great work done.
nice working....

Hi, i'm getting the following erros can you help me
??? Undefined variable "imaq" or class "imaq.VideoDevice".

Error in ==> RGB_Detection_and_Tracking at 14
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream

Aju

A great work been done. Thank You so much. But can anybody please help me out to correct this error mentioned below?

Error in RGB_Detection_and_Tracking (line 6)
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream

Abu Shuvom

ABHISHEK

Hey.
How can I connect this code to UART for serial communication to AVR..?
Can i follow using robot the path of red green & blue color object by using this interface..??

Atif Anwer

Hi.
The line "step(hshapeinsBox, rgbFrame, bboxRed, single([1 0 0])); % Instert the red box" wasnt working for me for some reason. However i do recommend an alternate that worked which did work fine for me :

insertShape(rgbFrame, 'Rectangle', bboxRed, 'Color', 'red'); % Insert the red box

Arindam Bose

@Nathan: Please change the threshold accordingly. It depends on the ambient color

Nathan

Sometimes, Matlab detects blue as green color. :( please help

koushik

Excellent work sir, i saw your video as well but when i am running code i am getting error as :

Undefined variable "imaq" or class "imaq.VideoDevice".

Error in RGB_Detection_and_Tracking (line 14)
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream

can you please help
thanks in advance

daher

hi, how to tag and track the first identified colored object, for example, in a video when other objects will appear with the same color without referring to geometry

Verena

Hello, thanks you so much!

@CHANDAN Nagaraja did you find any solution for your problem? I have the same problem ;)

Thank you so much.
I was wondering can we store all the X,Y vales of red object as output in MATlab code? Do you know code for it?

Loai

hello, first I wanted to thank u and i wanted to ask what should i do if i want it to follow a combination of RGB and return its area

Hello dear! I am getting error during running code. please help me with this. Below i have written down that error. your help will be highly appreciated. Thanks
??? Undefined variable "imaq" or class "imaq.VideoDevice".

Renato Henz

Hello everyone! I'm getting this error while trying to run the program:

??? Undefined variable "imaq" or class "imaq.VideoDevice".

Error in ==> RGB_Detection_and_Tracking at 14
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video
stream

Does anyone know what might be happening, or how can I fix it?
Thanks a lot!

ADNAN

gud job

Elizabeth

Hi, I was wondering if it would be possible to change the input from LIVE video to recorded video?

@Arindam Bose thank you

Arindam Bose

@swapna chougule: The number of red pixels are given in terms of area of corresponding blob. Look into area property of each blob.

how to get number of pixels of red color using in this code??

ok thnx bro

Arindam Bose

@mohammed ghazal: Red, Green and Blue are the basic colors. The simple code segment which subtracts the intensity image from the corresponding layer, can be used only for detecting these three basic colors. But if you need to detect more colors you have to convert the image into HSV color map and prepare different threshold filters for different colors in Hue layer.

thnx dear my copy of matlab2012b don't have computer vision toolbox installed,so i installed the old version2012a...and it is worked ok...thnx alot
tell me pls how to determine color Threshold ... what is the rule ? i want
to track more colors
redThresh = 0.24; % Threshold for red detection
greenThresh = 0.05; % Threshold for green detection
blueThresh = 0.15; % Threshold for blue detection

Arindam Bose

@mohammed ghazal The problem is that you don't have computer vision toolbox installed in your MATLAB version. This special toolbox comes with newer
version of MATLAB e.g. R2012a onwards. The Blob analysis handles does the statistical analysis of detected blobs in a logical image. However you can use 'regionprops' command instead of vision.blobAnalysis. It does almost same job. Dig into the help for more information about it.
Anyway you can collect the newer version of MATLAB. MATLAB R2013a has already been released. Always stay updated to experience more.

how to solve this error pls?

Undefined variable "vision" or class "vision.BlobAnalysis".

Error in RGB_Detection_and_Tracking (line 19)
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob
analysis handling

Arindam Bose

@Masoud : Because, I used a while loop in the middle of code and set the loop counter to around 300 frames. So theoretically it will run for 10 seconds(30fps). You can change the loop counter to several thousand. I did not make the loop counter Inf because it will cause a little inconvenience. In that case you have to release the camera handle individually by running this code piece "release(vidDevice)".
So its better to have a loop counter. You can change it to several thousands to run it for several minutes. I made it smaller for demonstration.

Hi
Why this program will close after several seconds automatically???

Fiem

Thanks a lot

Fiem

Updates

1.4.0.0

Description changed

1.4.0.0

Description Changed

1.3.0.0

1. Memory management enhanced
2. Description changed

1.1.0.0

Description updated

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: detecting_yellow_color_using_thresholding