
close all
im = imread('/studier/emner/matnat/ifi/IN5520/h19/undervisningsmateriale/tall_noise10_backgr.png');

figure, imshow(im,[])

%% Try to filter out the high frequent noise using either a convolution low-pass filter or a median filter.
low= repmat(1,3) /9;

smoth = conv2(im,low);

figure,imshow(smoth,[])

%% Threshold
% Thresholding the image using a global threshold does not work as the image has varying background.
% We can either compute the threshold in a local window, or estimate the background and subtract it from the image. 
% Try estimating the background using either a mean filter or a median filter with a very large size (applied to the result from point 2).
% The filter size should be so large that the filtered image only shows the background, not the numbers.

size = 151;
low= repmat(1,size) /size^2;
% Here I use a mean filter, but maybe it would be better with median
very_smooth = conv2(smoth,low,'same');
figure, imshow(very_smooth,[]) , title('Estimate of background')
%%
%figure, imshow(very_smooth,[]),title('Very smothed with kernel ')


diff = very_smooth-smoth;

figure, imshow(diff,[]),title('difference')

%%
max(max(diff))
min(min(diff))

%% global thresh

threshed = diff>25;
figure, imshow(threshed),title('Threshed')

%% region obj

b1 = bwlabel(threshed);
cc=bwconncomp(b1);
ll = labelmatrix(cc);
regim = label2rgb(ll);
figure , imshow(regim), title('region objects')

%% hough

[HT,theta, rho] = hough(threshed);
peaks = houghpeaks(HT,4);
lines = houghlines(threshed,theta,rho,peaks);
figure, imshow(im,[]), hold on
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',5,'Color','green');
end

%% Scatter, and lets look at a histogram
% use Matlab function to find boundingbox,area of 'lables'
st = regionprops(b1, 'BoundingBox', 'Area','ConvexImage','ConvexHull');

areal = [];
xes = []
yes = []
for i =1:length(st)
    %round(number, -1) rounds to nearest tenth
    areal = [areal,round(st(i).Area,-1)]; 
    xes = [xes,st(i).BoundingBox(1)];
    yes = [yes,st(i).BoundingBox(2)];
end

% In the histogram we can see some peaks, but it is not 10 different peaks
% however maybe we can classify some numbers, but not all.
figure,hist(areal,100)

% The scatterplot shows the corner of a bounding box
% and the area of the bounding box, we can see that the
% boundary has area under 100.
figure,gscatter(xes,yes,areal)


%% regionprops
% we use the area of the object to classify the frame, ones and sevens,
% with fairly high accuracy

figure, imshow(threshed,[]), hold on
ones = 0;
sevens = 0;

for i =1:length(st)
    if st(i).Area < 100
        rectangle('Position',[st(i).BoundingBox(1:4)], 'EdgeColor','r','LineWidth',2 )
    elseif st(i).Area < 250
        rectangle('Position',[st(i).BoundingBox(1:4)], 'EdgeColor','g','LineWidth',2 )
        ones = ones + 1;
    elseif st(i).Area < 275
        rectangle('Position',[st(i).BoundingBox(1:4)], 'EdgeColor','b','LineWidth',2 )
        sevens = sevens + 1;
    end
end

%The image is of the 805 first decimals of pi
% Some have been misclassified, but still good
% there are 92 ones
ones
% there are 75 sevens
sevens


