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 ...
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:
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:
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.
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.
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. :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ Color Detection in MATLAB Live Video ‘’ ( 10 )
0
sharifrajabu
Says:
hi how can i do to detect another color apart from rgb color, like black, purple yellow.
Reply
100
1
raj
Says:
hi, I just wanted to know how to improve the frame rate of my laptop webcam. Is it good to use an external webcam?
Reply
100
2
malibongwe
Says:
hi I have a project for a color detection green and black bean.i would like to know if its possible to use this code. thank you.
Reply
100
3
enthusiast
Says:
Hello!
Your work is absolutely helpful and thank you for that ! i have encountered a problem in above code and that is that it does not detect green or blue when we subtract those particular color planes. This is puzzling to me as in the color detection in images it works as it should for all of R G B but does not work here. Could help me in this regard.
Thank you !
Reply
100
4
ariena
Says:
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.
Reply
akshay
Says:
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
Reply
100
7
tahir
Says:
what is happening in this cod?
for object=1:length(stats)
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','Y','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)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
Reply
100
8
lagari
Says:
winvideo is showing error
Reply
100
9
syedzainnasir1
Says:
You should install its MATLAB driver, give it a google search.
Reply