% training program... acquires RGB/YUYV image data from a the camera and extracts the colour information of
% chosen regions. Makes use of capYUYVnRGB()
clear;
global mode myHandles currRGB myWidth myHeight myOrigX myOrigY run exitflag selectFlag testColourDef;
global colourSpecFile scan4col CAPmov fri mdi;
% open GUI
trainCameraGUI;
% forever...
testColourDef = 0;
selectFlag = 0;
exitflag = 1;
run = 1;
while(exitflag)
% acquire image (camera mode) or run movie (off-line mode)
while(run)
try
% get a handle to the current image data
hRGBData = get(myHandles.Image, 'Children');
% possibly need to delete previously drawn detected objects...
if(length(hRGBData) > 1)
% delete all graphics objects but the last (= the image)
for(ii = 1:length(hRGBData)-1)
delete(hRGBData(ii));
end
hRGBData = hRGBData(end);
end
if(exist('CAPmov', 'var') && ~isempty(CAPmov))
% off-line mode: play movie frame by frame
if(fri > 1 && fri < length(CAPmov))
fri = fri + mdi;
else
mdi = -mdi;
fri = fri + mdi;
end
currRGB = CAPmov(fri).cdata;
set(hRGBData, 'CData', currRGB)
set(myHandles.Title, 'String', ['Displaying frame ' num2str(fri) '/' num2str(length(CAPmov))]);
% give Windows a chance to updated other events too...
pause(0.02)
else
% on-line mode: acquire image data (camera)
currRGB = capImage(mode, 0, myWidth, myHeight, myOrigX, myOrigY);
set(hRGBData, 'CData', currRGB)
end
drawnow;
% optionally process frame
if(testColourDef)
% process frame (no display)...
out = imgProcSilent(currRGB, colourSpecFile, scan4col);
nvr = [out(:).nRegions]; % number of valid regions per colour
if(any(nvr)) % any valid regions at all?
% hold on...
set(myHandles.Image, 'NextPlot', 'add')
% all colours with valid regions
for(j = 1:length(nvr))
kk = out(j).nRegions;
col = out(j).Colour;
% all detected objects... (regions)
while(kk)
cx = out(j).Regions(1, kk); % centroid
cy = out(j).Regions(2, kk);
x1 = out(j).Regions(3, kk); % boundary box
y1 = out(j).Regions(4, kk);
x2 = out(j).Regions(5, kk);
y2 = out(j).Regions(6, kk);
ll = plot(myHandles.Image, [x1 x2 x2 x1 x1], [y1 y1 y2 y2 y1]);
set(ll, 'Color', [1 1 1] - col);
ll = plot(myHandles.Image, cx, cy, 'x');
set(ll, 'Color', [1 1 1] - col);
kk = kk - 1;
end
end
% hold off
set(myHandles.Image, 'NextPlot', 'replace')
end
drawnow
end % testColourDef
catch
% user has closed the GUI -> exit 'gracefully'
capImage(-1);
run = 0;
exitflag = 0;
% exit from m-script here
return;
end
end % run
% service input/output chain (to avoid a ML locked state)
drawnow
% give Windows a chance to catch up with other jobs...
pause(0.3)
if(selectFlag)
% convert rgb image to yuv format (for colour selection)
yuv = rgb2yuv(currRGB);
% select object
figure;
image(currRGB)
axis image;
title('Select object to be tracked...')
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
close(gcf);
% determine object...
point1 = floor(point1(1,1:2)); % extract x and y
point2 = floor(point2(1,1:2));
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
yuvObj = double(yuv(y(2):y(3), x(1):x(2), :)); % (row, column, colour)
% --------------------------------------------------
% generate relevant statistics (mean, std, min, max)
% --------------------------------------------------
% yuv statistics of the seletcted object: min, max (???)
yMin = min(min(yuvObj(:,:,1)));
yMax = max(max(yuvObj(:,:,1)));
uMin = min(min(yuvObj(:,:,2)));
uMax = max(max(yuvObj(:,:,2)));
vMin = min(min(yuvObj(:,:,3)));
vMax = max(max(yuvObj(:,:,3)));
% yuv statistics of the selected object: mean +/- 1 STD
yAve = round(mean(mean(yuvObj(:,:,1))));
uAve = round(mean(mean(yuvObj(:,:,2))));
vAve = round(mean(mean(yuvObj(:,:,3))));
yStd = round(std(double(reshape(yuvObj(:,:,1), 1, numel(yuvObj(:,:,1))))));
uStd = round(std(double(reshape(yuvObj(:,:,2), 1, numel(yuvObj(:,:,2))))));
vStd = round(std(double(reshape(yuvObj(:,:,3), 1, numel(yuvObj(:,:,3))))));
numberSTD = 2;
yMin = max([yAve - numberSTD*yStd, yMin]);
yMax = min([yAve + numberSTD*yStd, yMax]);
uMin = max([uAve - numberSTD*uStd, uMin]);
uMax = min([uAve + numberSTD*uStd, uMax]);
vMin = max([vAve - numberSTD*vStd, vMin]);
vMax = min([vAve + numberSTD*vStd, vMax]);
% display colour info histograms
hist(myHandles.Y, reshape(yuvObj(:,:,1), 1, numel(yuvObj(:,:,1))), 0:255);
ax = axis(myHandles.Y);
axis(myHandles.Y, [0 255 0 ax(4)]);
set(myHandles.Y, 'NextPlot', 'add')
plot(myHandles.Y, [max(1, yMin) min(254, yMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.Y, 'NextPlot', 'replace')
hist(myHandles.U, reshape(yuvObj(:,:,2), 1, numel(yuvObj(:,:,2))), 0:255);
ax = axis(myHandles.U);
axis(myHandles.U, [0 255 0 ax(4)]);
set(myHandles.U, 'NextPlot', 'add')
plot(myHandles.U, [max(1, uMin) min(254, uMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.U, 'NextPlot', 'replace')
hist(myHandles.V, reshape(yuvObj(:,:,3), 1, numel(yuvObj(:,:,3))), 0:255);
ax = axis(myHandles.V);
axis(myHandles.V, [0 255 0 ax(4)]);
set(myHandles.V, 'NextPlot', 'add')
plot(myHandles.V, [max(1, vMin) min(254, vMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.V, 'NextPlot', 'replace')
drawnow;
% create filtered output...
currRGB = filterYUVimage(yuv, currRGB, yMin, yMax, uMin, uMax, vMin, vMax);
% replace image data by filtered version...
set(hRGBData, 'CData', currRGB)
% display selected colour ranges (thresholds)
myThresholds = ['(' num2str(double(yMin)) ':' num2str(double(yMax)) ...
', ' num2str(double(uMin)) ':' num2str(double(uMax)) ...
', ' num2str(double(vMin)) ':' num2str(double(vMax)) ')'];
disp('-------------------------------------------------------------------')
disp('(Ymin:Ymax, Umin:Umax, Vmin:Vmax)');
disp(myThresholds);
disp('-------------------------------------------------------------------')
% display colour ranges in edit box of the GUI
set(myHandles.Ranges, 'String', myThresholds);
% disable 'selectFlag'
selectFlag = 0;
% set visibility state of 'select' button to 'off'
set(myHandles.SelectButton, 'Visible', 'off');
end % selectFlag
end % exitflag