%% Setup

clear all; close all; clc; %#ok<CLALL> 

% MATLAB has a few built-in images that you can load to test with
rgb_pears = imread('pears.png');
rgb_chips = imread('coloredChips.png');

% Different formats can be read by the same function, but the data type
% that results can vary. 
I_rice = imread('rice.png');
I_coins = imread('coins.png'); % Note that imread takes a variety of different extensions, but the data type isn't necessarily the same. 

% make a double image from a uint8 image.
d_rice = double(I_rice); d_rice=d_rice./max(d_rice(:));


%
%% Image quantification and regions of interest

% A region of interest is defined either as a closed polygon (i.e. last
% point == first point) or a binary mask.

% If you know certain properties ahead of time, you can define an ROI
% programmatically.
figure;
imagesc(I_coins); colormap gray; axis equal tight off;

% This is known from the previous notebook and is manually copied here.
cent = [96.2,146];
dia = 58;
hax_circle = drawcircle(gca,'Center',cent,'Radius', dia/2,'Color','y');

% You can also interactively draw an ROI totally freehand (click and drag)...
figure;
imagesc(I_coins); colormap gray; axis equal tight off;
%% uncomment this line
% hax_free = drawfreehand('Color','g');

% Or assisted, snapping to edges in the image as you click multiple times. 
figure;
imagesc(I_coins); colormap gray; axis equal tight off;
%% uncomment this line
hax_assist = drawassisted('Color','r');

%

% If you have an ROI object, you can convert it into a binary mask and use
% that to do ROI-specific processing for each ROI
circleMask = createMask(hax_circle);
f = @(x)imsharpen(x,'Amount',5);
I_coin_filt = roifilt2(I_coins,circleMask,f);

figure;
montage({I_coins,I_coin_filt}); title('Effects of ROI-specific sharpen');



%
%% Case Study - Selectively filtering ROIs with different operations.

%load('./coin_regionprops.mat');

% Assume we know that the dimes are <50 pixels in size, but no less than 40
% pixels in size, and that the nickels are larger than 50 pixels in size,
% but no more than 60 pixels in size.
I_coin_bin = imbinarize(I_coins,'adaptive');
figure;
imagesc(I_coin_bin);

I_coin_BW = imfill(imbinarize(I_coins,'adaptive'),'holes');
dimes = bwpropfilt(I_coin_BW,'EquivDiameter',[40 50]);
nickels = bwpropfilt(I_coin_BW,'EquivDiameter',[50 60]);

% We will take the separated images and use their region props to create
% ROIs programmatically.
dimeprops = regionprops(dimes,'all');
nickelprops = regionprops(nickels,'all');

N_dimes = numel(dimeprops);
N_nickels = numel(nickelprops);

dime_ROIs = cell(N_dimes,1);
nickel_ROIs = cell(N_nickels,1);

dime_masks = cell(N_dimes,1);
nickel_masks = cell(N_nickels,1);

% Display image and ROIs for each dime. Also create a binary mask for each.
imagesc(I_coins); hold on; axis equal tight off; colormap gray;

for k = 1:N_dimes
    cent = dimeprops(k).Centroid;
    dia = dimeprops(k).EquivDiameter;
    dime_ROIs{k} = drawcircle(gca,'Center',cent,'Radius', dia/2,'Color','g');
    dime_masks{k} = createMask(dime_ROIs{k});
end

% Repeat for nickels. 
for k = 1:N_nickels
    cent = nickelprops(k).Centroid;
    dia = nickelprops(k).EquivDiameter;
    nickel_ROIs{k} = drawcircle(gca,'Center',cent,'Radius', dia/2,'Color','r');
    nickel_masks{k} = createMask(nickel_ROIs{k});
end

% We now have different-colored regions for the different coins.
% We can also use the ROIs to apply filters and functions to each region.
% Concatenate the masks along their 3rd axis.
all_dimes = any(cat(3,dime_masks{:}),3);
all_nickels = any(cat(3,nickel_masks{:}),3);

figure; 
% Shows the binary masks for each class, overlaid in different color channels. 
% Nickels are in the magenta channel, dimes in the green. Notice no
% overlay, and also notice that the small outlier region (region 8 from the
% segmentation notebook) is not present.
imshowpair(all_dimes,all_nickels); 


% Create a function to apply to the nickel ROIs, then apply it.
f = @(x)imsharpen(x,'Amount',5);
I_coin_filt_nickels = roifilt2(I_coins,all_nickels,f);

% Same for the dimes.
f = @(x)imgaussfilt(x,4);
I_coin_filt_dimes = roifilt2(I_coins,all_dimes,f);

% Apply to both images.
I_coin_filt_nickels_and_dimes = roifilt2(I_coin_filt_nickels,all_dimes,f);

figure;
montage({I_coins,I_coin_filt_nickels,I_coin_filt_dimes, I_coin_filt_nickels_and_dimes})


