-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlobs.asv
231 lines (197 loc) · 6.77 KB
/
Blobs.asv
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%% Scale invariant blob detector solution code
%% Adapted by Svetlana Lazebnik and Josef Sivic based on code by Serhat Tekin
%% UNC Chapel Hill, COMP 776 (Spring 2008)
%% ENS Paris, Object recognition and Computer Vision (Fall 2009)
%%
%% Usage: [r, c, rad] = BlobDetector(im, sigma, k, sigma_final, threshold)
%%
%% Arguments:
%% im - input image
%% sigma - initial scale
%% k - scale multiplication constant
%% sigma_final - largest scale to process
%% threshold - Laplacian threshold
%%
%% Returns:
%% r - row coordinates of blob centers
%% c - column coordinates of blob centers
%% rad - circular blob radius
%%
function [r, c, rad] = Blobs(im, sigma, k, sigma_final, threshold)
close all;
load('BW.mat','BW');
if size(im,3)>1
im = mean(im,3)/255;
end;
n = ceil((log(sigma_final) - log(sigma))/log(k)); % number of scale iterations
% allocate state space
[h, w] = size(im); % h, w => height and width of the state space
scaleSpace = zeros(h, w, n);
tic
% generate the Laplacian of Gaussian for the first scale level
filt_size = 2*ceil(3*sigma)+1; % important: to avoid "shifting" artifacts, make sure the kernel size is odd!
LoG = sigma^2 * fspecial('log', filt_size, sigma);
% generate the responses for the remaining levels
if 1 % Faster version: keep the filter size, downsample the image
fprintf('Filtering with Laplacian (keep filter size the same, downsample image)... \n');
imRes = im;
for i = 1:n
fprintf('Sigma %f\n', sigma * k^(i-1));
imFiltered = imfilter(imRes, LoG, 'same', 'replicate'); % filter the image with LoG
% note that no scale normalization is needed: the fact that the filter
% remains the same size while the image is downsampled ensures that the
% response of the filter is scale-invariant
imFiltered = imFiltered .^ 2; % save square of the response for current level
% upsample the LoG response to the original image size
scaleSpace(:,:,i) = imresize(imFiltered, size(im), 'bicubic'); % bilinear supersampling will result in a loss of spatial resolution
if i < n
imRes = imresize(im, 1/(k^i), 'bicubic');
end
end
toc
end;
% Slower version: increse filter size, keep image the same
if 0
fprintf('Filtering with Laplacian keep the image the same, change the filter size...\n');
scaleSpace2 = zeros(h, w, n);
for i = 1:n
sigmai = sigma * k^(i-1);
fprintf('%d/%d Sigma %f\n', i,n,sigmai);
% generate the Laplacian of Gaussian for the first scale level
filt_size = 2*ceil(3*sigmai)+1; % important: to avoid "shifting" artifacts, make sure the kernel size is odd!
LoG = sigmai^2 * fspecial('log', filt_size, sigmai); % scale normalized Laplacian
imFiltered = imfilter(im, LoG, 'same', 'replicate'); % filter the image with LoG
imFiltered = imFiltered .^ 2; % square of the response for current level
scaleSpace2(:,:,i) = imFiltered; % save response to a 3D array
figure(1); clf; imagesc(imFiltered); colorbar; % show the response
drawnow;
pause(0.01);
end
toc
scaleSpace = scaleSpace2;
end;
tic
fprintf('Performing nonmaximum suppression within scales...\n');
% perform non-maximum suppression for each scale-space slice
supprSize = 3;
maxSpace = zeros(h, w, n);
for i = 1:n
maxSpace(:,:,i) = ordfilt2(scaleSpace(:,:,i), supprSize^2, ones(supprSize));
% maxSpace(:,:,i) = colfilt(scaleSpace(:,:,i), [supprSize supprSize], 'sliding', @max);
% % this is a slightly less efficient option
end
toc
% non-maximum suppression between scales and threshold
tic
fprintf('Performing nonmaximum suppression between scales...\n');
for i = 1:n
maxSpace(:,:,i) = max(maxSpace(:,:,max(i-1,1):min(i+1,n)),[],3);
end
maxSpace = maxSpace .* (maxSpace == scaleSpace);
toc
r = [];
c = [];
rad = [];
for i=1:n
[rows, cols] = find(maxSpace(:,:,i) >= threshold);
radii = sigma * k^(i-1) * sqrt(2);
numBlobs = length(rows);
for j=1:numBlobs
if (BW(rows(j),cols(j))==1)
%radii = repmat(radii, numBlobs, 1);
r = [r; rows(j)];
c = [c; cols(j)];
rad = [rad; radii];
end
end
% radii = sigma * k^(i-1) * sqrt(2);
% radii = repmat(radii, numBlobs, 1);
% r = [r; rows];
% c = [c; cols];
% rad = [rad; radii];
end
figure(2); clf;
show_all_circles(im, c, r, rad, 'r', 1.5);
%findIndices(im,r,c);
pause(.1);
drawnow;
figure
getmask(im,c,r,rad);
function show_all_circles(I, cx, cy, rad, color, ln_wid)
%% I: image on top of which you want to display the circles
%% cx, cy: column vectors with x and y coordinates of circle centers
%% rad: column vector with radii of circles.
%% The sizes of cx, cy, and rad must all be the same
%% color: optional parameter specifying the color of the circles
%% to be displayed (red by default)
%% ln_wid: line width of circles (optional, 1.5 by default
load('BW.mat','BW');
if nargin < 5
color = 'r';
end
if nargin < 6
ln_wid = 1.5;
end
imshow(I.*BW); hold on;
theta = 0:0.1:(2*pi+0.1);
cx1 = cx(:,ones(size(theta)));
cy1 = cy(:,ones(size(theta)));
rad1 = rad(:,ones(size(theta)));
theta = theta(ones(size(cx1,1),1),:);
X = cx1+cos(theta).*rad1;
Y = cy1+sin(theta).*rad1;
line(X', Y', 'Color', color, 'LineWidth', ln_wid);
title(sprintf('%d circles', size(cx,1)));
function mask = getmask(im,c,r,rad)
[h,w,~] = size(im);
[X,Y] = meshgrid(1:w,1:h);
len = length(rad);
mask =zeros(h,w);
for i = 1:len
mask= mask==1 | sqrt((X-c(i)).^2 + (Y-r(i)).^2) < rad(i);
%mask= mask + (sqrt((X-c(i)).^2 + (Y-r(i)).^2) < rad(i));
% mask = mask + sqrt((X-c(i)).^2 + (Y-r(i)).^2) < rad(i);
end
%regmax =imregionalmax(mask,8);
mask =imclose(mask,strel('disk',10));
%mask =imdilate(regmax,[1 1 1,1 0 1,1 1 1]);
stat = regionprops('table',mask,'centroid','area');
areas = stat.Area;
centers = stat.Centroid;
if (area)
radii = 5*ones(1,length(areas));
load('BW.mat','BW');
mask = mask.*BW;
%imshow(regmax.*BW+im);
figure
imshow(mask+im);
hold on
viscircles(centers,radii);
hold off;
% function indices = findIndices(im,r,c)
% len = length(r);
% threshold = 1;
% clusters = zeros(1,len);
% for i =1:len
% for j = 1:len
% if (i~=j)
% distance = sqrt((r(i)-r(j))^2+(c(i)-c(j))^2);
% if (distance<threshold)
% clusters(i) = clusters(i)+1;
% end
% end
% end
% end
%
% [~,locs]=findpeaks(clusters);
% figure
% imshow(im); hold on;
% theta = 0:0.1:(2*pi+0.1);
% r1 = r(:,ones(size(theta)));
% c1 = c(:,ones(size(theta)));
% theta = theta(ones(size(c1,1),1),:);
% X = c1+cos(theta).*10;
% Y = r1+sin(theta).*10;
% line(X', Y', 'Color', 'b', 'LineWidth', 1.5);
%
% save('clusters.mat','clusters');