function CS6640_Week3

% Quiz 1, Q1
figure(1)
clf
A = zeros(200,200);
A(90:120,30:170) = 1;
imshow(A);
Ar = imrotate(A,25);
imshow(Ar);

cell = imread('cell.tif');
[M,N,P] = size(cell)

cell_not = imcomplement(cell);
imshow(cell);
imshow(cell_not);

min(min(imadd(cell,cell_not)))

cell_add = imadd(cell,100);
max(max(cell))
max(max(cell_add))
imshow(cell_add);

mask = zeros(M,N);
mask(1:floor(M/2),1:floor(N/2)) = 1;
mask = uint8(mask);
cell_mask = cell.*mask;
imshow(cell_mask);

tom = imread('Tom.jpg');
tom = imrotate(tom,-90);
subplot(1,2,1);
imshow(tom);
tom_5 = immultiply(tom,5);
subplot(1,2,2);
imshow(tom_5);
cam = imread('cameraman.tif');
subplot(1,2,1);
imshow(cam);
subplot(1,2,2);
imshow(immultiply(cam,5));

% Thresholding
cell_100 = uint8(cell<100);
figure(1)
clf
imshow(cell)
figure(2)
clf
imshow(cell_100==1);
surf(cell);
combo(cell,cell_100==1);

[X,tabX] = imread('trees.tif');
trees = uint8(255*ind2rgb(X,tabX));
treesg = rgb2gray(trees);
clf
imshow(treesg);
combo(trees,treesg>130);
% MapProcessing
map1 = imread('map1.jpg');
map1g = rgb2gray(map1);
imshow(map1g>220);
combo(map1,~bwmorph(bwmorph(map1(:,:,1)<200&map1(:,:,3)>200,...
    'erode',3),'dilate',15));

% Log transform
figure(1)
clf
c = 255/log(1+(exp(1)-1)*max([0:255]));
lolo1 = c*log(1+(exp(1)-1)*([0:255]));
plot([0:255],lolo1,'k');
hold on
axis square
c = 255/log(1+(exp(5)-1)*max([0:255]));
lolo5 = c*log(1+(exp(5)-1)*([0:255]));
plot([0:255],lolo5,'k');
c = 255/log(1+(exp(10)-1)*max([0:255]));
lolo10 = c*log(1+(exp(10)-1)*([0:255]));
plot([0:255],lolo10,'k');
xlabel('I_{input}');
ylabel('I_{output}');
axis square
text(130,130,'increasing {\sigma}');
plot([120,20],[120,250],'k');
plot([20,20],[250,240],'k');
plot([20,30],[250,250],'k');
figure(2);
clf
h = hist(double(cam(:)),256);
plot(h);
cam1 = CS6640_log_trans(double(cam),0,1);
h1 = hist(double(cam1(:)),256);
hold on
plot(h1,'r');

clf
subplot(1,2,1);
imshow(cam);
I2 = CS6640_log_trans(cam,0,2);
subplot(1,2,2);
imshow(mat2gray(I2));

photo350 = imread('photo350.JPG');
photo350g = rgb2gray(photo350);
imo = CS6640_log_trans(photo350g,0,0.1);
figure(1)
clf
subplot(2,1,1);
imshow(photo350g);
subplot(2,1,2);
imshow(mat2gray(imo));

% exponential transform
figure(1);
clf
lolo1 = CS6640_exp_trans([0:255],0,0.01);
plot(lolo1,'k');
hold on
axis square
lolo5 = CS6640_exp_trans([0:255],0,.02);
plot(lolo5,'k');
lolo10 = CS6640_exp_trans([0:255],0,.05);
plot(lolo10,'k');

% gamma transform
figure(3);
clf
subplot(1,2,1);
imshow(cam);
subplot(1,2,2);
I3 = CS6640_gamma(cam,255,1/3);
imshow(uint8(I3));

x = [0:255];
I3 = CS6640_log_trans(x,255,1);
plot(I3);
hold on
I3 = CS6640_log_trans(x,255,4);
plot(I3);
I3 = CS6640_log_trans(x,255,10);
plot(I3);

I3 = CS6640_gamma(x,255,1);
plot(I3);
I3 = CS6640_gamma(x,255,5);
plot(I3);
I3 = CS6640_gamma(x,255,10);
plot(I3);

% histograms
tomg = rgb2gray(tom);
h = imhist(tomg);
figure(2);
subplot(1,2,1);
plot(h);
subplot(1,2,2);
combo(tom,tomg<160);
combo(tom,tomg>160);

% histogram equalization
tomhe = histeq(tomg);
hee = imhist(tomhe);
subplot(1,2,1);
hold on
plot(hee,'r');
clf
subplot(1,2,1);
imshow(tomg);
subplot(1,2,2);
imshow(tomhe);

% color image histogram equalization

photo350_hsv = rgb2hsv(photo350);
V = histeq(photo350_hsv(:,:,3));
photo350_hsv(:,:,3) = V;
photo350r = hsv2rgb(photo350_hsv);
figure(1);
subplot(2,1,1);
imshow(photo350);
subplot(2,1,2);
imshow(photo350r);

% Color histogram
hc = CS6640_hist_color(tom,8);
figure(1);
clf
plot(hc);
hcp = hc>0.02;
hcp3 = reshape(hcp,8,8,8);
figure(2)
clf
plot3(0,0,0,'w.');
hold on
plot3(10,10,10,'w.');
for r = 1:8
    for g = 1:8
        for b = 1:8
            if hcp3(r,g,b)==1
                plot3(r,g,b,'ro');
            end
        end
    end
end
hc1 = CS6640_hist_color(photo350,8);
hc1p = hc1>0.02;
hc1p3 = reshape(hc1p,8,8,8);
for r = 1:8
    for g = 1:8
        for b = 1:8
            if hc1p3(r,g,b)==1
                plot3(r,g,b,'ko');
            end
        end
    end
end
figure(1)
hold on
plot(hc1,'r');

% Kullback-Leibler divergence
CS6640_KL_div(hc,hc)
CS6640_KL_div(hc,hc1) 

tch = 0;
