1
function RGB = Face_tracking(VideoFile)
2
vid_obj = VideoReader(strrep(VideoFile,"_nc",""));
3
VideoFile = NameVideoFile(VideoFile);
5
FDetect = vision.CascadeObjectDetector('FrontalFaceCART','MergeThreshold',10); %Viola-Jones Algorithm
6
%EyeDetect = vision.CascadeObjectDetector('EyePairSmall','UseROI', true);
8
videoFrame = readFrame(vid_obj);
9
bbox = step(FDetect, videoFrame);
11
videoFrame = insertShape(videoFrame, "rectangle", bbox);
12
figure(); imshow(videoFrame); title("Detected face");
14
%Попытки сегментации изображений
16
% Face_cluster = mean_shift(videoFrame,40,2,6);
19
% L = superpixels(videoFrame,1000);
20
% points = bbox2points(bbox);
21
% roi = poly2mask(points(:,1),points(:,2),size(L,1),size(L,2));
22
% Face_cluster = grabcut(videoFrame,L,roi);
23
% maskedImage = videoFrame;
24
% maskedImage(repmat(~Face_cluster,[1 1 3])) = 0;
27
% [BW,maskedImage] = segmentImage(videoFrame);
29
[~, skmap] = skinmap(videoFrame);
31
% Display the Skinmap data and draw the bounding box around the face.
32
figure(); imshow(skmap); title('Skinmap data');
33
rectangle('Position',bbox(1,:),'LineWidth',2,'EdgeColor',[1 1 0]);
35
% Skin = bsxfun(@times, videoFrame, uint8(skmap));
36
% Skin_gray = rgb2gray(Skin);
37
% I_filtered = edge(Skin_gray, 'sobel');
38
% figure; imshow(I_filtered);
40
noseDetector = vision.CascadeObjectDetector('Nose', 'UseROI', true,'MergeThreshold',10);
41
noseBBox = step(noseDetector, videoFrame, bbox(1,:));
42
%find center of nose Haar box
43
% nx = noseBBox(1) + noseBBox(3)/2;
44
% ny = noseBBox(2) + noseBBox(4)/2;
48
%eyeBBox = step(EyeDetect, videoFrame, bbox(1,:));
49
% rectangle('Position',noseBBox(1,:),'LineWidth',3,'EdgeColor',[1 1 0]);
50
% rectangle('Position',noseBBox(1,:),'LineWidth',3,'EdgeColor',[1 1 0]);
52
% plot(nx,ny, 'Marker','+','Color','red','MarkerSize',10);
54
% Create a tracker object.
55
tracker = vision.HistogramBasedTracker;
57
% Initialize the tracker histogram using the pixels from the nose.
58
initializeObject(tracker, skmap, noseBBox(1,:));
60
% Create a video player object for displaying video frames.
61
%videoPlayer = vision.VideoPlayer;
64
numframes = int16(fix(vid_obj.FrameRate*vid_obj.Duration));
65
% f = waitbar(0,'1','Name','Approximating pi...','CreateCancelBtn','setappdata(gcbf,''canceling'',1)');
66
f = waitbar(numframes, "������ ��������� ������");
67
% Track the face over successive video frames until the video is finished.
69
% ���������� ������� RGB
70
RGB = zeros(numframes, 3);
71
% file=fopen(strcat(VideoFile, '_RGB', '.txt'),'w');
73
while hasFrame(vid_obj)
75
waitbar(frame/numframes, f, sprintf("��������� ������ [%d/%d] - %s", frame, numframes, VideoFile))
77
% Extract the next video frame
78
videoFrame = readFrame(vid_obj);
80
% A_lin = rgb2lin(videoFrame);
82
% illuminant = illumgray(A_lin,percentiles);
83
% B_lin = chromadapt(A_lin,illuminant,'ColorSpace','linear-rgb');
84
% videoFrame = lin2rgb(B_lin);
94
[~, skmap] = skinmap(videoFrame);
96
% Track using the skinmap data
97
bbox = step(tracker, skmap);
99
% Apply the mask to the image
100
Skin = bsxfun(@times, videoFrame, uint8(skmap));
101
Face = imcrop(Skin, bbox);
105
RGB(frame, 1) = sum(red)/length(red);
107
green = nonzeros(green);
108
RGB(frame, 2) = sum(green)/length(green);%mean(green./length(green));
110
blue = nonzeros(blue);
111
RGB(frame, 3) = sum(blue)/length(blue);%mean(blue./length(blue));
114
% disp(RGB(frame, 1));
115
% disp(RGB(frame, 2));
116
% disp(RGB(frame, 3));
118
% if ((RGB(frame, 1) ~= 0) && (RGB(frame, 2) ~= 0) && (RGB(frame, 3) ~= 0))
119
% fprintf(file,'%f;%f;%f\n',RGB(frame, 1), RGB(frame, 2), RGB(frame, 3));
122
% Insert a bounding box around the object being trackenamesd
123
%videoOut = insertObjectAnnotation(Skin,'rectangle',bbox,'Face');
124
% Display the annotated video frame using the video player object
125
%step(videoPlayer, videoOut);
126
% disp(sprintf("[%d/%d]", frame, numframes));
131
% writematrix([RGB(:, 1).',RGB(:, 2).',RGB(:, 3).'], strcat(VideoFile, '.txt'));
134
%release(videoPlayer);