Sei sulla pagina 1di 3

DETECCION DE COLORES EN MATLAB

%DETECTA SI HAY CAMARA


function [ camera_name, camera_id, resolution ] = camcheck()
display('Detectando Camara');
a = imaqhwinfo
c = imaqhwinfo('winvideo')
b = c.DeviceIDs

camera_name = char(a.InstalledAdaptors(1))

[p,q] = size(b)

if (q==0)
nohw = errordlg('Hardware no encontrado','ERROR');
else
switch q
case 1
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(1).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(1).SupportedFormats(end));
case 2
hw2 = questdlg('Seleccionar dispositivo para entrada de
video',...
'Seleccionar dispositivo de entrada',...
c.DeviceInfo(1).DeviceName,c.DeviceInfo(2).DeviceName,...
c.DeviceInfo(1).DeviceName);
if strcmp(hw2,c.DeviceInfo(1).DeviceName)==1
camera_name = char(a.InstalledAdaptors);
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(1).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(1).SupportedFormats(end));
else if strcmp(hw2,c.DeviceInfo(2).DeviceName)==1
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(2).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(2).SupportedFormats(end));
end
end
case 3
hw2 = questdlg('Seleccionar dispositivo para entrada de
video',...
'Seleccionar dispositivo de entrada',...
c.DeviceInfo(1).DeviceName,c.DeviceInfo(2).DeviceName,...
c.DeviceInfo(3).DeviceName,c.DeviceInfo(1).DeviceName);
if strcmp(hw2,c.DeviceInfo(1).DeviceName)==1
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(1).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(1).SupportedFormats(end));
end
if strcmp(hw2,c.DeviceInfo(2).DeviceName)==1
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(2).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(2).SupportedFormats(end));
end
if strcmp(hw2,c.DeviceInfo(3).DeviceName)==1
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo(3).DeviceID(end);
resolution =
char(camera_info.DeviceInfo(3).SupportedFormats(end));
end
end
end
end

function x = detectcolor (image,h,s,v)


image = rgb2hsv(image);
image = hsv2binary(image,h,s,v);
cc = bwconncomp(image);
color = regionprops(cc,'centroid');
if isempty(color)
x = [];
else
numPixels = cellfun(@numel,cc.PixelIdxList);
[~,idx] = max(numPixels);
x(1) = color(idx).Centroid(1);
x(2) = color(idx).Centroid(2);
end
end

function output = hsv2binary(input,h,s,v)


output = input(:,:,1)>=h(1)&input(:,:,1)<=h(2)&...
input(:,:,2)>=s(1)&input(:,:,2)<=s(2)&...
input(:,:,3)>=v(1)&input(:,:,3)<=v(2);
end

function [ output_args ] = movems( x,y )


%import java robot class
import java.awt.Robot;
%Creando captura de pantalla
mouse = Robot;
mouse.mouseMove(3.5*x-350,2.7*y-270);
end

function main(num)
%verde: main(1)
%azul: main(2)
%rojo: main(3)
%amarillo: main(4)
switch num
case 1
h = [0.3 0.4];
s = [0.6 1];
v = [0.5 1];
case 2
h = [0.58 0.67];
s = [0.8 1];
v = [0.5 0.6];
case 3
h = [0 0.03];
s = [0.6 1];
v = [0.5 0.7];
case 4
h = [0.12 0.25];
s = [0.85 1];
v = [0.4 0.7];
end
imaqreset;
r=30;
t=0:pi/50:2*pi;
[camera_name,camera_id,format]= camcheck();
video= videoinput(camera_name,camera_id,format);
video.FramesPerTrigge = 1;
video.TriggerRepeat = Inf;
triggerconfig(video,'manual');
video.ReturnedColorspace = 'rgb';
source = getselectedsource(video);
%h = [0.3 0.4];
%s = [0.6 1];
%v = [0.5 1];
start(video);
while (video.FramesAcquired<400)
trigger(video);
frame = getdata(video);
frame = flipdim(frame,2);
x = detectcolor(frame,h,s,v);
imshow(frame);
hold on;
if(x)
z=x(1);
y=x(2);
movems(z,y);
zunit=r*cos(t)+z;
yunit=r*sin(t)+y;
plot(zunit,yunit);
hold off;
end
end
stop(video)
flushdata(video)
clear all
disp('verde: main(1)')
disp('azul: main(2)')
disp('rojo: main(3)')
disp('amarillo: main(4)')