MATLAB Answers

Why is my code starts fast and then eventually slows down?

1 view (last 30 days)
Francisco Mendoza
Francisco Mendoza on 26 Dec 2014
Edited: Geoff Hayes on 27 Dec 2014
Well, I have a problem with this script that take video from my webcam and process it the image for detect some objects with basic colors... it's hardly fast, at first, but after some seconds it gets so slowly... can you help me?
The code is the next, and it's divided by principal function and other that
process..
% Script para detectar los colores rojo, azul y amarillo.
% Francisco Mendoza Ruiz, estudiante de ingenierÌa electrÛnica
% de la universidad autÛnma del Carmen (UNACAR).
%%-------------------------PRINCIPAL--------------------------------------
function vid_tiem_ejec(num_tomas)
vid = videoinput( 'macvideo' , 1 );
set( vid , 'ReturnedColorSpace' , 'RGB' );
triggerconfig( vid , 'manual' );
start(vid);
for i=1:num_tomas
I = getsnapshot(vid);
I = imresize( I , 0.5);
colores(I)
end
stop(vid);
clear all
close all
end
%%---------------------------IMAGE PROCES PER FRAME---------------------------------
function colores(I);
r = I( : , : , 1);
g = I( : , : , 2);
b = I( : , : , 3);
rojo = r-g-b;
verd = g-b-r;
azul = b-r-g;
amar = g-r/2-b/2;
rojo = rojo > 35;
verd = verd > 35;
azul = azul > 20;
amar = amar > 20;
rojo = bwareaopen( rojo , 700 );
verd = bwareaopen( verd , 700 );
azul = bwareaopen( azul , 700 );
amar = bwareaopen( amar , 700 );
det_roj = regionprops ( rojo , 'Centroid' , 'Area' , 'BoundingBox' );
det_ver = regionprops ( verd , 'Centroid' , 'Area' , 'BoundingBox' );
det_azu = regionprops ( azul , 'Centroid' , 'Area' , 'BoundingBox' );
det_amar = regionprops ( amar , 'Centroid' , 'Area' , 'BoundingBox' );
[ id_roj , ~ ] = size(det_roj);
[ id_ver , ~ ] = size(det_ver);
[ id_azu , ~ ] = size(det_azu);
[ id_amar , ~ ] = size(det_amar);
num_obj = id_roj + id_ver + id_azu + id_amar;
pts_roj = zeros ( 1 , id_roj );
pts_ver = zeros ( 1 , id_ver );
pts_azu = zeros ( 1 , id_azu );
pts_amar = zeros ( 1 , id_amar );
if ~isempty(id_roj) % CondiciÛn de existencia del objeto.
for i=1:id_roj
pts_rojx(i) = det_roj(i).Centroid(1);
pts_rojy(i) = det_roj(i).Centroid(2);
end
end
if ~isempty(id_ver)
for i=1:id_ver
pts_verx(i) = det_ver(i).Centroid(1);
pts_very(i) = det_ver(i).Centroid(2);
end
end
if ~isempty(id_azu)
for i=1:id_azu
pts_azux(i) = det_azu(i).Centroid(1);
pts_azuy(i) = det_azu(i).Centroid(2);
end
end
if ~isempty(id_amar)
for i=1:id_amar
pts_amarx(i) = det_amar(i).Centroid(1);
pts_amary(i) = det_amar(i).Centroid(2);
end
end
imshow(I);
if ~isempty(id_roj) && id_roj(1)~=0
hold on;
plot( pts_rojx , pts_rojy, '-b+' );
for i=1:id_roj
rectangle( 'Position' , det_roj(i).BoundingBox, 'Edge' , 'g' , 'LineWidth' , 2);
end
end
if ~isempty(id_azu) && id_azu(1)~=0
hold on;
plot( pts_azux , pts_azuy, '-r+' );
for i=1:id_azu
rectangle( 'Position' , det_azu(i).BoundingBox, 'Edge' , 'r' , 'LineWidth' , 2);
end
end
if ~isempty(id_amar) && id_amar(1)~=0
hold on;
plot( pts_amarx , pts_amary, '-m+' );
for i=1:id_amar
rectangle( 'Position' , det_amar(i).BoundingBox, 'Edge' , 'm' , 'LineWidth' , 2);
end
end
if ~isempty(id_ver) && id_ver(1)~=0
hold on;
plot( pts_verx , pts_very, '-y+' );
for i=1:id_ver
rectangle( 'Position' , det_ver(i).BoundingBox, 'Edge' , 'y' , 'LineWidth' , 2);
end
end
drawnow;
end
%%____________________________________________________________
This is a preview of the output:
I'll be grateful because your support. Thank you all.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by