ssa

Форк
0
/
Face_tracking.m 
134 строки · 4.8 Кб
1
function RGB = Face_tracking(VideoFile)
2
    vid_obj = VideoReader(strrep(VideoFile,"_nc",""));
3
    VideoFile = NameVideoFile(VideoFile);
4
    
5
    FDetect = vision.CascadeObjectDetector('FrontalFaceCART','MergeThreshold',10); %Viola-Jones Algorithm
6
    %EyeDetect = vision.CascadeObjectDetector('EyePairSmall','UseROI', true);
7

8
    videoFrame = readFrame(vid_obj);
9
    bbox = step(FDetect, videoFrame);
10

11
    videoFrame = insertShape(videoFrame, "rectangle", bbox);
12
     figure(); imshow(videoFrame); title("Detected face");
13

14
    %Попытки сегментации изображений
15
    %Метод 1
16
    % Face_cluster = mean_shift(videoFrame,40,2,6);
17
    %
18
    %Метод 2
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;
25
    %  imshow(maskedImage)
26
    %Метод 3
27
    % [BW,maskedImage] = segmentImage(videoFrame);
28

29
    [~, skmap] = skinmap(videoFrame);
30

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]);
34

35
    % Skin = bsxfun(@times, videoFrame, uint8(skmap));
36
    % Skin_gray = rgb2gray(Skin);
37
    % I_filtered = edge(Skin_gray, 'sobel');
38
    % figure; imshow(I_filtered);
39

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;
45
    nx = bbox(1,1);
46
    ny = bbox(1,2);
47

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]);
51
%     hold on;
52
%     plot(nx,ny, 'Marker','+','Color','red','MarkerSize',10);
53

54
    % Create a tracker object.
55
    tracker = vision.HistogramBasedTracker;
56

57
    % Initialize the tracker histogram using the pixels from the nose.
58
    initializeObject(tracker, skmap, noseBBox(1,:));
59

60
    % Create a video player object for displaying video frames.
61
    %videoPlayer  = vision.VideoPlayer;
62

63
    frame = 0;
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.
68
    
69
    % ���������� ������� RGB
70
    RGB = zeros(numframes, 3);
71
%     file=fopen(strcat(VideoFile, '_RGB', '.txt'),'w'); 
72
    
73
    while  hasFrame(vid_obj)
74
        frame = frame + 1;
75
        waitbar(frame/numframes, f, sprintf("��������� ������ [%d/%d] - %s", frame, numframes, VideoFile))
76

77
        % Extract the next video frame
78
        videoFrame = readFrame(vid_obj);
79
        
80
%             A_lin = rgb2lin(videoFrame);
81
%     percentiles = 10;
82
%     illuminant = illumgray(A_lin,percentiles);
83
%     B_lin = chromadapt(A_lin,illuminant,'ColorSpace','linear-rgb');
84
%     videoFrame = lin2rgb(B_lin);
85
         
86
%         if frame < 200
87
%              continue
88
%         end
89
%         if frame == 201
90
%             break
91
%         end
92
%         disp(frame);
93

94
        [~, skmap] = skinmap(videoFrame);
95

96
        % Track using the skinmap data
97
        bbox = step(tracker, skmap);
98

99
        % Apply the mask to the image
100
        Skin = bsxfun(@times, videoFrame, uint8(skmap));
101
        Face = imcrop(Skin, bbox);
102

103
        red = Face(:,:,1); 
104
        red = nonzeros(red);
105
        RGB(frame, 1) = sum(red)/length(red);
106
        green = Face(:,:,2);
107
        green = nonzeros(green);
108
        RGB(frame, 2) = sum(green)/length(green);%mean(green./length(green));
109
        blue = Face(:,:,3);
110
        blue = nonzeros(blue);
111
        RGB(frame, 3) = sum(blue)/length(blue);%mean(blue./length(blue));
112
       
113
%         disp(frame);
114
%         disp(RGB(frame, 1));
115
%         disp(RGB(frame, 2));
116
%         disp(RGB(frame, 3));
117
        
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));
120
%         end 
121

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));
127
    end
128
    delete(f);
129
    
130
% fclose(file); 
131
%     writematrix([RGB(:, 1).',RGB(:, 2).',RGB(:, 3).'], strcat(VideoFile, '.txt'));
132
end 
133
% Release resources
134
%release(videoPlayer);

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.