% The following code segments might be helpful...

%%% An important note on the first assignment:
%%% When the input image is a color image and if you apply histogram equalization
%%%  on each (red,green,blue) channel separately, you may (and probably will) notice
%%%  changes on the perceived colors... (Why? --> Relative color ratios may change!..)
%%%  So figure out how to do histogram equalization for color images..

%
a = imread('tire.tif');
a = double(a);
figure; imshow(a,[]);

[I,J] = find(a==0); % returns row and column indices where a is equal to zero..

[I,J] = find(a<=128); % returns row and column indices where a is less than or equal to zero..

%
b = a;
b(b>128) = 255;
figure; imshow(b,[]);

% Another way of doing the same thing given above...
b = a;
[height,width] = size(a);
for i=1:height,
    for j=1:width,
        if (a(i,j)>128)
            b(i,j) = 255;
        end
    end
end


% Now let me create a pixel-mapping function:
T = zeros(256,1);
T(100:120) = 255;
figure; plot(T);
b = a; 
b = T(b+1);
figure; imshow(b,[]);

%%% Now, a face detection example
a = imread('faces2.bmp'); % I didnt put the faces2.bmp on the web... try on another image...
figure; imshow(a);  
a_red = a(:,:,1);
a_green = a(:,:,2);
a_blue = a(:,:,3);

figure; imshow(a_red);
figure; imshow(a_green);
figure; imshow(a_blue);

b = zeros(size(a_red));
b( (a_red>240)&(a_green>150)&(a_green<240)&(a_blue>150)&(a_blue<220) ) = 1;
figure; imshow(b,[]);

c = a;
c(:,:,1) = double(a_red).*b;
c(:,:,2) = double(a_green).*b;
c(:,:,3) = double(a_blue).*b;
figure; imshow(c,[]);
figure; imshow(uint8(c));

%%% threshold-based segmentation...
clear all; close all;
a = imread('rice.png'); 
%a = imread('coins.png');  
figure; imshow(a);
figure; imhist(a);

b = zeros(size(a));
b(a>110) = 1;
figure; imshow(b);

%%%% divide image into sub-regions and then do segmentation...



%%% watershed
clear all;
a = imread('rice.png'); 
%a = imread('coins.png'); 
a = double(a); 
figure; imshow(a,[]);
l = watershed(a);
figure; imshow(l,[]);