-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBW_All.m
More file actions
117 lines (92 loc) · 4.38 KB
/
BW_All.m
File metadata and controls
117 lines (92 loc) · 4.38 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
106
107
108
109
110
111
112
113
114
115
116
117
function [CellRegion_All, CellNumDetected] = BW_All(ImageInfo, Background_nor, BlurSize, ExtensionRatio, varargin)
ActiveContourStatus = 'off';
AutoCellSize = 120;
ActiveContourTimes = 5;
Tag=char(datetime('now','format','-HH-mm-ss'));
if isempty(varargin)
else
for i = 1:(size(varargin, 2) / 2)
AssignVar(varargin{i * 2 - 1},varargin{i * 2})
end
end
File_id = ImageInfo.File_id;
TrackImageIndex = ImageInfo.TrackImageIndex;
FileName = ImageInfo.FileName;
Path = ImageInfo.Path;
BWtifStackName = [Path, 'BW_', FileName, Tag];
if exist([BWtifStackName, '.tif'], 'file') == 2
BWtifStackNameFull = [BWtifStackName, '_', num2str(floor(rand(1) * 10^5)), '.tif'];
while exist(BWtifStackNameFull, 'file') == 2
BWtifStackNameFull = [BWtifStackName, '_', num2str(floor(rand(1) * 10^5)), '.tif'];
end
else
BWtifStackNameFull = [BWtifStackName, '.tif'];
end
if strcmp(ImageInfo.FileType, '.nd2')
[FilePointer,ImagePointer,ImageReadOut] = ND2Open(File_id);
elseif strcmp(ImageInfo.FileType, '.tif')
else
warning('Error!')
return
end
CellNumDetected = zeros(1, size(TrackImageIndex, 2));
CellRegion_All = cell(0);
for i = 1:size(TrackImageIndex, 2)
if strcmp(ImageInfo.FileType, '.nd2')
Original_Image = ND2Read(FilePointer,ImagePointer,ImageReadOut,TrackImageIndex(i));
elseif strcmp(ImageInfo.FileType, '.tif')
Original_Image = imread(File_id, 'Index', TrackImageIndex(i), 'Info', ImageInfo.main);
else
warning('Error!')
return
end
if strcmp(Method, 'Fluorescent')
Normalize_Image = mat2gray(double(Original_Image(:, :)) ./ Background_nor);
elseif strcmp(Method, 'PhaseContrast')
Normalize_Image = 1-mat2gray(double(Original_Image(:, :)) - Background_nor);
end
BW_Image = BW_Single(Normalize_Image, BlurSize, ExtensionRatio, 'AutoCellSize', AutoCellSize, 'ActiveContourStatus', ActiveContourStatus, 'ActiveContourTimes', ActiveContourTimes);
imwrite(BW_Image, BWtifStackNameFull, 'WriteMode', 'append', 'Compression', 'none');
CellRegion = regionprops(BW_Image);
CellRegion_array = (reshape(struct2array(CellRegion), [7, size(CellRegion, 1)]))';
% remove the cells less then 5 pixels and larger than 360 pixels
CellRegion_array(CellRegion_array(:, 1) < 5 | CellRegion_array(:, 1) > 360, :) = [];
CellNumDetected(i) = size(CellRegion_array, 1);
CellRegion_array(:, 8)=(sum(CellNumDetected(1:i-1))+1):sum(CellNumDetected(1:i));
CellRegion_All{i} = CellRegion_array;
DisplayBar(i, size(TrackImageIndex, 2));
end
if strcmp(ImageInfo.FileType, '.nd2')
ND2Close(FilePointer);
clear('FilePointer');
else
end
if min(CellNumDetected) < 8
LowCellNumFrame = TrackImageIndex(CellNumDetected == min(CellNumDetected));
warning('off','backtrace')
warning(['Too few cells detected at frame ', num2str(LowCellNumFrame)])
warning('on','backtrace')
% for i = 1:size(LowCellNumFrame, 2)
% Original_Image = bfGetPlane(r, TrackImageIndex(LowCellNumFrame(i)));
% Normalize_Image = uint16(double(Original_Image(:, :)) ./ Background_nor);
% BW_Image = BW_Single(Normalize_Image, BlurSize, ExtensionRatio, 'ActiveContourTimes', ActiveContourTimes);
% imshow(BW_Image)
% end
else
end
if max(CellNumDetected) > 300
ManyCellNumFrame = TrackImageIndex(CellNumDetected == min(CellNumDetected));
warning('off','backtrace')
warning(['Too many cells detected at frame ', num2str(ManyCellNumFrame)])
warning('on','backtrace')
% for i = 1:size(ManyCellNumFrame, 2)
% Original_Image = bfGetPlane(r, TrackImageIndex(ManyCellNumFrame(i)));
% Normalize_Image = uint16(double(Original_Image(:, :)) ./ Background_nor);
% BW_Image = BW_Single(Normalize_Image, BlurSize, ExtensionRatio, 'ActiveContourTimes', ActiveContourTimes);
% imshow(BW_Image)
% end
else
end
disp(['The mean cells num is about: ', num2str(floor(mean(CellNumDetected)))])
disp(['The B/W movie is saved as: ', BWtifStackName])
end