clc;
%Load an image into Octave. The size and class of the output depends on the 
%format of the image. A color image is returned as an MxNx3 matrix. Grayscale 
%and black-and-white images are of size MxN. Multipage images will have an
%additional 4th dimension.  map = colourmap, alpha = transparency
 [ImgClr, map, alpha] = imread ("AK1.jpg");  
%In python + OpenCV: Img = cv2.imread("AK1.jpg"); cv2.imshow(Img);
 subplot(2,2,1); imshow(ImgClr);

%Convert the RGB image to an color-indexed image
 [x, map] = rgb2ind(Img);
 
%Convert the color-indexed image to a gray-intensity matrix: 
%int8 = 2^8 - 1 = 255 colour intensities - 0: darkest, 255 - whitest
 ImgGray = ind2gray(x, map);
 ImgGray = uint8((255 * ImgGray) / max(max(ImgGray)));
 imwrite(ImgGray, "AK1_Gray.jpg", "jpg", "Quality", 75);
 
%In Python + OpenCV: grayImg = cv2.cvtColor(Img, cv2.COLOR_BGR2GRAY)
%imwrite(ImgGray, "AK1_Gray.png", "png", "Quality", 90);
 subplot(2,2,2); imshow(ImgGray);
%------------------------------------------------------------------------------
%Check size of matrix that stores the images
 %size(ImgClr)   %1780 x 1512 x 3 [3 for red, green and blue layers]
 %size(ImgGray)  %1780 x 1512

%Convert the color image to 'red' by converting columns green and blue to zero
 ImgRed = ImgClr; ImgRed(:, :, 2) = 0; ImgRed(:, :, 3) = 0;
 subplot(2,2,3); imshow(ImgRed);

%Convert the color image to 'blue' by converting columns red and green to zero
 ImgBlu = ImgClr; ImgBlu(:, :, 1) = 0; ImgBlu(:, :, 2) = 0;
 subplot(2,2,4); imshow(ImgBlu);
 
 %I = rgb2gray(Img); %Grayscale intensity I = 0.298936*R+0.587043*G+0.114021*B
 %imshow(I)

%------------------------------------------------------------------------------
%conv2: Return the 2-D convolution of A and B. Subsection of the convolution, 
%specified as one of these values: 'full' — Return the full 2-D convolution.
%'same' — Return central part of the convolution, which is the same size as A.
%'valid' — Return parts of convolution that computed without zero-padded edges.

 kSharp = [0, -1, 0; -1, 5, -1; 0, -1, 0];
 %kSharp = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
 ImgSharp = conv2(double(ImgGray), double(kSharp), "same");
 figure(2); 
 subplot(2,2,1); imshow(uint8(ImgSharp), []);

 %Simple box blurr
 kBB = ones(3, 3)/2;
 ImgBBlr = conv2(double(ImgGray), double(kBB), "same");
 subplot(2,2,2); imshow(uint8(ImgBBlr), []);

 %Define the Sobel kernels
 kV = [-1 0 1; -2 0 2; -1  0  1];
 kH = [ 1 2 1;  0 0 0; -1 -2 -1];
 ImgSobV = conv2(ImgGray, kV, "same");
 subplot(2,2,3); imshow(uint8(ImgSobV), []);
 ImgSobH = conv2(ImgGray, kH, "same");
 subplot(2,2,4); imshow(uint8(ImgSobH), []);

%------------------------------------------------------------------------------ 
%To define a kernel for spatial averaging, fill the kernel with ones and divide 
%it by the number of elements in it.
 avg3 = ones(3)/9;
 
 %Edge detection kernel
 kEdge = [-1,-1,-1; -1,8,-1],[; -1,-1,-1];
