Segmentación de colores con MATLAB Dr. Alejandro Aceves Seminario del Proyecto de Investigación en robótica Humanoide Objetivo • Al finalizar el taller, el participante podrá analizar imágenes desde MATLAB para hacer procesos simpes de segmentación de colores y posterior mente detectar objetos de colores de una secuencia de video. Lista de Material • • • Una computadora Una web-cam MATLAB con Image Adquisition MATLAB >> ver ------------------------------------------------------------------------------------MATLAB Version 7.9.0.529 (R2009b) MATLAB License Number: 161051 Operating System: Microsoft Windows Vista Version 6.1 (Build 7600) Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode ------------------------------------------------------------------------------------MATLAB Version 7.9 (R2009b) Simulink Version 7.4 (R2009b) Control System Toolbox Version 8.4 (R2009b) Data Acquisition Toolbox Version 2.15 (R2009b) Fuzzy Logic Toolbox Version 2.2.10 (R2009b) Genetic Algorithm and Direct Search Toolbox Version 2.4.2 (R2009b) Image Acquisition Toolbox Version 3.4 (R2009b) Image Processing Toolbox Version 6.4 (R2009b) Model Predictive Control Toolbox Version 3.1.1 (R2009b) Neural Network Toolbox Version 6.0.3 (R2009b) Optimization Toolbox Version 4.3 (R2009b) Real-Time Windows Target Version 3.4 (R2009b) Real-Time Workshop Version 7.4 (R2009b) Real-Time Workshop Embedded Coder Version 5.4 (R2009b) Robust Control Toolbox Version 3.4 (R2009b) Signal Processing Blockset Version 6.10 (R2009b) Signal Processing Toolbox Version 6.12 (R2009b) Simulink Control Design Version 3.0 (R2009b) Spline Toolbox Version 3.3.7 (R2009b) Statistics Toolbox Version 7.2 (R2009b) Symbolic Math Toolbox Version 5.3 (R2009b) System Identification Toolbox Version 7.3.1 (R2009b) Video and Image Processing Blockset Version 2.8 (R2009b) Drivers >> info=imaqhwinfo info = InstalledAdaptors: MATLABVersion: ToolboxName: ToolboxVersion: {'coreco' 'winvideo'} '7.9 (R2009b)' 'Image Acquisition Toolbox' '3.4 (R2009b)' Drivers >> info=imaqhwinfo('winvideo') info = AdaptorDllName: [1x81 char] AdaptorDllVersion: '3.4 (R2009b)' AdaptorName: 'winvideo' DeviceIDs: {[1]} DeviceInfo: [1x1 struct] Drivers >> info=imaqhwinfo('winvideo',1) info = DefaultFormat: 'YUY2_176x144' DeviceFileSupported: 0 DeviceName: 'USB 2821 Device' DeviceID: 1 ObjectConstructor: 'videoinput('winvideo', 1)' SupportedFormats: {1x8 cell} Drivers >> info.SupportedFormats' ans = 'YUY2_176x144' 'YUY2_352x480' 'YUY2_352x576' 'YUY2_480x480' 'YUY2_480x576' 'YUY2_640x480' 'YUY2_720x480' 'YUY2_720x576' Variable de VIDEO >> vid=videoinput('winvideo',1,'YUY2_720x480') Summary of Video Input Object Using 'USB 2821 Device'. Acquisition Source(s): composite and svideo are availa Acquisition Parameters: 'composite' is the current sele 10 frames per trigger using the 'YUY2_720x480' video data to be Grabbing first of every 1 frame Log data to 'memory' on trigger Trigger Parameters: 1 'immediate' trigger(s) on STA Status: Waiting for START. 0 frames acquired since startin 0 frames available for GETDATA. Preview >> preview(vid) Una Imagen Imagen obtenida de Image Processing Toolbox For Use with MATLAB User’s guide v2.0, pp 1-8 Espacio de Colores RGB YUV Y - Luminance (brightness) U (Cb) - Blueness (Blue vs. Green) V (Cr) - Redness (Red vs. Green) Imágenes obtenidas de los siguientes sitios: http://commons.wikimedia.org/wiki/File:Cubo_YUV_con_las_capas_de_color.png http://i.msdn.microsoft.com/Aa511283.color02%28en-us,MSDN.10%29.png http://en.wikipedia.org/wiki/Color_space Espacio de Color >> vid.VideoResolution ans = 720 480 >> set(vid,'ReturnedColorSpace','rgb') Capturar una imagen >> foto=getsnapshot(vid); >> figure(1) >> imshow(foto) Pixeles Elegir pixeles de un mismo color >> pixels=impixel(foto) pixels = 185 203 204 189 206 200 181 195 197 203 220 214 196 210 210 Pixeles >> figure(2) >> plot3(pixels(:,1),pixels(:,2),pixels(:,3),'r*') >> axis([0 255 0 255 0 255]) Pixeles >> maxR=max(pixels(:,1)) maxR = 194 >> minR=min(pixels(:,1)) minR = 178 >> maxG=max(pixels(:,2)) maxG = 214 >> minG=min(pixels(:,2)) minG = 192 >> maxB=max(pixels(:,3)) maxB = 209 >> minB=min(pixels(:,3)) minB = 187 Voxel http://www.mathworks.com/matlabcentral/fileexchange/3280 Voxel VOXEL function to draw a 3-D voxel in a 3-D plot Usage: voxel(start,size,color,alpha); will draw a voxel at 'start' of size 'size' of color 'color' and transparency alpha (1 for opaque, 0 for transparent) Default size is 1 Default color is blue Default alpha value is 1 start is size the color is (type a three element vector [x,y,z] a three element vector [dx,dy,dz] a character string to specify color 'help plot' to see list of valid colors) Clases de Colores voxel([minR minG minB], [maxR-minR maxG-minG maxB-minB],'b',0.8) Clasificador de pixeles Segmentar (Azul) >> >> >> >> >> >> >> >> >> >> >> foto=getsnapshot(vid); figure(1); imshow(foto); pixels=impixel(foto); minR=min(pixels(:,1)); minG=min(pixels(:,2)); minB=min(pixels(:,3)); maxR=max(pixels(:,1)); maxG=max(pixels(:,2)); maxB=max(pixels(:,3)); segmenta figure(2); imshow(foto); Segmentar (Verde) >> >> >> >> >> >> >> >> >> >> >> foto=getsnapshot(vid); figure(1); imshow(foto); pixels=impixel(foto); minR=min(pixels(:,1)); minG=min(pixels(:,2)); minB=min(pixels(:,3)); maxR=max(pixels(:,1)); maxG=max(pixels(:,2)); maxB=max(pixels(:,3)); segmenta figure(2); imshow(foto); Segmentar (Blanco) >> >> >> >> >> >> >> >> >> >> >> foto=getsnapshot(vid); figure(1); imshow(foto); pixels=impixel(foto); minR=min(pixels(:,1)); minG=min(pixels(:,2)); minB=min(pixels(:,3)); maxR=max(pixels(:,1)); maxG=max(pixels(:,2)); maxB=max(pixels(:,3)); segmenta figure(2); imshow(foto); Programa foto=getsnapshot(vid); figure(1); pixels=impixel(foto); minR=min(pixels(:,1)); minG=min(pixels(:,2)); minB=min(pixels(:,3)); maxR=max(pixels(:,1)); maxG=max(pixels(:,2)); maxB=max(pixels(:,3)); figure(2); axis([0 255 0 255 0 255]); %voxel([minR minG minB],[maxR-minR maxG-minG maxB-minB],'b',0.8); %voxel([minR minG minB],[maxR-minR maxG-minG maxB-minB],'g',0.8); voxel([minR minG minB],[maxR-minR maxG-minG maxB-minB],'w',0.8); res=vid.VideoResolution; for x=1:res(2) for y=1:res(1) if foto(x,y,1)>= minR && foto(x,y,1)<=maxR && ... foto(x,y,2)>= minG && foto(x,y,2)<=maxG && ... foto(x,y,3)>= minB && foto(x,y,3)<=maxB, % % foto(x,y,1)=0; foto(x,y,2)=255; foto(x,y,3)=0; foto(x,y,1)=0; foto(x,y,2)=0; foto(x,y,3)=255; foto(x,y,1)=255; foto(x,y,2)=255; foto(x,y,3)=255; else foto(x,y,1)=0; foto(x,y,2)=0; foto(x,y,3)=0; end; end; end; figure(3); imshow(foto) Segmentación por colores Referencias [1] Image Processing Toolbox For Use with MATLAB User’s guide v2.0 [2] VOXEL function to draw a 3-D voxel in a 3-D plot, Suresh Joel Apr 15,2003, Updated Feb 25, 2004 http://www.mathworks.com/matlabcentral/fileexchange/3 280