This simulation in matlab allows you to paint in a matlab figure with the help of of a blue colored cap attached to your finger. This will also also contain a small button which can be used as an eraser and as a brush whenever needed. For more details see here. So here's how to do it.....
Here is a demo video showing this...
And here's the matlab code...
%Accesing the camera and initializing the variables...
vid = videoinput('winvideo', 2, '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;
r = 'g';
p = 'paint on';
k = getsnapshot(vid);
k(:,:,:) = 255;
imshow(k)
hold on
%Identifying the blue object
while(vid.FramesAcquired<=2000)
data = getsnapshot(vid);
data(:,:,1) = fliplr(data(:,:,1));
data(:,:,2) = fliplr(data(:,:,2));
data(:,:,3) = fliplr(data(:,:,3));
%Showing the Button
rectangle('Position',[100,200,50,10],'FaceColor',r)
text(105,205,p)
%Color processing...
diff_im = imsubtract(data(:,:,3), 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)<150)&&(bc(1,mem)>100))&&((bc(2,mem)<210)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>150)||(bc(1,mem-1)<100))||((bc(2,mem-1)>210)||(bc(2,mem-1)<200))))
if (r == 'r')
r = 'g';
p = 'paint on';
else
r = 'r';
p = 'paint off';
end
end
%Plot the point to show the location of cursor
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
if ((~(count == 1))&&(~(count == 0))&&(r=='g'))
%Drawing a line between the previous coordinate and next
%coordinate.....
line([bc(1,mem) bc(1,(mem-1))],[bc(2,mem) bc(2,(mem-1))],'Color','r','LineWidth',2);
end
end
hold off
stop(vid);
flushdata(vid);
clear all
0 comments:
Post a Comment