Professional Documents
Culture Documents
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
Para permitir el uso del tiempo dentro del ciclo principal del programa se utiliz la
funcin tic-toc, as el conteo en segundos reales realizado por la funcin fue
utilizado en varios procesos dentro del ciclo mencionado:
% Comienza un ciclo de 120 segundos
seg=120;
tic
while toc <= seg
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
Para que el usuario identifique los que el objeto ha sido reconocido como
marcador se enmarca en un paralelogramo verde y su centroide es resaltado con
una cruz:
%Los objetos azules reconocidos son marcados con mrgenes verdes
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',5)
xx=bc(1);
yy=bc(2);
end
2
5
8
1
4
7
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
if x/5*2>xx && xx>=x/5 && y/5*2>yy && yy>=y/5 && band1==0
if toc>=cont1+3
cont1=round(toc);
segValido1=1;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont1==3 && segValido1==1
if turno==1
band1=1;
turno=2;
mat(1,1)=1;
else
turno=1;
band1=2;
mat(1,1)=7;
end
end
end
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
if band6==1
plot(x/10*7,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band6==2
plot(x/10*7,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band7==1
plot(x/10*3,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band7==2
plot(x/10*3,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band8==1
plot(x/10*5,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band8==2
plot(x/10*5,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band9==1
plot(x/10*7,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band9==2
plot(x/10*7,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
hold off
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if mat(1,1)+mat(2,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(2,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5*4 y/5],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(1,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if band1~=0 && band2~=0 && band3~=0 && band4~=0 && band5~=0 &&...
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
band6~=0&&band7~=0 && band8~=0 && band9~=0
title('GATO','FontSize',16,'BackgroundColor','y')
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color',
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color',
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color',
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color',
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color',
break;
end
'r')
'r')
'r')
'r')
'r')
Resultados
Se realizaron pruebas simulando que se utilizaban dos marcadores distintos de
color azul para determinar si eran reconocidos (Fig. 1).
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
Conclusiones
En este proyecto se pusieron en prctica conceptos aprendidos a lo largo del
curso como la captura de frames de una secuencia de video y la inversin de la
imagen proyectada en pantalla para simular el efecto de un espejo y as facilitar la
manipulacin del videojuego. Otros conceptos como el procesamiento en tiempo
real y el filtrado tambin fueron imprescindibles para dar al usuario una
experiencia agradable al hacer que manipulacin adquiriera ms realismo al
interactuar con la interfaz. En conclusin se observa la utilidad del procesamiento
de imgenes al hacer uso de sus conceptos hasta en proyectos de slo ndole
recreacional como lo es un videojuego.
Cdigo fuente
%Delgadillo De la torre Leonardo Misael
%CINVESTAV
%Depto: Ingeniera elctrica
%Seccin: Bioelectrnica
clear all; close all; clc;
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
% Graba las capturas de video
vid = videoinput(camera_name, camera_id, format);
% Las capturas se realizan rapidamente y se retornan matrices RGB
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
% Comienza la adquisicin
start(vid)
% Declaracin de variables
band1=0;
band2=0;
band3=0;
band4=0;
band5=0;
band6=0;
band7=0;
band8=0;
band9=0;
cont1=0;
cont2=0;
cont3=0;
cont4=0;
cont5=0;
cont6=0;
cont7=0;
cont8=0;
cont9=0;
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
turno=1;
mat=zeros(3);
figure('NumberTitle','off','Name','Tic Tac Toe')
% Comienza un ciclo de 120 segundos
seg=120;
tic
while toc <= seg
% Se invierten las coordenadas para la proyeccin de la imagen
set(gca,'xDir','reverse')%,'yDir','reverse')
% Se toma la imagen
data = getsnapshot(vid);
%Se detectan los objetos azules
diff_im = imsubtract(data(:,:,3), rgb2gray(data));
%Se usa un fitro para la adquisicin de los objetos
diff_im = medfilt2(diff_im, [3 3]);
% Convirtiendo el resultado a imagen binaria
diff_im = im2bw(diff_im,0.18);
% Discriminar objetos azules menores a 13000 px de rea (distancia de
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
% reconocimiento)
diff_im = bwareaopen(diff_im,13000);
% Conecta los pixeles en objetos que se compongan de mnimo 8 px
bw = bwlabel(diff_im, 8);
% Se obtiene el marco y el centroide de los objetos identificados
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Se despliega la imagen
imshow(data)
hold on
if turno == 1
title('Jugador 1 "O"','FontSize',15)
else
title('Jugador 2 "X"','FontSize',15)
end
% Creacin de las lneas del gato
[y,x]=size(data(:,:,3));
line([x/5 x/5*4],[y/5*2 y/5*2],'LineWidth',10)
line([x/5 x/5*4],[y/5*3 y/5*3],'LineWidth',10)
line([x/5*3 x/5*3],[y/5 y/5*4],'LineWidth',10)
line([x/5*2 x/5*2],[y/5 y/5*4],'LineWidth',10)
yy=y+1;
xx=x+1;
%Los objetos azules reconocidos son marcados con mrgenes verdes
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',5)
xx=bc(1);
yy=bc(2);
end
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont1==3 && segValido1==1
if turno==1
band1=1;
turno=2;
mat(1,1)=1;
else
turno=1;
band1=2;
mat(1,1)=7;
end
end
end
% Casilla 2
if x/5*3>xx && xx>=x/5*2 && y/5*2>yy && yy>=y/5 && band2==0
if toc>=cont2+3
cont2=round(toc);
segValido1=0;
segValido2=1;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont2==3 && segValido2==1
if turno==1
band2=1;
turno=2;
mat(1,2)=1;
else
turno=1;
band2=2;
mat(1,2)=7;
end
end
end
%Casilla 3
if x/5*4>=xx && xx>=x/5*3 && y/5*2>yy && yy>=y/5 && band3==0
if toc>=cont3+3
cont3=round(toc);
segValido1=0;
segValido2=0;
segValido3=1;
segValido4=0;
segValido5=0;
segValido6=0;
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont3==3 && segValido3==1
if turno==1
band3=1;
turno=2;
mat(1,3)=1;
else
turno=1;
band3=2;
mat(1,3)=7;
end
end
end
% Casilla 4
if x/5*2>xx && xx>=x/5 && y/5*3>yy && yy>=y/5*2 && band4==0
if toc>=cont4+3
cont4=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=1;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont4==3 && segValido4==1
if turno==1
band4=1;
turno=2;
mat(2,1)=1;
else
turno=1;
band4=2;
mat(2,1)=7;
end
end
end
% Casilla 5
if x/5*3>xx && xx>=x/5*2 && y/5*3>yy && yy>=y/5*2 && band5==0
if toc>=cont5+3
cont5=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=1;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
end
end
if round(toc)-cont5==3 && segValido5==1
if turno==1
band5=1;
turno=2;
mat(2,2)=1;
else
turno=1;
band5=2;
mat(2,2)=7;
end
end
% Casilla 6
if x/5*4>=xx && xx>=x/5*3 && y/5*3>yy && yy>=y/5*2 && band6==0
if toc>=cont6+3
cont6=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=1;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont6==3 && segValido6==1
if turno==1
band6=1;
turno=2;
mat(2,3)=1;
else
turno=1;
band6=2;
mat(2,3)=7;
end
end
end
% Casila 7
if x/5*2>xx && xx>=x/5 && y/5*4>=yy && yy>=y/5*3 && band7==0
if toc>=cont7+3
cont7=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=1;
segValido8=0;
segValido9=0;
end
if round(toc)-cont7==3 && segValido7==1
if turno==1
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
band7=1;
turno=2;
mat(3,1)=1;
else
turno=1;
band7=2;
mat(3,1)=7;
end
end
end
% Casilla 8
if x/5*3>xx && xx>=x/5*2 && y/5*4>=yy && yy>=y/5*3 && band8==0
if toc>=cont8+3
cont8=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=1;
segValido9=0;
end
if round(toc)-cont8==3 && segValido8==1
if turno==1
band8=1;
turno=2;
mat(3,2)=1;
else
turno=1;
band8=2;
mat(3,2)=7;
end
end
end
% Casilla 9
if x/5*4>=xx && xx>=x/5*3 && y/5*4>=yy && yy>=y/5*3 && band9==0
if toc>=cont9+3
cont9=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=1;
end
if round(toc)-cont9==3 && segValido9==1
if turno==1
band9=1;
turno=2;
mat(3,3)=1;
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
else
end
end
turno=1;
band9=2;
mat(3,3)=7;
end
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if band1~=0 && band2~=0 && band3~=0 && band4~=0 && band5~=0 &&
band6~=0 &&...
band7~=0 && band8~=0 && band9~=0
title('GATO','FontSize',16,'BackgroundColor','y')
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color', 'r')
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color', 'r')
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
end
end
if round(toc)==120
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color',
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color',
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color',
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color',
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color',
title('GATO, el tiempo se ha acabado','FontSize',16,...
'BackgroundColor','y')
end
% Se detiene la adquisicin
stop(vid);
% Se borran todos los datos de la imagen del buffer
flushdata(vid);
'r')
'r')
'r')
'r')
'r')