forked from hoogenboom-lab/image-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomCoords.m
64 lines (55 loc) · 2.26 KB
/
RandomCoords.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
MainFolder = '';
FilePattern = strcat(MainFolder, '*.csv');
Files = dir(FilePattern);
% Get 5 sets of random coordinates within real image bounds
for z = 1 : 5
for k = 1 : length(Files)
Dists_filename = strcat(Files(k).folder, '\', Files(k).name);
Dists = readtable(Dists_filename);
Parts = split(Files(k).name, '_CLAHE');
SampleName = Parts{1};
SizeFiles = dir(strcat(Files(k).folder, '\*_', SampleName, '_Area_only.tif'));
%Get area outline
AreaOutlineFile = '';
AreaOutlinergb = imread(AreaOutlineFile);
if size(AreaOutlinergb,3)==3
AreaOutlineBW = imbinarize(rgb2gray(AreaOutlinergb));
else
AreaOutlineBW = imbinarize(AreaOutlinergb);
end
ImageSize = size(AreaOutlineBW);
%Change ImageSize scale to nm
nmPerPixel = 500/512;
w = ImageSize(1)/nmPerPixel;
h = ImageSize(2)/nmPerPixel;
areanm = imresize(AreaOutlineBW, nmPerPixel);
% Get Random coordinates
RanCoords = [];
% Find same number as real coordinates
while length(RanCoords) < height(Dists)
x = randi([1 ImageSize(1)], 1, 1);
y = randi([1 ImageSize(2)], 1, 1);
xnm = x/nmPerPixel;
ynm = y/nmPerPixel;
xy = [xnm,ynm];
% Make sure coordinate falls within image boundary
if AreaOutlineBW(x,y) == 1
RanCoords = [RanCoords;xy];
RanCoordsSize = size(RanCoords);
% And that the coordinates are no closer than physically
% possible
for j = 1 : (RanCoordsSize(1)-2)
Dist = sqrt((xnm - RanCoords(j,1))^2 + (ynm - RanCoords(j,2))^2);
if Dist <= 8
RanCoords(end,:) = [];
continue;
end
end
end
end
%Save all the ran coords
savenumber1 = strcat('RanCoords', num2str(z));
RanCoordsFile = strcat(Files(k).folder, '\', 'Out\', strrep(Files(k).name, 'CLAHE_prediction', savenumber1));
writematrix(RanCoords, RanCoordsFile);
end
end