tuneaccuracy.m
2.83 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
clc;
clear;
disp('***************** NEW RUN *********************');
% ******* Initialize voting parameters **************************************
rmin = 2; %minimum radius of a cell
rmax = 30; %maximum radius of the cell
phi = 25; %half the angular range of the voting area
iterNum =6; %number of voting iterations
%%%%%%%%%%
% cell_t = 4;
% sigmagk =0.8;
% sigma =0.75;
% final_t = 250;
cell_t = 1;
sigmagk = 0.8;
sigma = 2.5;
% ******************Loading input Image**********************************
% I = imread('test/test10.png');
I = imread('newcrp.bmp');
X = size(I, 1);
Y = size(I, 2);
% **************** Gradient Magnitude and Direction for Image **************
% make a 2D gaussian kernel has its size defined by sigma (the std)
gX = sigma * 4;
gY = gX;
[gx, gy] = meshgrid(-gX:gX,-gY:gY);
a = 1/(2*pi*sigma^2);
b = -1/(2*sigma^2);
c = gx.^2 + gy.^2;
G = a*exp(b*c); % the Gaussian function
[dgx,dgy] = gradient(G); % derivatives of gaussian kernel in x and y directions.
%convolve I with the derivatives of the gaussian
Igx = conv2(double(I),dgx,'same');
Igy = conv2(double(I),dgy,'same');
M = sqrt(Igx.*Igx+Igy.*Igy); % compute the gradient magnitude.
D = atan2(Igx,Igy);
% ************************************** Thresholding **************************************
Mt = M > cell_t;
%find nonzero values in the threshold image (return 2D indices)
[sx, sy] = find(Mt);
%find the 1D index into the gradient direction D
idx = sub2ind(size(D), sx, sy);
utheta = D(idx); %get the gradient direction for each coordinate
% compute the 2D Gaussian Kernel with size of [7 7] and yariance of 1.
Xgk = sigmagk * 4;
Ygk = Xgk;
[gkx, gky] = meshgrid(-Xgk:Xgk,-Ygk:Ygk);
agk = 1/(2*pi*sigmagk^2);
bgk = -1/(2*sigmagk^2);
cgk = gkx.^2 + gky.^2;
gaussiankernel = agk*exp(bgk*cgk); % the Gaussian function
% Voting
for i=1 : iterNum
if phi>=0
[VVV, utheta, phi] = voting (rmin, rmax, X, Y, phi, utheta, sx, sy, length(sx),M, gaussiankernel);
end
end
%% ************************************** Detecting Cell Centers **************************************
final_t = 620;% 400-425 the best result with this data set
cellcenter = VVV;
cellcenter (VVV <final_t) = 0;
cellcenter = imregionalmax(cellcenter);
% ********************* Create an Overlay *************************************************************
output = cellcenter;
[tx, ty] = find(output);
% figure; imshow(mat2gray(output)); title('voting results');
% Ground Truth
gt = imread('GT.bmp');
gt(gt<255) = 0;
gt(gt==255)=1;
[gtx, gty] = find(gt);
% figure; imshow(mat2gray(gt)); title('Ground Truth');
% Validation
gt1 = [gtx(:), gty(:)];
out1 = [tx(:), ty(:)];
[IDX, D] = knnsearch(out1,gt1);
idx = IDX;
idx(D>5) = 0;
tp = nnz(idx);
lgt = length(gt1);
lout = length(out1);
fp = lout - tp;
fn = lgt - tp;
precision(final_t) = tp/(tp+fp);
recall(final_t) = tp/(tp+fn);
accuracy= tp/(tp+fp+fn)