Diff of /matlab/add_dataset.m [000000] .. [b758a2]

Switch to unified view

a b/matlab/add_dataset.m
1
function [len_M, num_true] = add_dataset(image_file, csv_file, dataset_folder)
2
3
% parameters
4
d = 10;
5
d2 = d*d;
6
N = 101; % must be odd
7
8
R = (N-1)/2;
9
S = R + d;
10
11
% create list of coordinates within a circle of radius d
12
k = 1;
13
for i=(1-d):(d-1)
14
    for j=(1-d):(d-1)
15
       if i*i + j*j < d2
16
           region(k,:) = [i j];
17
           k = k + 1;
18
       end
19
    end
20
end 
21
22
% load image and csv files
23
A = imread(image_file);
24
M = csvread(csv_file);
25
26
[A_height, A_width, ~] = size(A);
27
28
% extend the image by mirroring (just for the positive samples)
29
D = padarray(A,[S S],'symmetric');
30
31
% find the centroids
32
len_M = size(M,1);
33
P = zeros(len_M,2);
34
for i=1:len_M;
35
    tmp = M(i,:);
36
    tmp = tmp(tmp~=0);
37
    len_tmp = size(tmp,2);
38
    sum_X = 0;
39
    sum_Y = 0;
40
    for j=1:len_tmp/2
41
        sum_X = sum_X + tmp(2*j-1);
42
        sum_Y = sum_Y + tmp(2*j);
43
    end
44
    mean_X = 2*sum_X/len_tmp;
45
    mean_Y = 2*sum_Y/len_tmp;
46
    P(i,:) = [mean_Y mean_X];
47
end
48
M = round(P);
49
50
% create positive dataset
51
% extract many images within a region around each mitosis
52
len_region = size(region,1);
53
B = cell(1,len_M*len_region);
54
for i=1:len_M
55
    for j=1:len_region
56
        y1 = M(i,1) + region(j,1) - R + S;
57
        y2 = M(i,1) + region(j,1) + R + S;
58
        x1 = M(i,2) + region(j,2) - R + S;
59
        x2 = M(i,2) + region(j,2) + R + S;
60
        %{
61
        if y1 >= 1 && y2 <= A_height && x1 >= 1 && x2 <= A_width
62
            B{j+(i-1)*len_region} = A(y1:y2, x1:x2, :);
63
        end
64
        %}
65
        B{j+(i-1)*len_region} = D(y1:y2, x1:x2, :);
66
    end
67
end
68
%B = B(~cellfun('isempty', B));
69
70
% create negative dataset
71
H = A_height - N + 1;
72
W = A_width - N + 1;
73
num_true = length(B);
74
C = cell(1,num_true);
75
for i=1:num_true
76
    flag = true;
77
    while flag
78
        X = randi(W);
79
        Y = randi(H);
80
        flag = isPos(X,Y,M,d);
81
    end
82
    y1 = Y;
83
    y2 = Y + 2*R;
84
    x1 = X;
85
    x2 = X + 2*R;
86
    C{i} = A(y1:y2, x1:x2, :);
87
end
88
89
% write set of images to file
90
n = length(dir([dataset_folder 'true/*.png']));
91
for i=1:num_true
92
    file1 = [dataset_folder 'true/' num2str(i+n,'%02u') '.png'];
93
    file2 = [dataset_folder 'false/' num2str(i+n,'%02u') '.png'];
94
    imwrite(B{i}, file1, 'png');
95
    imwrite(C{i}, file2, 'png');
96
end