Skip to content

Commit 7658beb

Browse files
committed
latest version of matlab code
1 parent 3286072 commit 7658beb

24 files changed

+1477
-261
lines changed

calcIndTr.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
% This function takes the registered locations, ccl, and the original
2+
% locations, cl, and calculates the local transformations for these points.
3+
% This is done in a weighted manner by taking each point in turn and
4+
% finding its 10 nearest neighbors. Then, we draw rays from the point of
5+
% interest to each of its nearest neighbors and divide that ray into 10
6+
% segments.We then use all of these points to calculate the local affine
7+
% transformation. By interpolating points along the rays between points, we
8+
% create a higher density of points nearest our point of interest.
9+
%
10+
% Author: Joshua Doyle, 08/2019 - e.rational.2.718@gmail.com
11+
12+
13+
function T = calcIndTr(ccl,cl,c,conf,np,nn)
14+
15+
T = zeros(length(np(conf>c)),6);
16+
[Idx,Dx] = knnsearch(ccl(conf>c,1:2),ccl(conf>c,1:2),'K',nn);
17+
% ind = (conf>c);
18+
dex = find(conf>c);
19+
Dxref = zeros(length(dex),size(Idx,2));
20+
Rreg = zeros(nn+1,size(Idx,2));
21+
Rref = zeros(nn+1,size(Idx,2));
22+
REG = [];%zeros(size(Rreg,1)*size(Rreg,2));
23+
REF = [];
24+
25+
for i1 = 1:length(np(conf>c))
26+
Dxref(i1,:) = ((cl(np(dex(Idx(i1,:))),1)-cl(np(dex(Idx(i1,1))),1)).^2 +...
27+
(cl(np(dex(Idx(i1,:))),2)-cl(np(dex(Idx(i1,1))),2)).^2).^0.5';
28+
thetareg = atan2(ccl(dex(Idx(i1,:)),2)-ccl(dex(Idx(i1,1)),2),...
29+
ccl(dex(Idx(i1,:)),1)-ccl(dex(Idx(i1,1)),1));
30+
thetaref = atan2(cl(np(dex(Idx(i1,:))),2)-cl(np(dex(Idx(i1,1))),2),...
31+
cl(np(dex(Idx(i1,:))),1)-cl(np(dex(Idx(i1,1))),1));
32+
for i2 = 2:length(thetareg)
33+
Rreg(:,i2) = 0:Dx(i1,i2)/nn:Dx(i1,i2);
34+
Rref(:,i2) = 0:Dxref(i1,i2)/nn:Dxref(i1,i2);
35+
end
36+
regx = reshape(Rreg,[],1).*cos(reshape(repmat(thetareg',size(Rreg,1),1),[],1));
37+
regy = reshape(Rreg,[],1).*sin(reshape(repmat(thetareg',size(Rreg,1),1),[],1));
38+
refx = reshape(Rref,[],1).*cos(reshape(repmat(thetaref',size(Rref,1),1),[],1));
39+
refy = reshape(Rref,[],1).*sin(reshape(repmat(thetaref',size(Rref,1),1),[],1));
40+
41+
ref = [refx refy] + [cl(np(dex(Idx(i1,1))),1) cl(np(dex(Idx(i1,1))),2)];
42+
reg = [regx regy] + [ccl(dex(Idx(i1,1)),1) ccl(dex(Idx(i1,1)),2)];
43+
44+
[ab,cd,e,f] = newtform(ref,reg);
45+
T(i1,:) = [ab' cd' [e;f]'];
46+
REG = [REG;reg];
47+
REF = [REF;ref];
48+
%
49+
% figure,scatter(ref(:,1),ref(:,2)),hold on, scatter(reg(:,1),reg(:,2),'x')
50+
% hold on, scatter(cl(np(dex(Idx(i1,1))),1),cl(np(dex(Idx(i1,1))),2),'x')
51+
%
52+
end
53+
54+
end

checkVars.m

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,78 @@
1-
function [regData,ihcrs,ifpaths,b,h,w,scale,ihcscale] = checkVars(ihc,ifdir)
1+
%% checkVars
2+
%% Created by: Joshua Doyle
3+
%% Edited by: Benjamin Green
4+
%% -----------------------------------------------------------
5+
%% Description
6+
% part of the registration protocol for slides from different microscopes
7+
% reads in the images to memory and gets image parameters
8+
% -- Possible Improvement in readYCBCr2RGB for parloop reading in subset of
9+
% main IHC image to improve read time -- use imread 'Region Property'
10+
%% -----------------------------------------------------------
11+
function [regData,ihcrs,ifpaths,b,h,w,scale,ihcscale] ...
12+
= checkVars(ihc,ifdir)
13+
%
214
% pixels/u - Vectra scale (mft provided)
15+
%
316
scale = 1.9981018;
17+
%
418
% Number of bands in IF images
19+
%
520
ncomponents = 8;
21+
%
622
% pixels/u - Hamamatsu scale (mft provided)
23+
%
724
ihcscale = 2.173913;
8-
9-
if ~exist('regData', 'var')
10-
%myDir = uigetdir('Load Componenet Tiff Directory'); %gets directory
11-
ifpaths = dir(fullfile(ifdir,'*component_data.tif'));
12-
13-
regData = struct('filename',{{}},'micronLoc',{{}},'regLoc',{{}});
14-
for i1 = 1:length(ifpaths)
15-
baseFileName = ifpaths(i1).name;
16-
% B = strsplit(baseFileName,'_');
17-
% if length(B) > 3
18-
% if strcmp(B{4},'binary')
19-
% imstr.binaryfile = baseFileName;
20-
% end
21-
% if strcmp(B{4},'composite')
22-
% imstr.componentfile = baseFileName;
23-
% end
24-
% if strcmp(B{4},'phenotype')
25-
% imstr.phenofile = baseFileName;
26-
% end
27-
%
28-
% if strcmp(B{8},'component')
29-
% imstr.compositefile = baseFileName;
30-
regData.filename{i1} = baseFileName;
31-
C = strsplit(baseFileName,{'[',',',']'});
32-
% TODO
33-
% add if wholeslide || if TMA
34-
regData.micronLoc{i1} = [str2double(C{2}) str2double(C{3})];
35-
%regData.micronLoc{i1} = [str2double(C{6}) str2double(C{7})];
36-
% end
37-
% end
38-
end
39-
b = cast(reshape(cell2mat(regData.micronLoc),2,[]),'uint32')';
25+
%
26+
% get if image names
27+
%
28+
ifpaths = dir(fullfile(ifdir, '*component_data.tif'));
29+
if isempty(ifpaths)
30+
msg = ['Error: could not find IF images in - ',ifpaths];
31+
error(msg)
4032
end
41-
42-
% if ~exist('miData','var')
43-
% load('miData');
44-
% mirs = reshape(miData.mmi,[],1);
45-
% midata2 = zeros(length(mirs),1);
46-
% for i1 = 1:length(mirs)
47-
% midata2(i1) = sum(mirs{i1}(:));
48-
% end
49-
% [ma,mind] = max(midata2);
50-
% [I,J] = ind2sub([9,9],mind);
51-
% end
52-
53-
if ~exist('ihcrs','var')
54-
55-
%[ihcfile,ihcpath] = uigetfile('*/*.tif','Select a full slide image for registration');
56-
%tobj = Tiff([ihcpath,ihcfile],'r');
57-
tobj = Tiff(ihc,'r');
58-
origh = getTag(tobj,'ImageLength');
59-
origw = getTag(tobj,'ImageWidth');
60-
ihcrs = imresize(readYCbCr2RGB(tobj),[origh*scale/ihcscale,origw*scale/ihcscale]);
61-
62-
%ihcrs = imresize(readYCbCr2RGB(tobj),[origh*scale/miData.param.scale{I,J}(2),origw*scale/miData.param.scale{I,J}(1)]);
63-
64-
% tobjATP = Tiff('Z:\stitch\Hamamatsu Channel Images\M41-ATPase.tif','r');
65-
% tobjHematox = Tiff('Z:\stitch\Hamamatsu Channel Images\M41-Hematoxyln.tif','r');
66-
% origh = getTag(tobjATP,'ImageLength');
67-
% origw = getTag(tobjATP,'ImageWidth');
68-
% ihcrs(:,:,1) = imresize(read(tobjATP),[origh*scale/miData.param.scale{I,J}(2),origw*scale/miData.param.scale{I,J}(1)]);
69-
% ihcrs(:,:,2) = imresize(read(tobjHematox),[origh*scale/miData.param.scale{I,J}(2),origw*scale/miData.param.scale{I,J}(1)]);
33+
%
34+
regData = struct('filename', {{}}, 'micronLoc', {{}}, 'regLoc', {{}});
35+
for i1 = 1:length(ifpaths)
36+
baseFileName = ifpaths(i1).name;
37+
regData.filename{i1} = baseFileName;
38+
C = strsplit(baseFileName,{'[',',',']'});
39+
regData.micronLoc{i1} = [str2double(C{2}) str2double(C{3})];
7040
end
71-
41+
%
42+
% variable for registered locations
43+
%
44+
b = cast(reshape(cell2mat(regData.micronLoc),2,[]),'uint32')';
45+
%
46+
% get IHC image and size params
47+
%
48+
if ~exist(ihc,'file')
49+
msg = ['Error: could not find IHC image - ',ihc];
50+
error(msg)
51+
end
52+
%
53+
fprintf(1, 'Reading %s\n', ihc);
54+
%
55+
tic
56+
imf = imfinfo(ihc);
57+
level = find(ismember({imf(:).ImageDescription}, 'x20 z0'));
58+
origh = imf(level).Height;
59+
origw = imf(level).Width;
60+
%
61+
img = imread(ihc,level);
62+
ihcrs = imresize(img,...
63+
[origh*scale/ihcscale,origw*scale/ihcscale]);
64+
clear img
65+
fprintf(' ');
66+
toc
67+
%
68+
% get IF size params
69+
%
7270
baseFileName = ifpaths(1).name;
7371
fullFileName = fullfile(ifdir, baseFileName);
74-
fprintf(1, 'Now reading %s\n', fullFileName);
75-
tobj = Tiff(fullFileName);
76-
img = makeTifIm(tobj,ncomponents);
77-
[h,w,bands] = size(img);
72+
img = imfinfo(fullFileName);
73+
h = img.Height;
74+
w = img.Width;
75+
regData.h = h; %hpf height
76+
regData.w = w; %hpf width
77+
%
78+
end

cropandwritehpfs.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function cropandwritehpfs(rd,wsimf,wsimdir)
2+
3+
if ~exist('wsimdir','dir')
4+
mkdir(wsimdir);
5+
end
6+
S = strsplit(wsimdir,'\');
7+
suff = S{end-2};
8+
9+
for i1 = 1:length(rd.filename)
10+
11+
S = strsplit(rd.filename{i1}, '_');
12+
fid = strjoin(S(1:end-2),'_');
13+
fid = [fid,'_',suff];
14+
15+
imr = wsimf(rd.regLoc(i1,2):rd.regLoc(i1,2)+rd.h-1,...
16+
rd.regLoc(i1,1):rd.regLoc(i1,1)+rd.w-1,:);
17+
18+
imwrite(imr,[wsimdir,'\',fid,'.tif']);
19+
20+
end
21+
save([wsimdir,'\rd'],'rd')
22+
end

doRegSet.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
% topdir = '/Volumes/e$/Clinical_Specimen/';
2+
topdir = '\\bki04\e$\Clinical_Specimen\';
3+
A = dir(fullfile(topdir, 'M*'));
4+
specID = {A(:).name};%{'M1_1','M2_3'};%M9_1','M10_2','M11_1','M12_1'};
5+
% specID = {'M41_1'};
6+
for i1 = 1%round(length(specID)/2)+1:length(specID)
7+
8+
tic;
9+
he = ['\\bki04\e$\Clinical_Specimen\',specID{i1},'\HE\',specID{i1},'.ndpi'];
10+
ihc = ['\\bki04\e$\Clinical_Specimen\',specID{i1},'\IHC\',specID{i1},'.ndpi'];
11+
ifdir = ['\\bki04\e$\Clinical_Specimen\',specID{i1},'\inform_data\Component_Tiffs'];
12+
if_hpfs = dir(fullfile(ifdir,'*component_data.tif'));
13+
14+
if exist(ihc,'file')
15+
S = strsplit(ihc,'\');
16+
ihc_dir = ['\',strjoin(S(1:6),'\'),'\HPFs\'];
17+
ihc_hpfs = dir(fullfile(ihc_dir, '*IHC.tif'));
18+
if length(ihc_hpfs) == length(if_hpfs)
19+
try
20+
regFullSlide(ihc,ifdir);
21+
catch ME
22+
warning(['MATLAB returned the following error identifier - ',ME.identifier]);
23+
warning(['MATLAB returned the following error message - ',ME.message]);
24+
A(i1).error = 1;
25+
end
26+
% else
27+
% fprintf(['Directory ',ihc_dir,' already exists with all HPFs. Delete directory and try again. \n'])
28+
29+
end
30+
end
31+
toc;
32+
tic;
33+
if exist(he,'file')
34+
S = strsplit(he,'\');
35+
he_dir = ['\',strjoin(S(1:6),'\'),'\HPFs\'];
36+
he_hpfs = dir(fullfile(he_dir, '*HE.tif'));
37+
if length(he_hpfs) == length(if_hpfs)
38+
try
39+
regFullSlide(he,ifdir);
40+
catch ME
41+
warning(['MATLAB returned the following error identifier - ',ME.identifier]);
42+
warning(['MATLAB returned the following error message - ',ME.message]);
43+
A(i1).error = 1;
44+
end
45+
% else
46+
% fprintf(['Directory ',he_dir,' already exists with all HPFs. Delete directory and try again. \n'])
47+
48+
end
49+
end
50+
toc;
51+
end

emptyTask.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function errStr = emptyTask(i1)
2+
errStr = ['Job ',int2str(i1),' has no fields.'];
3+
4+
end

0 commit comments

Comments
 (0)