clear all; close all; clc
pout = imread('cone.jpg');

seq=0;
width = 400;
dim = size(pout);
pout = imresize(pout,[width*dim(1)/dim(2) width],'bicubic');

K = rgb2gray(pout);

%pout_imadjust = imadjust(pout);
%pout_histeq = histeq(pout);
%pout_adapthisteq = adapthisteq(pout);

%figure, imshow(pout);
%title('Original');
%K = pout;
K = histeq(K);
nextStep = figure, imshow(K)
title('contrast ++');
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg')

K = wiener2(K,[5 5]);
nextStep = figure, imshow(K)
title('adaptive');
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

%I = imread('cone.jpg');
I = im2bw(K,0.2);
nextStep = figure, imshow(I);
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

K = edge(K,'sobel');
nextStep = figure, imshow(K)
title('sobel lines');
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

se = strel('disk',10);
K2 = imclose(K,se);
nextStep = figure, imshow(K2)
title('clean lines');
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

cc = bwconncomp(K2, 8);

regionData = regionProps(cc, 'basic');
maxArea = 0;
maxID=1;
for k=1:cc.NumObjects
    if (regionData(k).Area > maxArea)
        maxArea = regionData(k).Area;
        maxID = k;
    end
end

grain = false(size(K2));
grain(cc.PixelIdxList{maxID}) = true;
nextStep = figure, imshow(grain);
title('Big');
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

K = immultiply(K,grain);
nextStep = figure, imshow(K)
title('small');
seq = seq+1;
saveas(nextStep,strcat('step', num2str(seq)),'jpg') 

rotI = imrotate(K,0,'crop');
[H,T,R] = hough(K,'Theta',0.5);
P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(rotI, T, R, P, 'FillGap',5,'MinLength',7)

outputFig = figure, imshow(pout), hold on
max_len = 0;

slopes = [0];
if length(lines)>1
    for k = 1:length(lines)
        xy = [lines(k).point1; lines(k).point2];
        plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
        
        slope = atan((xy(2,1)-xy(2,2))/(xy(1,1)-xy(1,2)))*180/3.14159;
        search = true;
        pass = true;
        j=1;
        while search            
            if(abs(slope-slopes(j))<3)
               pass = false;
               search = false;
            end
            if j >= length(slopes)
               search = false; 
            end
            j = j+1;
        end
        if pass           
            slopes(length(slopes)+1) = slope;
        end
        
        % Plot beginnings and ends of lines
        plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
        plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
    
        % Determine the endpoints of the longest line segment
        len = norm(lines(k).point1 - lines(k).point2);
        if ( len > max_len)
            max_len = len;
            xy_long = xy;
        end
    end
    % highlight the longest line segment
        plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
end
title('Automatically generated with matlab.');
seq = seq+1;
saveas(outputFig,strcat('step', num2str(seq)),'jpg') 

slopes
imwrite(K,'stepFinal.jpg');

















