clc;
close all;
clear;

% Generating a cloud of random observations
% correlation coefficient 
r = 1;
x = 3 + 0.5*randn(1,1000);
y = r*x + 0.5*randn(1,1000);

% Estimating the covariance between 
C = cov(x,y, 1);

% Computing the eigenvectors and eigenvalues
[V,D] = eig(C);
e1 = V(:, 1);
l1 = D(1, 1);
e2 = V(:, 2);
l2 = D(2, 2);

% Plotting the result
figure(1);
hold on;
plot(x, y, '.'); title('A milky way cloud of observations with its eigenvectors! ');
% Plotting the eigenvectors as a line elements
line([mean(x) mean(x)+sqrt(l1)*e1(1)], [mean(y) mean(y)+sqrt(l1)*e1(2)], 'Color', 'r', 'LineWidth',2);
line([mean(x) mean(x)+sqrt(l2)*e2(1)], [mean(y) mean(y)+sqrt(l2)*e2(2)], 'Color', 'r', 'LineWidth',2);


% Read an image
filename = 'car24.tiff';
img = imread(filename);
img_d = double(img);

[I,map] = rgb2ind(img,128);

% Plotting the cumulative histogram 
figure(3);
bar(cumsum(hist(double(I(:)),255))); title('Cumulative Histogram of the Car Image');

% Scaling the intensities to the interval [0:255]
max_intensity = max(img_d(:));
min_intensity = min(img_d(:));
img_scaled = (img_d-min_intensity)*255/(max_intensity - min_intensity);

figure(4);
subplot(2,1,1);
imshow(I,map); title('Original Image');
subplot(2,1,2);
imshow(uint8(img_scaled),map);title('Image after transformed intensities');

% Saving the image
imwrite(uint8(img_scaled), 'idotyl_scaled.tiff', 'tiff');
