-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiveHamWithDMD.m
More file actions
333 lines (289 loc) · 12.9 KB
/
Copy pathLiveHamWithDMD.m
File metadata and controls
333 lines (289 loc) · 12.9 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
% =========================================================================
% Microscope Control & Live Imaging Script
%
% Purposes:
% 1. Live imaging of samples (with option to save the image)
% 2. Align Spatial Light Modulator (SLM) / DMD for proper positioning/rotation
% 3. Generate a laser spot mark on screen for easy laser shooting targeting
%
% Author: Yao Wang
% Email: wang.yao2@northeastern.edu
%
% Hardware:
% - Camera: Hamamatsu Fusion BT (2304x2304)
% - Illumination: Lumencor Sola or Thorlabs LED
% - Microscope: Nikon Ti2
% =========================================================================
clc;
close all;
clearvars; % 'clearvars' is safer and more efficient than 'clear all'
%% 1. Workspace & Path Setup
workDir = 'E:\Yao\Nikon';
cd(workDir);
addpath(workDir);
addpath('C:\Program Files\Nikon\Ti2-SDK\bin'); % Nikon microscope SDK
addpath('E:\Yao\Nikon\ScanningPattern'); % Scanning pattern location
%% 2. General Parameters
CameraExposureTime = 0.1; % Exposure time in seconds
ImageLowLimitForShow = 100; % Lower limit for contrast
ImageName = '20241111-WF-BOS121-Sola20-2a.tif';
AlignMode = 1; % 1: DMD alignment mode, 0: Real imaging (saves image)
IlluminationSource = 1; % 1: Lumencor Sola, 2: Thorlabs LED
LEDOn = 1; % 1: LED on, 0: LED off
LaserMode = 0; % 1: Laser overlay on, 0: Off
LaserPosX = 988; % Laser X coordinate (used if LaserMode == 1)
LaserPosY = 1098; % Laser Y coordinate (used if LaserMode == 1)
LaserShootMarkSize = 13; % Crosshair size for laser spot
NeedImageEnhancement = 1; % 1: Use neuron fiber enhancement filter, 0: Original widefield
FiberThickness = 5; % Parameter for image enhancement filter
DMDOpticalInvert = 1; % 0: DMD normal, 1: DMD inverted
%% 3. Mode-Specific Configurations
if AlignMode == 1
ImageUpLimitForShow = 25000;
CaptureIt = 1;
LEDIntensity = 2; % 0-100 linear representation
ObjectiveMag = 60; % Options: 2, 10, 20, 40, 60 (60x is oil immersion)
CrossAlignNeed = 0;
else % AlignMode == 0
ImageUpLimitForShow = 7000;
CaptureIt = 1;
LEDIntensity = 1;
ObjectiveMag = 60;
CrossAlignNeed = 0; % Assumed 0 for normal imaging, adjust if needed
end
%% --- CRITICAL HARDWARE SETTINGS (Do Not Change Below) ---
UseAOI = 1;
% ImageWidth = 500; % smaller FOV for activity tracking
% ImageHeight = 500; % smaller FOV for activity tracking
ImageWidth = 2304; % camera pixel numbers to match our DMD
ImageHeight = 1868; % camera pixel numbers to match our DMD
ImageOffsetX = (2304 - ImageWidth) / 2;
ImageOffsetY = (2304 - ImageHeight) / 2;
% ROI configuration for DLi9000 DMD [Left, Top, Width, Height]
ROI = [ImageOffsetX, ImageOffsetY, ImageWidth, ImageHeight];
save('HamamatsuROI.mat', 'ROI');
% DMD display initializations
ShowOutlineToDLiDMD; % Figure for DMD alignment
if DMDOpticalInvert == 0
ShowWhiteToDLiDMD; % Used for laser shooting and wide field image
else
ShowDarkToDLiDMD;
end
%% 4. Initialize Camera
disp('Setting up Hamamatsu Camera...');
imaqhwinfo('hamamatsu');
imageFormat = 'MONO16_2304x2304_FastMode';
vid = videoinput('hamamatsu', 1, imageFormat);
src = getselectedsource(vid);
% Apply Camera Settings
vid.ROIPosition = ROI;
src.ExposureTime = CameraExposureTime;
vid.FramesPerTrigger = 1;
triggerconfig(vid, 'manual');
src.HotPixelCorrectionLevel = 'standard';
vid.LoggingMode = 'memory';
src.TriggerPolarity = 'positive';
disp('Starting acquisition...');
start(vid);
%% 5. Setup Live Viewing UI
CenterLinesLeft = ImageWidth/2 - 1;
CenterLinesRight = ImageWidth/2 + 2;
CenterLinesUp = ImageHeight/2 - 1;
CenterLinesDown = ImageHeight/2 + 2;
ImageBuf = uint16(zeros(ImageHeight, ImageWidth));
FigureForCheck = figure('Name', 'Live', 'NumberTitle', 'off', 'Color', 'r');
if NeedImageEnhancement == 0
% Standard UI Setup
set(FigureForCheck, 'Position', [600, 200, ImageWidth*1.2, ImageHeight*1.2]);
h = imshow(ImageBuf, [ImageLowLimitForShow, ImageUpLimitForShow]);
impixelinfo;
hold on;
% Draw SLM Alignment Crosshairs (Drawn ONCE to save memory)
line([CenterLinesLeft-1000 CenterLinesLeft-600],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesLeft-500 CenterLinesLeft-400],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesLeft-300 CenterLinesLeft-200],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesLeft-100 CenterLinesRight+100],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesRight+200 CenterLinesRight+300],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesRight+400 CenterLinesRight+500],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesRight+600 CenterLinesRight+1000],[CenterLinesUp CenterLinesUp], 'Color', 'b');
line([CenterLinesLeft-1000 CenterLinesLeft-600],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesLeft-500 CenterLinesLeft-400],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesLeft-300 CenterLinesLeft-200],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesLeft-100 CenterLinesRight+100],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesRight+200 CenterLinesRight+300],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesRight+400 CenterLinesRight+500],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesRight+600 CenterLinesRight+1000],[CenterLinesDown CenterLinesDown], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesUp-1000 CenterLinesUp-600], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesUp-500 CenterLinesUp-400], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesUp-300 CenterLinesUp-200], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesUp-100 CenterLinesDown+100], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesDown+200 CenterLinesDown+300], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesDown+400 CenterLinesDown+500], 'Color', 'b');
line([CenterLinesLeft CenterLinesLeft],[CenterLinesDown+600 CenterLinesDown+1000], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesUp-1000 CenterLinesUp-600], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesUp-500 CenterLinesUp-400], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesUp-300 CenterLinesUp-200], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesUp-100 CenterLinesDown+100], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesDown+200 CenterLinesDown+300], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesDown+400 CenterLinesDown+500], 'Color', 'b');
line([CenterLinesRight CenterLinesRight],[CenterLinesDown+600 CenterLinesDown+1000], 'Color', 'b');
if LaserMode == 1
% Draw Laser Crosshairs
line([LaserPosX-LaserShootMarkSize LaserPosX+LaserShootMarkSize],[LaserPosY LaserPosY], 'Color', 'r');
line([LaserPosX LaserPosX],[LaserPosY-LaserShootMarkSize LaserPosY+LaserShootMarkSize], 'Color', 'r');
end
hold off;
else
% Image Enhancement UI Setup (Side-by-side view)
set(FigureForCheck, 'Position', [300, 60, 1600, 800]);
ax1 = subplot(1, 2, 1);
h(1) = imshow(ImageBuf, [ImageLowLimitForShow ImageUpLimitForShow], 'Parent', ax1, 'Border', 'tight');
title('Original Widefield Image');
hold on;
if LaserMode == 1
line([LaserPosX-LaserShootMarkSize LaserPosX+LaserShootMarkSize],[LaserPosY LaserPosY], 'Color', 'r');
line([LaserPosX LaserPosX],[LaserPosY-LaserShootMarkSize LaserPosY+LaserShootMarkSize], 'Color', 'r');
end
hold off;
ax2 = subplot(1, 2, 2);
h(2) = imshow(ImageBuf, [0 ImageUpLimitForShow*8], 'Parent', ax2, 'Border', 'tight');
title('Enhanced Widefield Image');
hold on;
if LaserMode == 1
line([LaserPosX-LaserShootMarkSize LaserPosX+LaserShootMarkSize],[LaserPosY LaserPosY], 'Color', 'r');
line([LaserPosX LaserPosX],[LaserPosY-LaserShootMarkSize LaserPosY+LaserShootMarkSize], 'Color', 'r');
end
hold off;
impixelinfo;
linkaxes([ax1 ax2], 'xy');
end
%% 6. Setup Nikon Ti2 Microscope
disp('Connecting to Nikon Ti2...');
!regsvr32 /s NkTi2Ax.dll;
ti2 = actxserver('Nikon.Ti2.AutoConnectMicroscope');
ti2.iXPOSITIONSpeed = 3;
ti2.iYPOSITIONSpeed = 3;
ti2.iZPOSITIONSpeed = 3;
ti2.iLIGHTPATH = 2; % 2: right camera, 4: left camera
ti2.iTURRET2SHUTTER = 0;
ti2.iTURRET2POS = 1;
ti2.iDIA_LAMP_Switch = 0;
ti2.iDIA_LAMP_Pos = 0;
ti2.iTURRET1SHUTTER = 1;
ti2.iTURRET1POS = 1;
switch ObjectiveMag
case 2, ti2.iNOSEPIECE = 6;
case 10, ti2.iNOSEPIECE = 2;
case 20, ti2.iNOSEPIECE = 3;
case 40, ti2.iNOSEPIECE = 1;
case 60, ti2.iNOSEPIECE = 5;
end
%% 7. Setup LED Illumination
if LEDOn == 1
if IlluminationSource == 1
disp('Setting up Sola illumination...');
SolaOn;
Sola(LEDIntensity);
elseif IlluminationSource == 2
disp('Setting up DAQ LEDD1B illumination...');
LEDIntensityForDAQ = (LEDIntensity / 100) * 5; % Linear conversion to voltage (max 5V)
dq = daq("ni");
dq.Rate = 24000;
addoutput(dq, "Dev1", "ao0", "Voltage");
write(dq, LEDIntensityForDAQ);
end
end
%% 8. Alignment / Image Acquisition Loop
if NeedImageEnhancement == 0
set(FigureForCheck, 'Position', [600, 10, ImageWidth*1.1, ImageHeight*1.1]);
end
if CrossAlignNeed == 1
% Setup Cross Profile alignment graphs
FigureForCrossProfile = figure('Name', 'AlignmentProfile', 'NumberTitle', 'off', 'Color', 'g');
set(FigureForCrossProfile, 'Position', [2000, 50, 1400, 400]);
XForPlot = 1:20;
CrossX = subplot(1, 2, 1);
CrossPlot(1) = plot(XForPlot, zeros(1,20), 'r');
xlim([1, 20]); ylim([0, 1.2]); xticks(1:2:20);
title('Alignment in X direction');
CrossY = subplot(1, 2, 2);
CrossPlot(2) = plot(XForPlot, zeros(1,20), 'r');
xlim([1, 20]); ylim([0, 1.2]); xticks(1:2:20);
title('Alignment in Y direction');
end
% === MAIN LIVE LOOP ===
disp('Entering Live View... Close the figure window to stop.');
if NeedImageEnhancement == 0
% Standard Display Loop
while ishandle(h)
ImageBuf = getsnapshot(vid);
if ishandle(h)
set(h,'CData',ImageBuf);
drawnow;
end
if CrossAlignNeed == 1
CrossXdirection = double(ImageBuf(ImageHeight/2 - 50, ImageWidth/2-9:ImageWidth/2+10));
CrossXdirectionNormal = CrossXdirection(:) / max(CrossXdirection(:));
CrossYdirection = double(ImageBuf(ImageHeight/2-9:ImageHeight/2+10, ImageWidth/2-50));
CrossYdirectionNormal = CrossYdirection(:) / max(CrossYdirection(:));
CrossPlot(1).YData = CrossXdirectionNormal;
CrossPlot(2).YData = CrossYdirectionNormal;
end
drawnow;
end
else
% Enhanced Display Loop (Filters applied live)
FiberFilter = [-1*ones(FiberThickness) 2*ones(FiberThickness) -1*ones(FiberThickness)];
while ishandle(h(1)) || ishandle(h(2))
ImageBuf = getsnapshot(vid);
if ishandle(h(1))
set(h(1),'CData',ImageBuf);
drawnow;
end
% Apply directional filtering
buf2EnhancedX = double(imfilter(ImageBuf, FiberFilter, 'replicate'));
buf2EnhancedY = double(imfilter(ImageBuf, FiberFilter', 'replicate'));
buf2EnhancedX(buf2EnhancedX < 0) = 0;
buf2EnhancedY(buf2EnhancedY < 0) = 0;
buf2Enhanced = sqrt(buf2EnhancedX.^2 + buf2EnhancedY.^2);
if ishandle(h(2))
set(h(2), 'CData', buf2Enhanced);
drawnow;
end
end
end
%% 9. Hardware Shutdown & Cleanup
close all;
% Turn off illumination
if LEDOn == 1
if IlluminationSource == 1
SolaOff;
disp('Sola illumination disconnected.');
elseif IlluminationSource == 2
write(dq, 0);
daqreset;
disp('DAQ LED disconnected.');
end
end
% Save the 2D image
if CaptureIt == 1
imwrite(ImageBuf, ImageName);
disp(['Image saved as ', ImageName]);
end
% Disconnect Camera & Microscope
stop(vid);
delete(vid);
disp('Camera Disconnected.');
zposition = get(ti2, 'iZPOSITION');
xposition = get(ti2, 'iXPOSITION');
yposition = get(ti2, 'iYPOSITION');
% Synchronize UI positions back to scope (if applicable to your SDK wrapper)
try
ti2.ZPosition.Value = zposition;
ti2.XPosition.Value = xposition;
ti2.YPosition.Value = yposition;
catch
% Failsafe in case exact property names vary by SDK version
end
save('Position.mat', 'zposition', 'xposition', 'yposition');
disp('Acquisition complete. Positions saved.');