File Exchange

image thumbnail

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

version 1.4 (2.6 KB) by

This code can detect and track Red, Green and Blue colored objects in LIVE video.

4.35714
18 Ratings

52 Downloads

Updated

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

Comments and Ratings (45)

Sayan Seth

Mukundettan

Thank you

Mark Caruana

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

Mark Caruana

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

nmc (view profile)

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?

hani alsareti

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

Syed Asad Ali

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

Syed Asad Ali

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

Mayur Randive

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

Aju (view profile)

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

Arindam Bose (view profile)

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

Nathan

Nathan (view profile)

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

koushik

chaitanya

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

daher (view profile)

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

Verena (view profile)

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

Loai (view profile)

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

ADNAN (view profile)

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

Arindam Bose (view profile)

@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

Arindam Bose (view profile)

@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

Arindam Bose (view profile)

@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

Arindam Bose (view profile)

@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

Fiem (view profile)

Thanks a lot

Fiem

Fiem (view profile)

Updates

1.4

Description changed

1.4

Description Changed

1.3

1. Memory management enhanced
2. Description changed

1.1

Description updated

MATLAB Release
MATLAB 7.14 (R2012a)
Acknowledgements

Inspired: detecting_yellow_color_using_thresholding

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video