Commit 8283ebee authored by Antonio Politi's avatar Antonio Politi

Upload of Julius Hossein Segmentation code

To do is to remove tiffread dependency and use bioformat for matlab
parent e768fa31
function [ threshold ] = Otsu_2D(image)
[xSize, ySize] = size(image);
maxVal = max(max(image));
histN = round(maxVal)+1;
histogram = zeros(1,histN);
threshold=0;
w0=0.0;
w1=0.0;
m0=0.0;
m1=0.0;
N=0;
p=0;
sum=0.0;
mean=0.0;
var_bet_class=0.0;
var_max=0.0;
mu_k=0.0;
for i = 1:xSize
for j = 1:ySize
hIndex = round(image(i,j))+1;
histogram(hIndex) = histogram(hIndex)+1;
end
end
% histogram
% pause;
for i=1:histN
N=N+histogram(i);
sum=sum+histogram(i)*i;
end
mean=sum/N;
for i=1:histN
p=histogram(i)/N;
%cumulative for class 1 and class 2
w0=w0+p;
w1=1-w0;
%mean for class 1 and class 2
mu_k=mu_k+i*p;
if(w0==0)
m0=0;
else
m0=(mu_k/w0);
end
if(1-w0==0)
m1=0;
else
m1=(mean-mu_k)/(1-w0);
end
var_bet_class = w0*(m0-mean)*(m0-mean)+w1*(m1-mean)*(m1-mean);
if var_bet_class>var_max
var_max=var_bet_class;
threshold=i;
end
end
end
function [ threshold ] = Otsu_2D_Img(image, masked)
[xSize, ySize] = size(image);
maxVal = max(max(image));
histN = round(maxVal)+1;
histogram = zeros(1,histN);
threshold=0;
w0=0.0;
w1=0.0;
m0=0.0;
m1=0.0;
N=0;
p=0;
sum=0.0;
mean=0.0;
var_bet_class=0.0;
var_max=0.0;
mu_k=0.0;
for i = 1:xSize
for j = 1:ySize
hIndex = round(image(i,j))+1;
histogram(hIndex) = histogram(hIndex)+1;
end
end
if masked == 1
histogram(1) = 0;
end
for i=1:histN
N=N+histogram(i);
sum=sum+histogram(i)*i;
end
mean=sum/N;
for i=1:histN
p=histogram(i)/N;
%cumulative for class 1 and class 2
w0=w0+p;
w1=1-w0;
%mean for class 1 and class 2
mu_k=mu_k+i*p;
if(w0==0)
m0=0;
else
m0=(mu_k/w0);
end
if(1-w0==0)
m1=0;
else
m1=(mean-mu_k)/(1-w0);
end
var_bet_class = w0*(m0-mean)*(m0-mean)+w1*(m1-mean)*(m1-mean);
if var_bet_class>var_max
var_max=var_bet_class;
threshold=i;
end
end
end
function [threshold] = Otsu_3D_Hist(histogram,cut_thresh, masked)
%This function receives histogram as input
%It computes a threshold without considering the values in the histogram
%greater than cut_threshold
histN = cut_thresh;
threshold=1;
w0=0.0;
w1=0.0;
m0=0.0;
m1=0.0;
N=0;
p=0;
sum=0.0;
mean=0.0;
var_bet_class=0.0;
var_max=0.0;
mu_k=0.0;
if masked ==1
histogram(1) = 0;
end
for i=1:histN
N=N+histogram(i);
sum=sum+histogram(i)*i;
end
mean=sum/N;
for i=1:histN
p=histogram(i)/N;
%cumulative for class 1 and class 2
w0=w0+p;
w1=1-w0;
%mean for class 1 and class 2
mu_k=mu_k+i*p;
if(w0==0)
m0=0;
else
m0=(mu_k/w0);
end
if(1-w0==0)
m1=0;
else
m1=(mean-mu_k)/(1-w0);
end
var_bet_class = w0*(m0-mean)*(m0-mean)+w1*(m1-mean)*(m1-mean);
if var_bet_class>var_max
var_max=var_bet_class;
threshold=i;
end
end
end
function [threshold, histogram] = Otsu_3D_Img(image, masked)
[xSize, ySize, zSize] = size(image);
maxVal = max(max(max(image)));
histN = round(maxVal)+1;
histogram = zeros(1,histN);
threshold=1;
w0=0.0;
w1=0.0;
m0=0.0;
m1=0.0;
N=0;
p=0;
sum=0.0;
mean=0.0;
var_bet_class=0.0;
var_max=0.0;
mu_k=0.0;
for i = 1:xSize
for j = 1:ySize
for k = 1:zSize
hIndex = round(image(i,j,k))+1;
histogram(hIndex) = histogram(hIndex)+1;
end
end
end
if masked == 1
histogram(1) = 0;
end
for i=1:histN
N=N+histogram(i);
sum=sum+histogram(i)*i;
end
mean=sum/N;
for i=1:histN
p=histogram(i)/N;
%cumulative for class 1 and class 2
w0=w0+p;
w1=1-w0;
%mean for class 1 and class 2
mu_k=mu_k+i*p;
if(w0==0)
m0=0;
else
m0=(mu_k/w0);
end
if(1-w0==0)
m1=0;
else
m1=(mean-mu_k)/(1-w0);
end
var_bet_class = w0*(m0-mean)*(m0-mean)+w1*(m1-mean)*(m1-mean);
if var_bet_class>var_max
var_max=var_bet_class;
threshold=i;
end
end
end
% Version data: 2015-11-11
% Author: Julius Hossain, EMBL, Heidelberg, Germany: julius.hossain@embl.de
% modified 21.04.2016 Antonio Politi introduce channel parameter
%This scripts search for folders containing high resoluation images and
%call the function that segments chromosomes and save output in a mat file
clc; clear all; close all;
rootDir = 'Z:\AntonioP_elltier1\CelllinesImaging\160413\mEGFP-Nup107\';
fn = dir([rootDir 'HR5.lsm']);
channel = 3;
for i=1:length(fn)
try
fn_SegmentChromosome_ST(rootDir, fn(i).name, channel);
catch
disp(['Could not process the cell: ' fn(i).name]);
end
end
% rootDir = 'U:\Antonio_151214\toSegment\Ki67_1336_meta\';
% fn = dir([rootDir '*.lsm']);
%
% for i=1:length(fn)
% try
% fn_SegmentChromosome_ST(rootDir, fn(i).name);
% catch
% disp(['Could not process the cell: ' fn(i).name]);
% end
% end
\ No newline at end of file
This diff is collapsed.
% CStruct Toolbox.
% Version 2.0.1 12-Apr-2007
%
% Functions.
% structappend - Add value to a struct, handling duplicate fields.
% structcombine - Crash two structs together.
% structmap - Use a mapping struct to map keys to values.
% structread - Read in structured data.
% structsizeof - Determine the size of a readin struct.
%
% Updates from Version 2.0.0 of 30-Aug-2005
% Added structcombine.
% Peter Li 12-Apr-07
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
function newvalue = structappend(struct, field, value)
%STRUCTAPPEND
% VALUE = STRUCTAPPEND(STRUCT, FIELD, VALUE)
%
% When adding data to struct, gracefully handle duplicated field names by
% concatenating values into a cell array.
%
% Peter Li 30-Aug-05
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
error(nargchk(3, 3, nargin));
if isfield(struct, field) % This field already initialized!
if iscell(struct.(field)) % Already converted this field into cells
newvalue = struct.(field);
newvalue{end+1} = value;
else % Otherwise, convert this field into cells
newvalue = {struct.(field), value};
end
else % Make a new field
newvalue = value;
end
function combined = structcombine(struct1, struct2)
%STRUCTCOMBINE
% COMBINED = STRUCTCOMBINE(STRUCT1, STRUCT2)
%
% Crash structs together, using STRUCTAPPEND to resolve overlapping fields.
% This will take all the fieldnames of STRUCT2 and add them to STRUCT1, then
% copy all the data of STRUCT2 into the new expanded STRUCT1, including data
% in higher dimensions of struct2.
%
% For example, if STRUCT1 is:
% struct1.x = 0
%
% And STRUCT2 is:
% struct2.y = 0
% struct2(50,50).y = 0
%
% Then the result, COMBINED is a 50-by-50 struct array with fields x and y and
% almost no data except for fields combined(1).x, combined(1).y, and
% combined(50,50).y, all of which are set to zero.
%
% For example of overlapping fields, if:
% struct1.x = 0
% struct2.x = 0
%
% Then combined.x will be cell array {0 0}.
% Peter Li 12-Apr-07
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
error(nargchk(2, 2, nargin));
% Start with just STRUCT1
combined = struct1;
% Iterate through the fields of STRUCT2, adding them to STRUCT1
fields1 = fieldnames(struct1);
fields2 = fieldnames(struct2);
nlength2 = prod(size(struct2));
for i = 1:length(fields2)
% Check that the field name we're about to add doesn't already exist in
% STRUCT1
if ~length(strmatch(fields2{i}, fields1))
[combined(1:nlength2).(fields2{i})] = deal(struct2.(fields2{i}));
else
combined(nlength2).(fields2{i}) = [];
for j = 1:nlength2
combined(j).(fields2{i}) = structappend(combined(j), fields2{i}, struct2(j).(fields2{i}));
end
end
end
function tag = structmap(tagcode, map, numprefix)
% STRUCTMAP use struct as a map between field names and values
% TAG = STRUCTMAP(TAGCODE, MAP, [NUMPREFIX])
%
% Unfortunately, MatLab structs cannot have numerical field names, so we must
% use a hack NUMPREFIX. A better solution would be to use cell arrays and OO
% programming.
%
% Peter Li 30-Aug-05
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
error(nargchk(2, 3, nargin));
if nargin < 3, numprefix = 'x'; end % Default NUMPREFIX hack for numerical fields
if ~isstruct(map), error('Second argument must be a struct map'); end
if isnumeric(tagcode),
tagcode = [numprefix num2str(tagcode)];
elseif ~ischar(tagcode),
error('Function ''structmap'' is defined for tagcode values of class numeric and char only');
end
if isfield(map, tagcode),
tag = map.(tagcode);
else
tag = [];
end
function readstruct = structread(fid, structdef);
% STRUCTREAD read in structured data
% READSTRUCT = STRUCTREAD(FID, STRUCTDEF)
%
% Read in structured data from file handle FID according to STRUCTDEF,
% which is in the ordered format of structdef.fieldname = datatype
% For example, structdef.isbn = '10*uint8' means that the ISBN field to be
% read consists of 10 unsigned integers.
%
% Peter Li 30-Aug-05
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
error(nargchk(2, 2, nargin));
if ~isstruct(structdef), error('Second argument must be a struct definition'); end
readstruct = struct([]);
% Get file length
pos = ftell(fid);
fseek(fid, 0,'eof');
flen = ftell(fid);
if fseek(fid, pos, 'bof') == -1, error(['Received error on file seek: ' ferror(fid)]); end
% Calculate how many bytes the file has left, compare to size of struct
fleft = flen - pos;
structsize = structsizeof(structdef);
if structsize > fleft, error(['Size of Struct (' num2str(structsize) ') > Bytes Left in File (' num2str(fleft) ')']); end
% Main section
fields = fieldnames(structdef);
for i = 1:length(fields),
field = fields{i};
datatype = structdef.(field);
% Parse datatype
starind = strfind(datatype, '*');
if length(starind) > 1, error(['Incorrect datatype format: ' datatype]); end
if starind,
num = str2num(datatype(1:starind-1));
datatype = datatype(starind + 1:end);
else
num = 1;
end
% Read value(s)
[value, readnum] = fread(fid, num, datatype);
if readnum ~= num, error(['Failed to read more than ' num2str(readnum) ' values for ' field '(' num2str(num) ')']); end
% If value is character string, convert to char.
if strfind(datatype, 'char'),
value = char(value);
end
% Handle fieldnames ending in numbers, e.g. if fields UNKNOWN1, UNKNOWN2,
% etc. exist, we will concatenate them all as a cell array into a single
% UNKNOWN field.
% See if filename ends in a number
for i = fliplr(1:length(field)),
if ~length(str2num(field(i:end))),
break;
end
end
if i < length(field),
appendnum = str2num(field(i+1:end));
appendfield = field(1:i);
% See if appendfield exists and whether appendnum matches the number
% of elements to be stored there.
if ~isfield(readstruct, appendfield),
if appendnum == 1, field = appendfield; end
else
if ~iscell(readstruct.(appendfield)),
if appendnum == 2, field = appendfield; end
else
if appendnum == length(readstruct.(appendfield)) + 1, field = appendfield; end
end
end
end
readstruct(1).(field) = structappend(readstruct, field, value');
end
function [size, errormsg] = structsizeof(structdef)
%STRUCTSIZEOF Determine size (in bytes) of struct to be read
% SIZE = STRUCTSIZEOF(STRUCTDEF)
%
% Peter Li 30-Aug-05
% Some rights reserved. Licensed under Creative Commons: http://creativecommons.org/licenses/by-nc-sa/3.0/
if nargin ~= 1 | ~isstruct(structdef), error('First argument must be struct definition'); end
size = 0;
errormsg = '';
fields = fieldnames(structdef);
for i = 1:length(fields);
datatype = structdef.(fields{i});
% Parse num
starind = strfind(datatype, '*');
if length(starind) > 1, error(['Incorrect datatype format: ' datatype]); end
if starind
num = str2num(datatype(1:starind(1) - 1));
datatype = datatype(starind(1)+1:end);
else
num = 1;
end
switch datatype
case {'uchar' 'schar' 'uint8' 'int8'}
size = size + num;
case {'uint16' 'int16'}
size = size + (2 * num);
case {'uint32' 'int32' 'single' 'float32'}
size = size + (4 * num);
case {'uint64' 'int64' 'double' 'float64'}
size = size + (8 * num);
otherwise
size = -1;
errormsg = ['Unknown datatype: ' datatype];
return;
end
end
% 431-400 Year Long Project
% LA1 - Medical Image Processing 2003
% Supervisor : Dr Lachlan Andrew
% Group Members : Alister Fong 78629 a.fong1@ugrad.unimelb.edu.au
% Lee Siew Teng 102519 s.lee1@ugrad.unimelb.edu.au
% Loh Jien Mei 103650 j.loh1@ugrad.unimelb.edu.au
%
% File and function name : euclidean_distance
% Version : 1.0
% Date of completion : 22 September 2003
% Written by : Alister Fong 78629 a.fong1@ugrad.unimelb.edu.au
%
% Inputs :
% Coord1,Coord2 - Coordinates to be calculated. Each of them in [X,Y]
%
% Outputs :
% distance - The calculated distance between each pair of coordinates
%
% Description :
% Calculates the euclidean_distance between 2 points
%
% To Run >> distance = euclidean_distance(Coord1,Coord2)
%
% Example >> distance = euclidean_distance([2,4;5,6],[4,6;5,7]);
function distance = euclidean_distance(Coord1,Coord2)
if isempty(Coord1) | isempty(Coord2)
error('No data input');
end
distance = sqrt((Coord1(:,1)-Coord2(:,1)).^2 + (Coord1(:,2)-Coord2(:,2)).^2 + (Coord1(:,3)-Coord2(:,3)).^2);
\ No newline at end of file
function biggestobject=findbiggestobject_bwconcomp(image,connection)
twoDLabel = bwconncomp(image,connection);
numPixels = cellfun(@numel,twoDLabel.PixelIdxList);
[biggest, idx] = max(numPixels);
biggestobject = image;
biggestobject(:,:) = 0;
biggestobject(twoDLabel.PixelIdxList{idx}) = 1;
function intVolume= fn_PC_GenIntermediateSlices(volume, zFactor)
%This function generates intermediate slices to convert stack to isotropy
%It uses simple linear interpolation so that neibourhood information is not
%used multiple times.
%It returns interpolated volume (intVolume)
[dx, dy, dz] = size(volume);
newDz = dz * zFactor - zFactor + 1;
intVolume = zeros(dx,dy,newDz);
for i = 1: dz
intVolume(:,:,i*zFactor-zFactor +1) = volume(:,:,i);
end
for i = 1:dz-1
for j = 2:zFactor
intVolume(:,:,i*zFactor-zFactor +j) = intVolume(:,:,i*zFactor-zFactor +1) *(1-(j-1)/zFactor) + intVolume(:,:,i*zFactor +1) * (j-1)/zFactor;