%% prepare workspace
clear; close all; clc;
format COMPACT;

%% File import

%  Use "dicomBrowser" app to explore dicom images

%image info
imfinfo('bu.jpg')

pic = imread('bu.jpg');
% Notice that the file has 3 dimentions!
whos
size(pic)

% show image
imshow(pic);


%% Image crop

croppedIm = imcrop(pic,[300, 200, 800, 700]);
figure
imshow(croppedIm)

% interactive crop: imcrop(pic)

%% Center Crop Image 
targetSize = [500 500];
r = centerCropWindow2d(size(pic),targetSize);
croppedIm2 = imcrop(pic, r);
imshow(croppedIm2)

%% Change image resolution

pic2=imresize(imresize(pic,1/32),32);
imshow(pic2)

%% Convert to grayscale image

grayIm = rgb2gray(pic);
figure
imshow(grayIm)

%% Convert to black and white

bwIm = im2bw(pic, 0.4);
figure
imshow(bwIm)

%% Color component
figure,imshow(pic(:,:,3))

%% Edge detection

f=fspecial('laplacian')
cf=filter2(f,grayIm);
imshow(cf/100)  % Laplacian filter only
f1=fspecial('log')

cf1=filter2(f1,grayIm);
figure,imshow(cf1/100)  % Laplacian + Gaussian ("log") filter


%%
fe1=edge(grayIm);
imshow(fe1)
%% a better result if we do it for each color component
f1=edge(pic(:,:,1));
f2=edge(pic(:,:,2));
f3=edge(pic(:,:,3));
fe2=f1| f2 | f3;
figure,imshow(fe2)
