ollo5 = imread('ollo5.jpg');
ollo5_gray = rgb2gray(ollo5);
figure, imshow(ollo5_gray);

ollo6 = imread('ollo6.jpg');
ollo6_gray = rgb2gray(ollo6);
figure, imshow(ollo6_gray);


ollo5_canny = edge(ollo5_gray, 'canny', 0.1);
ollo6_canny = edge(ollo6_gray, 'canny', 0.1);

ollo5_canny_dil = bwmorph(ollo5_canny, 'dilate', 10);
ollo6_canny_dil = bwmorph(ollo6_canny, 'dilate', 10);

[ollo5_points_x, ollo5_points_y, v] = find(ollo5_canny>0);
[ollo6_points_x, ollo6_points_y, v] = find(ollo6_canny>0);

%ollo5_chull = convhull(ollo5_points_x, ollo5_points_y);
%ollo5_chull = convhull(ollo5_points_x, ollo5_points_y);

ollo5_bwhull = bwconvhull(ollo5_canny, 'union', 8);
ollo6_bwhull = bwconvhull(ollo6_canny, 'union', 8);

ollo5_hull_contour = bwmorph(ollo5_bwhull, 'remove');
ollo6_hull_contour = bwmorph(ollo6_bwhull, 'remove');

[hull5_points_x, hull5_points_y] = find(ollo5_hull_countour>0);
[hull6_points_x, hull6_points_y] = find(ollo6_hull_countour>0);

hull5_points = [hull5_points_x, hull5_points_y];
hull6_points = [hull6_points_x, hull6_points_y];

[R2,T2] = icp(hull5_points, hull6_points,50,10);
ollo6_points_trafo = hull6_points;
npoints = length(ollo6_points_trafo);

t_ollo6_points_i = int32(ollo6_points_trafo);

reg_ollo5 = ollo5;
reg_ollo5(:,:,1) = ollo5_gray(:,:);
reg_ollo5(:,:,2) = ollo6_canny(:,:) * 255;
reg_ollo5(:,:,3) = 0;

for i = 1:npoints;
    if ( t_ollo6_points_i(i,1)> 0 &&  t_ollo6_points_i(i,2)> 0 );
        reg_ollo5(t_ollo6_points_i(i,1), t_ollo6_points_i(i,2), 3) = 255;
    end
end;

imshow(reg_ollo5);


%ollo5_points = [ollo5_points_x, ollo5_points_y];
%ollo6_points = [ollo6_points_x, ollo6_points_y];

%trafo = cp2tform(ollo5_points, ollo6_points, 'affine');

%ollo6_points_trafo = zeros(size(ollo6_points));

%npoints = length(ollo6_points);

