Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge about Multi Color Detection in MATLAB. Color Detection scheme plays a vital role at some places. Like in Image processing technique the detection of specific color among the different colors is necessary to perform some action on it. For example there are three cars of the colors red, blue and green and we want to wash the red car. You should also have a look at Color Detection in Images using MATLAB because I am gonna use the same code and will add some improvements in it. Moreover, if you are working on Live Videos instead of Images then you should have a look at Color Detection in Live Video using MATLAB.
For this purpose we need to design an algorithm which is capable of detecting red color among the different colors. Similarly there are a lot of other examples are there to perform the tasks like that. This tutorial will elaborate the step by step explanation of the algorithm designed for multi color detection in MATLAB using Graphical User Interface (GUI). The tutorial consists of two different parts. In first part the GUI will be created while the second part will consist of an algorithm to detect a particular color among the different colors. Further details about the designed algorithm is elaborated in the section below.
Multi Color Detection in MATLAB
Here, in the tutorial Multi Color Detection in MATLAB, I will explain the step by step description about designing an algorithm for multi color detection in MATLAB GUI. The total project is divide into two parts, GUI will be created in the first part and algorithm designing will be explained in the second part.
You can download the complete MATLAB GUI simulation here by clicking on the button below.
Download .rar. file, extract it and enjoy the complete package :)
This section of the tutorial Multi Color Detection in MATLAB will elaborate you all of the steps involved during the design of Graphical User Interface (GUI).All of the steps are given below.
GUI design consists of several different steps which are explained below.
GUI consists of the two different panel.
The left panel is for browsing or loading an image from the laptop or computer.
Left panel has a button to browse an image, an axis to visualize the loaded image and edit box to view the image's complete path.
The right panel is designed for color detection for the browsed image.
Right panel has three different buttons in order to detect red, green and blue colors and an axis to display the detected color on the image loaded in the left panel.
So that was the detailed description of the GUI, designed for multi color detection in MATLAB.
Initially designed GUI without panels is shown in the figure below.
The updated GUI with both the panels with proper tags is shown in the figure below.
Algorithm Design for Multi Color Detection in MATLAB
In this section of the tutorial Multi Color Detection in MATLAB, I will explain you about the design of an algorithm for detecting multi colors from the image loaded into GUI. The algorithm is further divided into two different parts. In the first part image is loaded into the GUI for the detection of multi color. Whereas, the second step consists of the detection of multi colors and its display on the GUI.
Go to the call back back function of Browse button and just copy paste here, the MATLAB code given below.
global ImageFile
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
ImageFile = strcat(pathname, filename);
axes(handles.axes1);
imshow(ImageFile) % displays the image on particular axis
set(handles.edit1,'string',ImageFile); %sets the text of the edit box on GUI
I have first write a line to load the image from its path into the GUI.
After loading the images I have adjusted an axis on which the image is to be displayed.
At the end I have set the text of the edit box as the address of the image.
So that the was the logic to load an image into GUI of MATLAB.
global ImageFile
ImageFile
data = imread(ImageFile);
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); % subtracts the red color from the image
%Use a median filte tFileo filter out noise
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
axes(handles.axes2); % axis adjustment
imshow(data) % displays the image with the detected colors
hold on
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)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'blue');
end
hold off
The main part of this code is to subtract the red color from the loaded image and to display it on the axis.
I have made rectangular areas around the detected colors at the end.
So that was the description of the algorithm to detect the red colors from the loaded image.
The final Updated GUI is shown in the figure below.
The image is to be loaded in the left panel of the updated GUI.
Loaded image in to the GUI is shown in the figure below.
Color will be detected in the right panel.
Red color detected from the loaded image is shown in the figure below.
Green color detected from the loaded image is shown in the figure below.
Blue color detected from the loaded image is shown in the figure below.
So that is all from the tutorial Multi Color Detection in MATLAB. I hope you enjoyed this tutorial. If you face any sort of problems, you can freely ask me in comments any time without even feeling any kind of hesitation. Our team will try our level best to entertain you and to solve your issues in some better way, if possible. I will explore MATLAB as well as other software in my later tutorials and will surely share all of them with you guys as well. So, till then, Take Care :)
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 ‘’ Multi Color Detection in MATLAB ‘’ ( 1 )
0
aijazahmed
Says:
Respected sir
First of all thank you for such a great tutorial, this project is very useful for beginners
currently i am encountering a problem
while running the code i am able to upload the picture but while pressing "detect red , green, or blue color button nothing is changing on left panel of GUI
it is remaining same as blank
there is no any error on command window
kindly guide me
Thanks
Reply