-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFanReconWithAlignment.m
More file actions
201 lines (174 loc) · 6.61 KB
/
Copy pathFanReconWithAlignment.m
File metadata and controls
201 lines (174 loc) · 6.61 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function [data, reconTime] = FanReconWithAlignment(filePrefix)
%process video into stack of useful images
imageList = dir('*.JPG') ;
[m,n] = size(imageList);
theta = 180/m;
testImage = imread(imageList(1).name);
% testImage = undistortImage(testImage,cameraParams);
figure
imshow(testImage);
[J, rect] = imcrop(testImage);
% if rect(3)>rect(4)
% rect(3)=rect(4);
% elseif rect(4)>rect(3)
% rect(4)=rect(3);
% else
% rect = rect;
% end
% assumes the images is landscape
rect(3) = rect(4);
testImage = imcrop(testImage,rect);
disp('Getting image dimensions ...')
imHeight = size(testImage,1);
imWidth = size(testImage,2);
%read in the movie frames to a structure
%when the loop is done there will be a structure with as many entries as
%there are movie frames. The structure will have a cdata field with teh
%image data and a cmap field with the color map
imFrames = struct('cdata',zeros(imHeight,imWidth,3,'uint8'),...
'colormap',[]);
scaledFrames = struct('cdata',zeros(imHeight,imWidth,3,'uint8'),...
'colormap',[]);
disp('Creating Image Frame Structure ...')
for i = 1:m
imFrames(i).cdata = imcrop(imread(imageList(i).name),rect);
% imFrames(i).cdata = imcrop(undistortImage(imread(imageList(i).name),cameraParams),rect);
% scaledFrames(i).cdata = imresize(imcrop(undistortImage(imread(imageList(i).name),cameraParams),rect),0.2);
scaledFrames(i).cdata = imresize(imcrop(imread(imageList(i).name),rect),0.5);
imshow(imFrames(i).cdata);
end
disp('Calculating Alignment ...')
mult = adjustAlignment(scaledFrames,theta);
disp('Applying Alignment to full stack ...')
imFrames = alignStack(imFrames,theta,mult);
%set aside teh memory
disp('Setting aside memory for data structures...')
for i = 1:1:imHeight
data(i).RrowVals = [];
data(i).GrowVals = [];
data(i).BrowVals = [];
end
% make the sinograms
disp('Making the sinograms ...')
for i = 1:1:m
currentImage = imFrames(i).cdata;
redImage = currentImage(:,:,1);
greenImage = currentImage(:,:,2);
blueImage = currentImage(:,:,3);
for j = 1:1:size(redImage,1)
data(j).RrowVals = [data(j).RrowVals; redImage(j,:)];
data(j).GrowVals = [data(j).GrowVals; greenImage(j,:)];
data(j).BrowVals = [data(j).BrowVals; blueImage(j,:)];
end
disp(['Sinogram ', num2str(i), ' out of ', num2str(m), ' complete.'])
end
% make the individual channels and the RGB image
currentDirectory = pwd;
directoryString = [filePrefix,'_slices'];
redDirString = [filePrefix,'_redSlices'];
greDirString = [filePrefix,'_greSlices'];
bluDirString = [filePrefix,'_bluSlices'];
mkdir(directoryString)
mkdir(redDirString)
mkdir(greDirString)
mkdir(bluDirString)
%cd(directoryString)
disp('Reconstructing the images ...')
tic
figure
for i = 1:1:size(data,2) % this should be 1:1:size(data,2)
rRecondImage = 256*iradon(data(i).RrowVals',theta, 'Cosine');
gRecondImage = 256*iradon(data(i).GrowVals',theta, 'Cosine');
bRecondImage = 256*iradon(data(i).BrowVals',theta, 'Cosine');
% imwrite(rRecondImage, [pwd,'\',redDirString, '\Red', filePrefix,'_', num2str(i,'%03i'),'.tif'],'tif')
% imwrite(gRecondImage, [pwd,'\',greDirString, '\Gre', filePrefix,'_', num2str(i,'%03i'),'.tif'],'tif')
% imwrite(bRecondImage, [pwd,'\',bluDirString, '\Blu', filePrefix,'_', num2str(i,'%03i'),'.tif'],'tif')
recondImage = cat(3,rRecondImage,gRecondImage, bRecondImage);
imshow(recondImage);
imwrite(recondImage, [pwd,'\',directoryString, '\', filePrefix,'_', num2str(i,'%03i'),'.tif'],'tif')
disp(['Reconstruction ', num2str(i), ' out of ', num2str(size(data,2)), ' complete.'])
end
reconTime = toc
cd ..
%imageExample = imread(imageList(1).name);
%imshow(rowVals);
%output_size = max(size(imageExample));
end
function outFrames = alignStack(inFrames,theta, mult)
%get the first and last frame. Estimate the translation distance between
%them
% startFrame = rgb2gray(inFrames(1).cdata);
% endFrame = fliplr(rgb2gray(inFrames(end).cdata));
% [optimizer, metric] = imregconfig('multimodal');
% optimizer.InitialRadius = 0.009;
% optimizer.Epsilon = 1.5e-4;
% optimizer.GrowthFactor = 1.01;
% optimizer.MaximumIterations = 100;
% tform = imregtform(startFrame,endFrame, 'translation', optimizer, metric);
disp('Aligning Images ...')
frNum = size(inFrames,2);
for i = 1:frNum
outFrames(i).cdata(:,:,1) = imtranslate(inFrames(i).cdata(:,:,1),...
[mult,0]);
outFrames(i).cdata(:,:,2) = imtranslate(inFrames(i).cdata(:,:,2),...
[mult,0]);
outFrames(i).cdata(:,:,3) = imtranslate(inFrames(i).cdata(:,:,3),...
[mult,0]);
end
end
function mult = adjustAlignment(inFrames,theta)
%get the first and last frame. Estimate the translation distance between
%them
startFrame = rgb2gray(inFrames(1).cdata);
endFrame = fliplr(rgb2gray(inFrames(end).cdata));
[optimizer, metric] = imregconfig('multimodal');
optimizer.InitialRadius = 0.009;
optimizer.Epsilon = 1.5e-4;
optimizer.GrowthFactor = 1.01;
optimizer.MaximumIterations = 100;
tform = imregtform(startFrame,endFrame, 'translation', optimizer, metric);
aligned = 0;
mult = round(tform.T(3,1)/2);
while aligned ~= 3
disp('Aligning Images ...')
frNum = size(inFrames,2);
for i = 1:frNum
outFrames(i).cdata(:,:,1) = imtranslate(inFrames(i).cdata(:,:,1),...
[mult,0]);
outFrames(i).cdata(:,:,2) = imtranslate(inFrames(i).cdata(:,:,2),...
[mult,0]);
outFrames(i).cdata(:,:,3) = imtranslate(inFrames(i).cdata(:,:,3),...
[mult,0]);
end
RrowVals = [];GrowVals = [];BrowVals = [];
for i = 1:size(outFrames,2)
currentImage = outFrames(i).cdata;
redImage = currentImage(:,:,1);
greenImage = currentImage(:,:,2);
blueImage = currentImage(:,:,3);
[rows, cols] = size(redImage);
RrowVals = [RrowVals; redImage(round((rows/4*3)),:)];
GrowVals = [GrowVals; greenImage(round(rows/4*3),:)];
BrowVals = [BrowVals; blueImage(round(rows/4*3),:)];
end
rtestRecondImage = 256*iradon(RrowVals',theta, 'Cosine');
gtestRecondImage = 256*iradon(GrowVals',theta, 'Cosine');
btestRecondImage = 256*iradon(BrowVals',theta, 'Cosine');
testRecondImage = cat(3,rtestRecondImage, gtestRecondImage, btestRecondImage);
imshow(testRecondImage);
aligned = input('Left(1,2), Right(4,5), or does this look OK? (3)');
if aligned == 1
mult = mult + 10;
elseif aligned == 2
mult = mult + 1;
elseif aligned == 3
mult = mult;
elseif aligned == 4
mult = mult - 1;
elseif aligned == 5
mult = mult -10;
end
end
end
function outFrames = deWarpImages(inFrames)
end