Multi Color Detection in MATLAB

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 :)
GUI Design for Multi Color Detection in MATLAB
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.
×
![]()















































































