clc;
clearvars;
close all;

%Load image
img = imread('dog_lift_smol.png'); 

figure;
imshow(img);

%Convert to greyscale
gray_img = rgb2gray(img);

figure;

imshow(gray_img);


%Extracting Color Channels 
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);

figure;

imshow(blue); 


%% Make image appear green
size(img);
img2 = zeros(size(img), 'uint8'); %The 'uint' is important
img2(:,:,3) = blue;

figure;
imshow(img2); 


%% Image ehancement and modification

% Contrast Enhancement for Visibility
enhanced = imadjust(gray_img);

figure;
imshow(enhanced); 


%Noise Reduction
filtered = medfilt2(gray_img, [3 3]);

figure;
imshow(filtered);


% Edge Detection
edges = edge(gray_img, 'Canny');

figure;
imshow(edges);


%% Thresholding for sky detection
threshold = blue > 165; % Adjust value as needed

% imbinarize(blue);

figure;
imshow(threshold);

% Remove holes
mask = ~bwareaopen(threshold, 500);
% mask = imfill(threshold, 'hole');

figure;
imshow(mask);


%% Mask the image

img_copy = img; % Copy original image
img_copy(repmat(~mask, [1 1 3])) = 0;

figure;
imshow(img_copy);