Switch to unified view

a b/combinedDeepLearningActiveContour/dcom2matES.m
1
% read dcom images and contours and save them into mat files
2
clc 
3
close all
4
clear all
5
addpath('functions');
6
imset='validation';
7
%imset='training';
8
%imset='online';
9
%%
10
patient=4;
11
12
% region of interest
13
Mroi=100;
14
%% read direcotory of images
15
folder=['dcom/',imset,'/ES',num2str(patient),'/images'];
16
%folder=uigetdir;
17
[IMAGES, xthickness, ythickness, zthickness] = gatherImages(folder);
18
19
[x_max,y_max,z_max]=size(IMAGES);
20
scale=Mroi/x_max;
21
 for k=1:z_max
22
 subplot(3,5,k)
23
 imagesc(IMAGES(:,:,k));colormap(gray);
24
 end
25
26
%% read directory of contours
27
current_dir=pwd;
28
%contour_dir_MC=['dcom/',imset,'/ED',num2str(patient),'/contours/MC/'];
29
contour_dir_LV=['dcom/',imset,'/ES',num2str(patient),'/contours/LV/'];
30
31
cd(contour_dir_LV);
32
LV_contours_names = dir('*.txt'); 
33
numFiles = size(LV_contours_names,1);
34
35
% load contours of LV
36
for k=1:numFiles
37
    contours{k}=load (LV_contours_names(k,:).name);
38
end
39
cd(current_dir);
40
41
42
nLarge=z_max;
43
yROI=zeros(x_max,y_max,nLarge);
44
Iroi=zeros(Mroi,Mroi,nLarge);
45
for k=1:z_max
46
47
    % show images
48
    figure (1)
49
    subplot(3,5,k)
50
    I1=IMAGES(:,:,k);
51
    imagesc(I1);
52
    colormap(gray);
53
    title(['image',num2str(k)])
54
    hold on
55
   
56
    % read contour and plot it
57
    C_LV=contours{k};
58
    Cx_LV=C_LV(:,1);Cy_LV=C_LV(:,2);
59
    
60
    % find the contour center and plot it
61
    [junk,xcnt,ycnt]=polycenter(Cx_LV,Cy_LV);
62
    plot(Cx_LV,Cy_LV,'r','LineWidth',2);   
63
    plot(xcnt,ycnt,'r*','markerSize',12);
64
65
    % creat segmentation mask and display it
66
    figure(4)
67
    subplot(3,5,k)
68
    LV_seg(:,:,k)= roipoly(I1,Cx_LV, Cy_LV);    
69
    imagesc(LV_seg(:,:,k));
70
    colormap(gray)
71
    title(['mask',num2str(k)])
72
    hold on
73
    plot(Cx_LV,Cy_LV,'b','LineWidth',2)
74
    plot(xcnt,ycnt,'b*','markerSize',12); 
75
   
76
    x_cnt=round(xcnt);
77
    y_cnt=round(ycnt);
78
    contour_center{k}=[x_cnt,y_cnt];
79
    
80
    % define a rectangle centered at contour
81
    x_roi=x_cnt-Mroi/2:x_cnt+Mroi/2-1;
82
    y_roi=y_cnt-Mroi/2:y_cnt+Mroi/2-1;
83
    xroi=[x_cnt-Mroi/2,x_cnt+Mroi/2,x_cnt+Mroi/2,x_cnt-Mroi/2,x_cnt-Mroi/2];
84
    yroi=[y_cnt-Mroi/2,y_cnt-Mroi/2,y_cnt+Mroi/2,y_cnt+Mroi/2,y_cnt-Mroi/2];
85
86
    % create ROI mask: this will be used for training of DL-ROI
87
    figure(1)
88
    subplot(3,5,k)
89
    yROI(:,:,k)=poly2mask(xroi,yroi,x_max,y_max);
90
    contour(yROI(:,:,k),[0 0],'r')
91
    plot(xcnt,ycnt,'b*','markerSize',12);
92
    
93
    % find ROI in the mask: this will be used as training data for DL-LV
94
    figure(5)
95
    subplot(3,5,k)
96
    yLV(:,:,k)=LV_seg(y_roi,x_roi,k);
97
    imshow(yLV(:,:,k));
98
    title('yLV')
99
    hold on
100
    plot(50,50,'r*','markerSize',12);
101
102
103
    % ROI in the image: this will be used as input training data for DL-LV 
104
    figure(3)
105
    subplot(3,5,k)
106
    Iroi(:,:,k)=I1(y_roi,x_roi);
107
    imagesc(Iroi(:,:,k));
108
    colormap(gray);
109
    hold on
110
    plot(10,50,'r*','markerSize',12);
111
    contour(yLV(:,:,k),[0 0],'r','LineWidth',2)
112
    %plot(Cx_LV-x_cnt+Mroi/2+1,Cy_LV-y_cnt+Mroi/2+1,'r')
113
    
114
    % make sure that mask is ok
115
    %figure(6)
116
    %subplot(3,4,k)
117
    %I_LV(:,:,k)=LV_mask(:,:,k).*Iroi(:,:,k);
118
    %imagesc(I_LV(:,:,k));
119
    %title('LV')
120
    %colormap(gray);
121
    %hold on
122
    %plot(50,50,'r*','markerSize',12);
123
124
    % convert mask to poly for ROI
125
    %figure(6)
126
    %contour(LV_mask(:,:,k));
127
    %plot(xylv(:,1),xylv(:,2),'b')
128
    
129
end
130
%% store images and region of interest on disk
131
% P# stands for patient numebr
132
% ED stands for End Diastole
133
% yROI : this is a mask of ROI which is used as the output for traning the 
134
% DL ROI
135
% Iroi : this is part of the image that interests us and it will be used as
136
% the input of DL-LV segmentation
137
% yLV : this is a mask which is used as the output for training the DL-LV
138
% segmentation
139
I=IMAGES(:,:,1:nLarge);
140
filename=['matFiles/',imset,'/ES/ES_P',num2str(patient)];
141
save (filename, 'I', 'yROI','Iroi','yLV','contours','patient','xthickness', 'ythickness' ,'zthickness','contour_center','LV_contours_names');
142
disp('results saved')
143
144
% make sure sizes are matched
145
sizes=[size(I,3),size(Iroi,3),size(yLV,3),size(yROI,3)]
146
%%
147
%figure
148
%h1=disp3d(flipdim(LV_seg,3),'red',7);
149
%hold on
150
%h2=disp3d(flipdim(MC_seg,3),'g',7);
151
%alpha(.6)
152
153
%colormap(gray); 
154
%z1 = ceil(1);
155
%z1=z_max;
156
%I1 =  IMAGES(:,:,z1); 
157
%xImage = [1 y_max; 1 y_max]; %  The x data for the image corners
158
%yImage = [1 1 ; x_max, x_max]; %  The y data for the image corners
159
%zImage = (z_max-z1+1) * ones(2,2);   % The z data for the image corners
160
161
%surf(xImage,yImage,zImage,...    %  Plot the surface
162
%     'CData',I1 ,...
163
%     'FaceColor','texturemap'); 
164
165