Repository navigation
Expand file tree
/
Copy pathCopy_of_create_ROImask_manual.m
More file actions
101 lines (83 loc) · 3.06 KB
/
Copy pathCopy_of_create_ROImask_manual.m
File metadata and controls
101 lines (83 loc) · 3.06 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
function [roiMask_id,roiMask_stack]=create_ROImask_manual(cam_size)
%Function to create binary roi mask from RoiSet.zip files created in FIJI
%ImageJ
[fname,pname,typeind]=uigetfile({'*.roi;*.zip' 'ROI File or ZIP File'},'Choose ROI file','V:\DMDPatterns');
strROIArchiveFilename= fullfile(pname, fname);
[sROI] = ReadImageJROI(strROIArchiveFilename);
% Make sROI a cell if it is only one ROI
if numel(sROI) ==1
sROI = {sROI};
end
Nx = cam_size(2);
Ny = cam_size(1);
%Create binary masks for individual roi
[cc,rr] = meshgrid(1:Nx, 1:Ny);
roiMasks=[];
for i=1:numel(sROI)
if isequal(sROI{i}.strType,'Freehand')
ind=sROI{i}.mnCoordinates;
%For whatever reasons, ind take values of 0-img_size not 1-img_size.
%replace 0 in index to 1 to avoid errors
ind(ind==0)=1;
if norm(ind(1,:) - ind(end,:)) > 0
ind = [ind; ind(1,:)];
end
centOfMass(i,:) = [mean(ind(:,2)), mean(ind(:,1))];
tmp = poly2mask(ind(:,1),ind(:,2),Ny,Nx);
dd= bwconncomp(tmp);
if dd.NumObjects==1
%tmp=closeOpenROI(tmp);
elseif dd.NumObjects>=2
tmp=(poly2mask(ind(:,1),ind(:,2),Ny,Nx));
end
roiMasks(:,:,i)=tmp;
% close open loop
elseif isequal(sROI{i}.strType,'Oval')
r=mean(sROI{i}.vnRectBounds([1,3]));
c=mean(sROI{i}.vnRectBounds([2,4]));
rw=(sROI{i}.vnRectBounds(3)-sROI{i}.vnRectBounds(1))/2;
cw=(sROI{i}.vnRectBounds(4)-sROI{i}.vnRectBounds(2))/2;
centOfMass(i,:) = [c, r];
%Create ellipse masks from bounding box
roiMasks(:,:,i) = sqrt(((rr-r)/rw).^2+((cc-c)/cw).^2)<1;
elseif isequal(sROI{i}.strType,'Rectangle')
rectbounds = sROI{i}.vnRectBounds;
miny = min(rectbounds(1),rectbounds(3))+1;
maxy = max(rectbounds(1),rectbounds(3))+1;
minx = min(rectbounds(2),rectbounds(4))+1;
maxx = max(rectbounds(2),rectbounds(4))+1;
bw = zeros([Ny,Nx]);
bw(miny:maxy, minx:maxx) = 1;
roiMasks(:,:,i) = bw;
centOfMass(i,:) = [mean(rectbounds(1:2:end)), mean(rectbounds(2:2:end))];
end
end
%Detect duplicates in roiMasks and delete that
ind_exclude = [];
cell_vec = reshape(roiMasks, Nx*Ny,[]);
for i=1:size(roiMasks,3)
tmp = cell_vec - cell_vec(:,i);
ind = find(~any(tmp));
if length(ind)>=2
ind_exclude = [ind_exclude, ind(2:end)];
end
end
roiMasks(:,:,unique(ind_exclude)) = [];
centOfMass(unique(ind_exclude),:)=[];
%Use this not LabelingBinary.m to deal with adjuscent ROIs
roiMask_id=zeros(size(roiMasks(:,:,1)));
%rank order center of mass in ascending order
[Data,p] = sort(centOfMass(:,1),'ascend');
posRank = 1:length(Data);
posRank(p) = posRank;
%Create single images containing id of roiMasks
for i=1:length(posRank)
roiMask_id(logical(roiMasks(:,:,posRank==i)))=i;
end
%Exclulde pixels that are shared by multiple ROIs
roiMask_id(sum(roiMasks,3)~=1)=0;
% figure;imagesc(logical(roiMasks));
for i=1:numel(sROI)
roiMask_stack(:,:,i)=logical(roiMask_id==i);
end
end