color detection, color detection in matlab, matlab color detection, detect color in matlab, matlab color detect
Hello 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: Few Image Processing Projects:

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:
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
B=getsnapshot(obj);

framesAcquired = 0;
while (framesAcquired <= 10) 
    
      data = getsnapshot(obj); 
      framesAcquired = framesAcquired + 1;    
      
      diff_im = imsubtract(data(:,:,1), rgb2gray(data)); 
      diff_im = medfilt2(diff_im, [3 3]);             
      diff_im = im2bw(diff_im,0.18);                   
      %stats = regionprops(diff_im, 'BoundingBox', 'Centroid'); 
      
  
      % 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(data)
    
    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(bc(1),bc(2), '-m+')
        a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '    Y: ', num2str(round(bc(2))), '    Color: Red'));
        set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
    end  
 
    hold off
    
end

clear all
  • 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:
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
B=getsnapshot(obj);
  • 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:
      data = getsnapshot(obj); 
      framesAcquired = framesAcquired + 1;    
      
      diff_im = imsubtract(data(:,:,1), rgb2gray(data)); 
      diff_im = medfilt2(diff_im, [3 3]);             
      diff_im = im2bw(diff_im,0.18);                   
      %stats = regionprops(diff_im, 'BoundingBox', 'Centroid'); 
      
  
      % 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(data)
  • 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:
for object = 1:length(stats)
        bb = stats(object).BoundingBox;
        bc = stats(object).Centroid;
        rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
        plot(bc(1),bc(2), '-m+')
        a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '    Y: ', num2str(round(bc(2))), '    Color: Red'));
        set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
    end
  • 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. :)