Dynamic text overlay on webcam video

Simple image processing task here. Summary of code below: get video frame from webcam, convert to binary after overlaying a circular mask. Sum 1s and divide by area inside mask to get % of black to white. I'd like to display this number(%) on the video window as it updates with each frame. Currently, the previous value is just overwritten ending up in a mess of numbers on top of each other. Any suggestions? Thank you!
if true
function damagetesting()
%%Reset and initialize
% Pick camera+resolution. Smaller resolution = faster processing
obj = videoinput('winvideo',1,'MJPG_640x480');
obj.FrameGrabInterval=5;
set(obj,'framesperTrigger',1,'TriggerRepeat',Inf);
set(obj,'ReturnedColorSpace','rgb');
im_size=obj.VideoResolution;
[xx,yy]=meshgrid(1:im_size(1),1:im_size(2));
mask=((xx-im_size(1)/2).^2 + (yy-im_size(2)/2).^2 > (im_size(2)/2)^2);
try
start(obj);
hold on;
figure(1);title(['Center lens on template'])
h=imshow(zeros(im_size(2),im_size(1)));
while islogging(obj);
im=getsnapshot(obj);
flushdata(obj);
im(mask)=0;
im=im2bw(im,0.6);
bw=bwlabel(im,8);
set(h,'Cdata',im);
imarea=size(im(1)).*size(im(2));
croparea=sum(mask,1);
croparea=sum(croparea);
lensarea=imarea-croparea;
damagearea=sum(im);
damagearea=sum(damagearea);
text(150,150,['damage area ',num2str(damagearea,'%4.2f')],'Color','m');
stats=regionprops(bw,'BoundingBox','Centroid');
% for(object=1:length(stats))
% cent=stats(object).Centroid;
% plot(cent(1),cent(2),'g+')
% end set(h,'Cdata',im);\
end
catch err
% This attempts to take care of things when the figure is closed
stop(obj);
imaqreset;
disp('Cleaned up')
rethrow(err);
end

Answers (0)

Asked:

on 12 Aug 2016

Edited:

on 12 Aug 2016

Community Treasure Hunt

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

Start Hunting!