% training program... acquires RGB/YUYV image data from a the cameras (stereoscopic vision)
% and extracts the colour information of chosen regions. Makes use of capImageStereo()
% open display and histogram window
trainFig = figure;
set(gcf, 'Position', [20 50 950 600]);
set(gcf, 'MenuBar', 'none');
set(gcf, 'Name', 'Click FREEZE to stop data acquisition...');
% check if we're running with live images or not
try
% try to capture a frame... (RGB)
[rgbDummy1, rgbDummy2] = capImageStereo(0);
clear rgbDummy CAPmov;
catch
error('Not enough cameras found on the bus');
end
% forever...
exitflag = 1;
while(exitflag)
% nothing has been selected yet -> display (empty) colour info histograms
subplot(6, 2, 2, 'replace');
axis([0 255 0 1]);
ylabel('y');
subplot(6, 2, 4, 'replace');
axis([0 255 0 1]);
ylabel('u');
subplot(6, 2, 6, 'replace');
axis([0 255 0 1]);
ylabel('v');
% nothing has been selected yet -> display (empty) colour info histograms
subplot(6, 2, 8, 'replace');
axis([0 255 0 1]);
ylabel('y');
subplot(6, 2, 10, 'replace');
axis([0 255 0 1]);
ylabel('u');
subplot(6, 2, 12, 'replace');
axis([0 255 0 1]);
ylabel('v');
% display original
subplot(6, 2, [1 3 5], 'replace');
axis image xy;
xlabel('Camera 1')
subplot(6, 2, [7 9 11], 'replace');
axis image xy;
xlabel('Camera 2')
% (re-)set loop control variable 'run' to active
run = 1;
% push button to stop data acquisition...
h = uicontrol('Style', 'pushbutton', 'String', 'Freeze',...
'Position', [20 200 70 50], 'Callback', 'run=0;');
% acquire image (camera mode) or run movie (off-line mode)
while(run)
% on-line mode: acquire image data (both cameras)
[rgb1, rgb2] = capImageStereo(0);
sp = subplot(6, 2, [1 3 5], 'replace');
image(rgb1);
axis image ij;
sp = subplot(6, 2, [7 9 11], 'replace');
image(rgb2);
axis image ij;
drawnow;
end
% delete 'freeze' button
delete(h);
% convert rgb image to yuv format (for colour selection)
yuv{1} = rgb2yuv(rgb1);
yuv{2} = rgb2yuv(rgb2);
% store rgb images in cell array
rgb = {rgb1, rgb2};
clear rgb1 rgb2;
for(im = 1:2)
% select object
set(trainFig, 'Name', ['Select object to be tracked (image ' num2str(im) ')']);
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
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)];
% convert cell array to normal array
curryuv = yuv{im};
yuvObj = double(curryuv(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,prod(size(yuvObj(:,:,1)))))));
uStd = round(std(double(reshape(yuvObj(:,:,2),1,prod(size(yuvObj(:,:,2)))))));
vStd = round(std(double(reshape(yuvObj(:,:,3),1,prod(size(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
subplot(6, 2, 2+(im-1)*6); % 2 or 8
hist(reshape(yuvObj(:,:,1),1, prod(size(yuvObj(:,:,1)))), 0:255);
ax = axis;
axis([0 255 0 ax(4)]);
hold on;
plot([max(1, yMin) min(254, yMax)], 0.5*[ax(4) ax(4)], 'r');
hold off;
ylabel('y');
subplot(6, 2, 4+(im-1)*6); % 4 or 10
hist(reshape(yuvObj(:,:,2),1, prod(size(yuvObj(:,:,2)))), 0:255);
ax = axis;
axis([0 255 0 ax(4)]);
hold on;
plot([max(1, uMin) min(254, uMax)], 0.5*[ax(4) ax(4)], 'r');
hold off;
ylabel('u');
subplot(6, 2, 6+(im-1)*6); % 6 or 12
hist(reshape(yuvObj(:,:,3),1, prod(size(yuvObj(:,:,3)))), 0:255);
ax = axis;
axis([0 255 0 ax(4)]);
hold on;
plot([max(1, vMin) min(254, vMax)], 0.5*[ax(4) ax(4)], 'r');
hold off;
ylabel('v');
drawnow;
% create filtered output...
[obj_r,obj_c] = find(...
curryuv(:,:,1)>=yMin & curryuv(:,:,1)<=yMax & ...
curryuv(:,:,2)>=uMin & curryuv(:,:,2)<=uMax & ...
curryuv(:,:,3)>=vMin & curryuv(:,:,3)<=vMax);
% re-display RGB image indicating selected colour
rgbFILT = rgb{im};
% % redisplay original image with selected colour changed to black
% for(i = 1:length(obj_r))
% rgbFILT(obj_r(i),obj_c(i),:) = uint8(0);
% end
% % redisplay original image with selected colours changed to the complement of the average
% for(i = 1:length(obj_r))
% %rgbFILT(obj_r(i),obj_c(i),:) = [255 255 255] - [rAve gAve bAve];
% end
% split vector into even and odd elements... (shaded display)
idx = 1:length(obj_r);
oddx = find(mod(idx,2));
evex = setdiff(idx, oddx);
% redisplay original image with selected colours replaced by shades of black and white
for(i = 1:length(evex))
rgbFILT(obj_r(evex(i)),obj_c(evex(i)),:) = uint8(255);
end
% redisplay original image without selected colours
for(i = 1:length(oddx))
rgbFILT(obj_r(oddx(i)),obj_c(oddx(i)),:) = uint8(0);
end
subplot(6, 2, [1 3 5]+(im-1)*[6 6 6], 'replace');
image(rgbFILT);
axis image ij;
% display selected colour ranges (thresholds)
disp('-------------------------------------------------------------------')
disp('(Ymin:Ymax, Umin:Umax, Vmin:Vmax)');
disp(['(' num2str(double(yMin)) ':' num2str(double(yMax)) ...
', ' num2str(double(uMin)) ':' num2str(double(uMax)) ...
', ' num2str(double(vMin)) ':' num2str(double(vMax)) ')']);
disp('-------------------------------------------------------------------')
% reset control variable 'run'
run = 1;
% push button to continue with data acquisition...
h1 = uicontrol('Style', 'pushbutton', 'String', 'CONTINUE',...
'Position', [20 170 70 40], 'Callback', 'run=0;');
% push button to continue with data acquisition...
h2 = uicontrol('Style', 'pushbutton', 'String', 'EXIT',...
'Position', [20 230 70 40], 'Callback', 'exitflag=0;');
drawnow;
end
while(run & exitflag)
title('Selected object');
drawnow;
end
% remove push buttons
delete(h1);
delete(h2);
end
% close figure
close(gcf);