clc
clearvars;
close all;

% Load video
video = VideoReader('dog_bump.mp4');
disp(video);

% Skip ahead (can leave commented)
% frame = read(video, 150); % Read the 50th frame
% imshow(frame); % Display the extracted frame

%% Read and show a video frame-by-frame

if 0
    while hasFrame(video)
        frame = readFrame(video); % Read the next frame
        frame = imrotate(frame, -90);
        imshow(frame); 
        pause(1/video.FrameRate); % Play in real-time
    end
end

%% Do image manipulation frame-by-frame

% Set HSV values
brown_H_low = 0/255;
brown_H_high = 20/255;

brown_S_low = 95/255;   
brown_S_high = 120/255;  

brown_V_low = 10/255;   
brown_V_high = 240/255;

if 0
    figure;
    while hasFrame(video)
        frame = readFrame(video); % Read the next frame
        frame = imrotate(frame, -90);
        
        % Convert to HSV color space
        hsvImg = rgb2hsv(frame);
        
        % Extract HSV channels
        H = hsvImg(:,:,1); % Hue
        S = hsvImg(:,:,2); % Saturation
        V = hsvImg(:,:,3); % Value
    
        color_mask = (hsvImg(:,:,1) >= brown_H_low) & (hsvImg(:,:,1) <= brown_H_high);
        satur_mask = (hsvImg(:,:,2) >= brown_S_low) & (hsvImg(:,:,2) <= brown_S_high);
        value_mask = (hsvImg(:,:,3) >= brown_V_low) & (hsvImg(:,:,3) <= brown_V_high);
    
        dogMask = color_mask & satur_mask & value_mask;

        % Clean mask
        dogMask = bwareaopen(dogMask, 50);

        se = strel('disk', 30, 0);
        dogMask = imdilate(dogMask, se);

        dogMask = imfill(dogMask, 'holes');

        mask_erode = imerode(dogMask, se);

        % Mask and save image
    
        frame_copy = frame; 
        frame_copy(repmat(~dogMask, [1 1 3])) = 0;
        
        imshow(frame_copy); 
        pause(1/video.FrameRate); % Play in real-time
    end

    return;
end


%% Create a video and save it

% Create video writer
outputVideo = VideoWriter('just_the_puppy'); 
outputVideo.FrameRate = video.FrameRate;

open(outputVideo); 

% Set HSV values
brown_H_low = 5/255;
brown_H_high = 20/255;

brown_S_low = 95/255;   
brown_S_high = 120/255;  

brown_V_low = 10/255;   
brown_V_high = 240/255;

thresh_values = [brown_H_low, brown_H_high ; ...
          brown_S_low, brown_S_high ; ...
          brown_V_low, brown_V_high];

frame_num = 0;
while hasFrame(video)
    frame = readFrame(video);
    frame = imrotate(frame, -90);
    
    % Convert to HSV color space
    hsvImg = rgb2hsv(frame);

    % Putting masks into a for loop
    % masks = zeros(size(hsvImg));
    dogMask = ones(size(hsvImg, [1,2]));
    for i = 1:3
        mask = (hsvImg(:,:,i) >= thresh_values(i,1)) & ...
               (hsvImg(:,:,i) <= thresh_values(i,2));
        % masks(:,:,i) = imerode(mask, se);
        dogMask = dogMask & mask;
    end

    % dogMask = masks(:,:,1) & masks(:,:,2) & masks(:,:,3);
    
    % Clean mask
    dogMask = bwareaopen(dogMask, 50);

    se = strel('disk', 30, 0);
    dogMask = imdilate(dogMask, se);

    dogMask = imfill(dogMask, 'holes');

    mask_erode = imerode(dogMask, se);

    % Save frame
    frame_copy = frame; 
    frame_copy(repmat(~dogMask, [1 1 3])) = 0;
    
    imshow(frame_copy); 

    writeVideo(outputVideo, frame_copy); 
    disp(frame_num)
    frame_num = frame_num + 1; % Display the frame number
end

close(outputVideo); % Finalize video file
disp('Processed video saved.');
