im = double(rgb2gray(imread('landsat.jpg')));
imrose = double(imread('rose.jpg'));

himg = histc(im(:),0:255);
himg = 255*himg/sum(himg);

h2img = zeros(256,1);

for i = 1:size(himg,1)
  h2img(i) = sum(himg(1:i));
end

im2 = zeros(size(im));
for i = 1:size(im,1)
  for j = 1:size(im,2)
    im2(i,j) = h2img(im(i,j)+1 );
  end
end

im2 = im2 /255;
imwrite(im2,'1_hist_equ.png','png');

%contrast strech
dimg = zeros(size(im));

dimg = (im - min(im(:))) * 255/double(max(im(:))-min(im(:)) );

dimg = dimg /255;
imwrite(dimg,'2_contrast_strech.png','png');

%convolution

conv1 = imfilter(imrose, fspecial('average', [3 3]));%tune able
conv2 = imfilter(imrose, fspecial('average', [7 7]));%tune able
conv3 = imfilter(imrose, fspecial('average', [7 1]));%tune able
conv4 = imfilter(imrose, fspecial('average', [1 7]));%tune able
conv5 = imfilter(imrose, fspecial('average', [11 11]));%tune able

conv1 = conv1/255;
conv2 = conv2/255;
conv3 = conv3/255;
conv4 = conv4/255;
conv5 = conv5/255;

imwrite(conv1,'3_conv3_3.png','png');
imwrite(conv2,'3_conv7_7.png','png');
imwrite(conv3,'3_conv7_1.png','png');
imwrite(conv4,'3_conv1_7.png','png');
imwrite(conv5,'3_conv11_11.png','png');

%perspective projection
nn = deg2rad(20);

X = [0,0,0; 1,0,0; 1,1,0; 0,1,0; 0 0 0; 0,0,1; 0,1,1; 0,1,0; 0,1,1; 1,1,1; 1,1,0; 1,1,1; 1,0,1; 1,0,0; 1,0,1; 0,0,1];
%rx = eye(3);
rx = [1 0 0; 0 cos(nn) sin(nn); 0 -sin(nn) cos(nn)];
ry = eye(3);
rz = eye(3);
R = rz*ry*rx;
T = [0;0;5;];

K = [800,0,250; 0, 800,250; 0,0,1];
%K = [5,0,.5; 0,5,.5;0,0,1];
%K = eye(3);

x = project(X,R,T,K);

x = x./[x(:,3),x(:,3),x(:,3)];

%hold on
%scatter3(X(:,1), X(:,2), X(:,3))
%scatter3(x(:,1), x(:,2), x(:,3))

%line(X(:,1),X(:,2),X(:,3));
line(x(:,1),x(:,2),x(:,3));




%%%%%RBT

A = [2,1;4,1;4,6;2,1];
tt = deg2rad(30);
rot = [cos(tt) -sin(tt); sin(tt), cos(tt)];
rat = [sin(tt) cos(tt); cos(tt), -sin(tt)];

A2 = A*rot;
A3 = A*rat;

%hold on
%line(A(:,1),A(:,2))
%line(A2(:,1),A2(:,2))
%line(A3(:,1),A3(:,2))

