-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
45 lines (43 loc) · 2.99 KB
/
main.m
File metadata and controls
45 lines (43 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
current_folder = 'C:\Users\tosta\OneDrive\Área de Trabalho\Tosta et al. 2023\';
nstains = 2; %Number of columns in the W matrix (H&E stain)
%Target image processing
target = imread('C:\Users\tosta\OneDrive\Área de Trabalho\Tosta et al. 2023\target.bmp'); %Parameter: Target image for normalization
magnification = 20; %For patch selection (this must be adjusted according to the image)
mini_batch_target = 0.1; %For W estimation (this is a parameter than can be adjusted)
%The output of the representative_patch function is the selected image
%patch. The input includes the current_folder and the file name, so the
%patch can be written to verify the function result. In the current folder,
%a subfolder "max_entropy" will have the results
target_grid = representative_patch(target,magnification,current_folder,'target');
Wtarget_initialization = W_initialization(target_grid,nstains);
[~,target_grid_forW] = beer_lambert_transformation(target_grid);
lambda_grid = CS(imhist(rgb2gray(target_grid)), 50, 3, 0.25);
Wtarget = W_estimation(Wtarget_initialization, target_grid_forW, nstains, lambda_grid, round(mini_batch_target*size(target_grid_forW,1)));
%All above functions are used for the W matrix estimation. The following
%calls are necessary for the H density map estimation
lambda = CS(imhist(rgb2gray(target)), 50, 3, 0.25);
[target_forH,target_forW] = beer_lambert_transformation(target);
%This function needs the SPAMS toolbox
[Htarget,~] = H_estimation(target_forH, target_forW, nstains, lambda, Wtarget, size(target,1), size(target,2));
%Source images processing
source_folder = 'C:\Users\tosta\OneDrive\Área de Trabalho\Tosta et al. 2023\samples';
source_files = dir(fullfile(source_folder, '*.tif'));
original_files_names = natsortfiles({source_files.name});
magnification = 40; %For patch selection (this must be adjusted according to the dataset)
mini_batch_source = 0.1; %For W estimation (this is a parameter than can be adjusted)
for n = 1:numel(source_files)
source = imread(char(fullfile(source_folder, original_files_names(1,n))));
[~,name,~] = fileparts(char(fullfile(source_folder, original_files_names(1,n))));
%The W matrix estimation
source_grid = representative_patch(source,magnification,current_folder,name);
Wsource_initialization = W_initialization(source_grid,nstains);
[~,source_grid_forW] = beer_lambert_transformation(source_grid);
lambda_grid = CS(imhist(rgb2gray(source_grid)), 50, 3, 0.25);
Wsource = W_estimation(Wsource_initialization, source_grid_forW, nstains, lambda_grid, round(mini_batch_source*size(source_grid_forW,1)));
%The H density map estimation
lambda = CS(imhist(rgb2gray(source)), 50, 3, 0.25);
[source_forH,source_forW] = beer_lambert_transformation(source);
[His,~] = H_estimation(source_forH, source_forW, nstains, lambda, Wsource, size(source,1), size(source,2));
[norm_version] = norm_image_generation(source,Htarget,Wtarget,His);
imwrite(norm_version,char(strcat(current_folder,name,'.png')));
end