color detection, color detection in matlab, matlab color detection, detect color in matlab, matlab color detectHello friends, I hope you all are fine and having fun with your lives. Today, I am going to show you Color Detection in MATLAB Live Video. In this project I am gonna take a live video feed from a camera and then will detect a specific color in that video feed. I have posted a project before in which I have done the same Color Detection in Images using MATLAB but that color detection was for images but in today’s post I am gonna do the color detection in MATLAB for live video feed.

I have designed this project in MATLAB 2009 and have also tested it on MATLAB 2015 and it works fine. So, I think it will work on any version of MATLAB. You must have the image processing toolbox in your MATLAB. If you got any questions then ask in comments and I will try to resolve them. So, let’s get started with Color Detection in MATLAB Live Video:

Color Detection in MATLAB Live Video

  • First of all, download the code from clicking the below button and open it in your MATLAB.

Download MATLAB Code

  • Now let me explain this code in detail.
  • One important thing, if you encountered some MATLAB project and you don’t know how it works then the best way of testing is to paste commands one by one in your Command Window.
  • In this way you will understand the working of each command and will give you a better idea of how it works.
  • The complete MATLAB cofor Color Detection in MATLAB Live Video is as follows:

  • I have also added the comments in the code so that you can understand it in a better way.
  • But if you still got any problems then as k in comments and I will resolve them.
  • Now run your simulation and bring any red color object in front of your camera and you will see a bounding box will be formed around red color and it will also show the coordinated of that red color.
  • The results are shown in the below figure:

color detection, color detection in matlab, matlab color detection, detect color in matlab, matlab color detect

  • You can see in the above figure that I am holding a bottle cap which is of red color and our code is detecting that red color.
  • Moreover it has created a bounding box around that bottle cap and then showing the X and Y coordinates of that cap.
  • I have also mentioned the Color Red infront of it and the color of these coordinates is also RED.
  • You can change these colors as you want and can detect any color using this code.
  • Let me explain this code a little bit.
Code Explanation
  • First of all, I created an object for the video camera and then took images from that video using the below code.
  • In this code I have used 1 for video input. 1 is default for your webcams and if you have any external camera connected via usb then you have to use 2 instead of 1.
  • The code is as follows:

  • After that I have created a while loop of 100 framesso it will keep on detecting the color for 100 frames and then it will stop you can increase or decrease it.
  • In this while loop, first of all I have subtracted the red color from each frame using the imsubtract command.
  • You can see the input of imsubtract is data( : , : , 1 ), where data is our image and 1 is used for red color subtraction, if you use 2 then it will subtract the green color so using these values you can subtract any color and infact you are detecting that color.
  • After that I have applied filter to remove the noise and have also removed the pixels less than 300px.
  • Then I have connected the dots of red color to create a single red object.
  • This code is as follows:

  • Finally after the detection of this red color, I have created a bounding box around it and have also printed its coordinates.
  • The code is as follows:

  • So, that’s how I ma doing the color detection in MATLAB Live Video.
  • The below video will explain this Color Detection in MATLAN Live Video in more detail:

I hope you guys have enjoyed this Color Detection in MATLAB and are gonna use it your projects. That’s all for today, will see you guys in the coming tutorials. Till then take care and have fun. 🙂


  1. what is happening in this cod?

    for object=1:length(stats)

    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’);

  2. can u please extend this program. when it identify one red object 1st led should glow through “Arduino”. when it identifies two red obj’s, two LEd’s should glow

  3. I try to changes the colour to the green.but it’s not working.i only changes data(:,:,2) is it true.or also to changes another coding.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.