This simulation uses the same red object tracking shown in previous posts to interact with a GUI. This GUI controls the media player.
A video...
Source....
clear all;
clc;
close all
%setting the first song
i = 1;
%Accessing media player
actx = actxcontrol('WMPlayer.ocx.7');
%Creating playlist
media(1) = actx.newMedia('C:\Manji\Backstreet Boys - Show Me The Meaning.mp3');
media(2) = actx.newMedia('C:\Manji\Baby.mp3');
media(3) = actx.newMedia('C:\Manji\pussycat dolls - jai ho .mp3');
%getting the number of tunes
imax1 = size(media);
imax = imax1(2);
%Playing the tune
actx.CurrentMedia = media(i);
actx.Controls.play
%Accesing the camera and initializing the variables...
vid = videoinput('winvideo', 1, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
start(vid)
count = 0;
mem = 2;
bc(2,mem) = 0;
bc(1,mem) = 0;
%Setting the graphics
r = ['r', 'g', 'r'];
p1 = 'previous';
p2 ='pause';
p3 = 'next';
I(480,640) = 255;
I(:,:) = 255;
imshow(I)
hold on
rectangle('Position',[320,200,100,50],'FaceColor',r(2))
rectangle('Position',[150,200,100,50],'FaceColor',r(1))
rectangle('Position',[480,200,100,50],'FaceColor',r(3))
a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
a(2) = text(340,220,p2);
set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
%Identifying the blue object
while(1)
data = getsnapshot(vid);
data(:,:,1) = fliplr(data(:,:,1));
data(:,:,2) = fliplr(data(:,:,2));
data(:,:,3) = fliplr(data(:,:,3));
%Color processing...
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, 'Centroid');
if ~isempty(stats)
count = count+1;
bc(1,1) = bc(1,mem);
bc(2,1) = bc(2,mem);
bc(1,mem) = stats(1).Centroid(1);
bc(2,mem) = stats(1).Centroid(2);
%Button logic toggle if the previous cordinate is not inside the box
%and present coordinate is inside the box
if (((bc(1,mem)<420)&&(bc(1,mem)>320))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>420)||(bc(1,mem-1)<320))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
%Play and pause buttun logic
if (r(2) == 'g')
r(2) = 'r';
p2 = 'play';
actx.Controls.pause
rectangle('Position',[320,200,100,50],'FaceColor',r(2))
a(2) = text(340,220,p2);
set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
else
r(2) = 'g';
p2 = 'pause';
actx.Controls.play
rectangle('Position',[320,200,100,50],'FaceColor',r(2))
a(2) = text(340,220,p2);
set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
end
end
if (((bc(1,mem)<250)&&(bc(1,mem)>150))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>250)||(bc(1,mem-1)<150))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
%Previous button logic
r(1) = 'g';
i = i - 1;
rectangle('Position',[150,200,100,50],'FaceColor',r(1))
a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
if(i == 0)
i = imax;
end
if(i == imax+1)
i = 1;
end
actx.CurrentMedia = media(i);
pause(1);
actx.Controls.play
r(1) = 'r';
rectangle('Position',[150,200,100,50],'FaceColor',r(1))
a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
end
if (((bc(1,mem)<580)&&(bc(1,mem)>480))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>580)||(bc(1,mem-1)<480))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
%next button logic
r(3) = 'g';
i = i + 1;
rectangle('Position',[480,200,100,50],'FaceColor',r(3))
a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
if(i == 0)
i = imax;
end
if(i == imax+1)
i = 1;
end
actx.CurrentMedia = media(i);
pause(1);
actx.Controls.play
r(3) = 'r';
rectangle('Position',[480,200,100,50],'FaceColor',r(3))
a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
end
%Plot the point to show the location of cursor
hold on
plot(bc(1,mem),bc(2,mem), '-m+')
if ((~(count == 1))&&(~(count == 0)))
%Remove the previous Cursor plot
plot(bc(1,mem-1),bc(2,mem-1), '-m+','Color','w')
end
end
end
hold off
stop(vid);
flushdata(vid);
clear all
0 comments:
Post a Comment