
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.
[dt_button link="https://www.theengineeringprojects.com/MATLABProjects/ColorDetectioninMATLABLiveVideo.rar" target_blank="false" button_alignment="default" animation="fadeIn" size="medium" style="default" bg_color_style="default" bg_hover_color_style="default" text_color_style="default" text_hover_color_style="default" icon="fa fa-chevron-circle-right" icon_align="left"]Download MATLAB Code[/dt_button]
- 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:

- 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:
Leave a Reply
Leave a Reply
You must be logged in to post a comment.
Comments on ‘’Color Detection in MATLAB Live Video‘’
( 13 )
winvideo is showing error
You should install its MATLAB driver, give it a google search.
Hi there
Thanks for great example,
I would like to know about It is possible to use this code for some video sequence to detect color and how should I change? Thanks before hand
Regards.
One more question
I have a LED blinking video sequence video and it blinks 3 types of color red, green and blue randomly, my goal is to detect it and identify them one by one like sequence, what would you suggest me?
Please I need your help!
thanks before hand.
Hi,
Yeah it’s possible, you need to read the whole video and then need to check for these 3 colors at the same time. Add me to Skype and I will help you out. My Skype id is theenggprojects.
Thanks.
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’);
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
Can+you+please+tell+the+concept+used+in+your+project+??
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.
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 !
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.
hi, I just wanted to know how to improve the frame rate of my laptop webcam. Is it good to use an external webcam?
hi how can i do to detect another color apart from rgb color, like black, purple yellow.