clc; clear all; close all;
img = imread('lena_norm.png');
[height,width,depth]=size(img);
if (depth>1)
  img=rgb2gray(img);
  depth=1;
end

thresh_1=logical(zeros(height,width,'uint8'));
thresh_1(img>100)=1;

thresh_2=zeros(height,width,'uint8');
thresh_2(:,:)=127;
thresh_2(img>150)=255;
thresh_2(img<100)=0;

adap_thresh=logical(zeros(height,width,'uint8'));
derivace=abs(diff(diff(imhist(img))));
level=find(derivace == max(derivace));
if (length(level)>1)
  level=max(level);
end
adap_thresh(img>level)=1;

figure
subplot(2,2,1);imshow(thresh_1);title('Jeden pevny prah');
subplot(2,2,2);imshow(thresh_2);title('Dva pevne prahy');
subplot(2,2,3);imshow(adap_thresh);title('Adaptivni prahovani');
subplot(2,2,4);imshow(adaptivethresh(img,16));title('Adaptivni prahovani dle his.');
