forked from evodevosys/AroSpotFindingSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findembryosPreManual.m
executable file
·101 lines (61 loc) · 2.06 KB
/
findembryosPreManual.m
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
function [embryosegments,outlab,currpolys] = findembryosPreManual(fishmax)
%originally written by Arjun Raj and modified by Scott Rifkin
%fishmax is a max merege of the fish stack
fishmax = double(fishmax);
h = fspecial('log',50,.5);
%h = fspecial('log',50,5);
%fishmax = double(stk(31).data);
fprintf('Filtering image...\n');
L = imfilter(fishmax,h,'replicate');
fprintf('Removing small stuff...\n');
er = imerode(medfilt2(L,[5 5])<0,strel('disk',5));
bw = bwareaopen(er,10000);
fprintf('Watershedding...\n');
dist = bwdist(bw);
mask_em = imextendedmax(dist,10);
dd = -dist;
dd = dd-min(dd(:));
dd = dd/max(dd(:));
I_mod = imimposemin(dd,mask_em);
ws = watershed(I_mod);
fprintf('Finding embryos...\n');
bw2 = ws==0;
bw3 = (bw+bw2)>0;
bw4 = ~bw3;
[lab,n] = bwlabeln(bw4);
currI = lab;
sz = size(currI);
perimvals = currI(1,1:sz(2));
perimvals = [perimvals currI(sz(1),1:sz(2))];
perimvals = [perimvals currI(1:sz(1),1)'];
perimvals = [perimvals currI(1:sz(1),sz(2))'];
remperim = unique(perimvals);
%badareas = find(areas > 2000);
%remperim = intersect(remperim,badareas);
mas = ismember(currI,remperim);
bw5 = lab & ~mas;
[lab,n] = bwlabeln(bw5);
s = regionprops(lab,'basic');
areas = [s.Area];
goodinds = find(areas>20000);
bw6 = ismember(lab,goodinds);
[lab,n] = bwlabeln(bw6);
s = regionprops(lab,'Area','Centroid','BoundingBox','Image','ConvexHull');
currpolys={};
outlab = zeros(size(lab));
%expand it just so it doesn't crop off -will have to fix neighboring ones
%anyway
se=strel('disk',25);
for i = 1:n
%outlab = outlab + (i*poly2mask(s(i).ConvexHull(:,1),s(i).ConvexHull(:,2),1024,1024)).*(outlab==0);
currpolys{i}=imdilate((i*poly2mask(s(i).ConvexHull(:,1),s(i).ConvexHull(:,2),1024,1024)),se);
outlab = outlab + (currpolys{i}).*(outlab==0);
end;
embryosegments = regionprops(outlab,'Area','Centroid','BoundingBox','Image');
% Okay, now let's fix that annoying boundingbox thing:
for i = 1:length(embryosegments)
R = embryosegments(i).BoundingBox;
R(1:2) = R(1:2)+1;
R(3:4) = R(3:4)-1;
embryosegments(i).imageframe = R;
end;