%img = imread('pecs1.jpg');
%img = imread('ilonka.jpg');
%img = imread('vaza.jpg');
%img = imread('shore.jpg');
img = imread('haz_auto.jpg');
img_gray = rgb2gray(img);
imshow(img_gray);
imhist(img_gray);
[count, x] = imhist(img_gray);
count2 = count;

count2(:) = count(:); for i = 11:245; count2(i) = sum(count(i-10:i+10)) / 21; end

local_mini = 1:length(count);

local_mini(:) = 0; for i= 10:250; if ( count2(i-1) > count2(i) && count2(i) < count2(i+1) ) local_mini(i) = 1; end; end

plot(x, count, 'r-', x,count2, 'b-');

local_mins = find(local_mini>0);
labels =uint8(1:256);

for j=1:length(local_mins ); if (j == 1) labels(1:local_mins (j)) = 1; else labels(local_mins (j-1):local_mins (j)) = j; end; end
labels(local_mins(length(local_mins))+1:256) = labels(local_mins(length(local_mins)))+1;


img_label = img_gray; 
dims = size(img_gray);
x = 1:dims(1); 
y = 1:dims(2);

for i = 1:dims(1);
    for j = 1:dims(2);
        img_label(i,j) = labels(img_gray(i,j)+1);
    end;
end;
img_color = label2rgb(img_label, 'jet');
figure;
subplot(1,2,1); imshow(img_gray);
subplot(1,2,2); imshow(img_color);
