Created
May 10, 2017 18:30
-
-
Save bha159/3d58dd045386b4cd7abb7683bdde1ebd to your computer and use it in GitHub Desktop.
MATLAB function for detecting different colours and can be used with in other projects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % | |
% Author : Bharat Kumar % | |
% % | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
%% Initialization | |
function varargout = Colors(varargin) | |
gui_Singleton = 1; | |
gui_State = struct('gui_Name', mfilename, ... | |
'gui_Singleton', gui_Singleton, ... | |
'gui_OpeningFcn', @Colors7_OpeningFcn, ... | |
'gui_OutputFcn', @Colors7_OutputFcn, ... | |
'gui_LayoutFcn', [] , ... | |
'gui_Callback', []); | |
if nargin && ischar(varargin{1}) | |
gui_State.gui_Callback = str2func(varargin{1}); | |
end | |
if nargout | |
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); | |
else | |
gui_mainfcn(gui_State, varargin{:}); | |
end | |
function Colors_OpeningFcn(hObject, eventdata, handles, varargin) | |
handles.output = hObject; | |
guidata(hObject, handles); | |
function varargout = Colors_OutputFcn(hObject, eventdata, handles) | |
varargout{1} = handles.output; | |
function Red_Callback(hObject, eventdata, handles) | |
w=webcam(2); | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); | |
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'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function Green_Callback(hObject, eventdata, handles) | |
w=webcam(2); | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,2), rgb2gray(data)); | |
diff_im = medfilt2(diff_im, [3 3]); | |
diff_im = im2bw(diff_im, 0.05); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function Blue_Callback(hObject, eventdata, handles) | |
w=webcam(2); | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,3), rgb2gray(data)); | |
diff_im = medfilt2(diff_im, [3 3]); | |
diff_im = im2bw(diff_im,0.15); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function White_Callback(hObject, eventdata, handles) | |
vid=webcam(2) | |
while(1) | |
data = snapshot(vid); | |
bwredFrame = im2bw(data(:,:,1), 0.75); % obtain the white component from red layer | |
bwgreenFrame = im2bw(data(:,:,2), 0.75); % obtain the white component from green layer | |
bwblueFrame = im2bw(data(:,:,3), 0.75); % obtain the white component from blue layer | |
binFrame = bwredFrame & bwgreenFrame & bwblueFrame; % get the common region | |
diff_im = medfilt2(binFrame, [3 3]); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function Pink_Callback(hObject, eventdata, handles) | |
w=webcam(2) | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); | |
diff_im = medfilt2(diff_im, [3 3]); | |
diff_im = im2bw(diff_im,0.17); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function Yellow_Callback(hObject, eventdata, handles) | |
w=webcam(2); | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); | |
diff_im = medfilt2(diff_im, [3 3]); | |
diff_im = im2bw(diff_im,0.1); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all | |
function Brown_Callback(hObject, eventdata, handles) | |
w=webcam(2); | |
while(1) | |
data = snapshot(w); | |
diff_im = imsubtract(data(:,:,1), rgb2gray(data)); | |
diff_im = medfilt2(diff_im, [3 3]); | |
diff_im = im2bw(diff_im,0.11); | |
diff_im = bwareaopen(diff_im,300); | |
bw = bwlabel(diff_im, 8); | |
stats = regionprops(bw, 'BoundingBox', 'Centroid'); | |
imshow(data) | |
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', 'yellow'); | |
end | |
hold off | |
end | |
clear all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment