function []=AutoStack()
bg =imread("bg.tif");
Folder = pwd;% Get current path
dataPath =fullfile(Folder,"All");
pattern_up ='_01';
pattern_down ='_02';
pattern_SPLEEM ='_03';
fileList =dir(dataPath);
up ={};
down ={};
SPLEEM ={};for i =1:length(fileList)
fileName =fileList(i).name;% read LEEM_up
ifcontains(fileName,pattern_up)&&endsWith(fileName,'.tif')
fullPath =fullfile(dataPath, fileName);
up{end+1}=imread(fullPath);
end
% read LEEM_down
ifcontains(fileName,pattern_down)&&endsWith(fileName,'.tif')
fullPath =fullfile(dataPath, fileName);
down{end+1}=imread(fullPath);
end
% read SPLEEM
ifcontains(fileName,pattern_SPLEEM)&&endsWith(fileName,'.tif')
fullPath =fullfile(dataPath, fileName);
SPLEEM{end+1}=imread(fullPath);
end
end
numFrames =length(up);for i =1:numFrames
% Deducting hexagonal backgrounds
up_hex =double(cell2mat(up(i)));
down_hex =double(cell2mat(down(i)));
SPLEEM_i =cell2mat(SPLEEM(i));
up_i = up_hex./bg;
up_i =uint16(up_i);
down_i = down_hex./bg;
down_i =uint16(down_i);imwrite(up_i,'Stack_LEEMup.tif',"WriteMode","append")imwrite(down_i,'Stack_LEEMdown.tif',"WriteMode","append")imwrite(SPLEEM_i,'Stack_SPLEEM.tif',"WriteMode","append")
end
end
2. 对imagestack按照指定区域特征registration—函数AutoDC()
function []=AutoDC()%file =dir('*.tif');% Specify the image stack file path
DCStackFile ="DCstack.tif";% Get information about the image stack using imfinfo
imageInfo =imfinfo(DCStackFile);
numFrames =numel(imageInfo);% Number of frames in the image stack
% Preallocate a 3D array to store the image stack
imageStack1 =zeros(imageInfo(1).Height,imageInfo(1).Width, numFrames,'uint16');% Read each frame in the image stack
for i =1:numFrames
imageStack1(:,:, i)=imread(DCStackFile, i);
end
% Specify the image stack file path
SPLEEMStackFile ="Stack_SPLEEM.tif";
LEEMupStackFile ="Stack_LEEMup.tif";
LEEMdownStackFile ="Stack_LEEMdown.tif";% Get information about the image stack using imfinfo
imageInfo =imfinfo(SPLEEMStackFile);
numFrames =numel(imageInfo);% Number of frames in the image stack
% Preallocate a 3D array to store the image stack
imageStack2 =zeros(imageInfo(1).Height,imageInfo(1).Width, numFrames,'uint16');
imageStack3 =zeros(imageInfo(1).Height,imageInfo(1).Width, numFrames,'uint16');
imageStack4 =zeros(imageInfo(1).Height,imageInfo(1).Width, numFrames,'uint16');% Read each frame in the image stack
for i =1:numFrames
imageStack2(:,:, i)=imread(SPLEEMStackFile, i);imageStack3(:,:, i)=imread(LEEMupStackFile, i);imageStack4(:,:, i)=imread(LEEMdownStackFile, i);
end
% Initialize the corrected image stack
correctedImageStack =zeros(size(imageStack1));
correctedSPLEEMStack =zeros(size(imageStack2));
correctedLEEMupStack =zeros(size(imageStack3));
correctedLEEMdownStack =zeros(size(imageStack4));
TransformationVector =zeros(1,2,numFrames);% Loop over each frame in the image stack
for i =1:numFrames
% First frame is the reference frame
if i ==1correctedImageStack(:,:,i)=imageStack1(:,:,i);
referenceFrame =imageStack1(:,:,i);else%figure(1)%imshowpair(referenceFrame,imageStack(:,:,i))% Compute cross-correlation between current frame and reference frame
crossCorr =normxcorr2(referenceFrame,imageStack1(:,:,i));%figure(2)%surf(crossCorr)% shading flat
% Find the peak of the cross-correlation
[~, peakLoc]=max(crossCorr(:));[ypeak, xpeak]=ind2sub(size(crossCorr), peakLoc);% Compute the drift vector
yoffSet = ypeak-size(referenceFrame,1);
xoffSet = xpeak-size(referenceFrame,2);
driftVector =[xoffSet, yoffSet];TransformationVector(:,:,i)= driftVector;% Shift the current frame to correct for drift
correctedImageStack(:,:,i)=imtranslate(imageStack1(:,:,i),-driftVector);correctedSPLEEMStack(:,:,i)=imtranslate(imageStack2(:,:,i),-driftVector);correctedLEEMupStack(:,:,i)=imtranslate(imageStack3(:,:,i),-driftVector);correctedLEEMdownStack(:,:,i)=imtranslate(imageStack4(:,:,i),-driftVector);
end
end
correctedImageStack =uint16(correctedImageStack);% Display the corrected image stack
%montage(correctedImageStack,'DisplayRange',[])for i =1:numFrames
imwrite(correctedImageStack(:,:,i),'DCstack_DC.tif',"WriteMode","append")
end
%% mean specific slice:1 to 175% correctedSPLEEMStack =correctedSPLEEMStack(:,:,1:175);% correctedLEEMupStack =correctedLEEMupStack(:,:,1:175);% correctedLEEMdownStack =correctedLEEMdownStack(:,:,1:175);
SPLEEM_DC =mean(correctedSPLEEMStack,3);
SPLEEM_DC =uint16(SPLEEM_DC);
LEEMup_DC =mean(correctedLEEMupStack,3);
LEEMup_DC =uint16(LEEMup_DC);
LEEMdown_DC =mean(correctedLEEMdownStack,3);
LEEMdown_DC =uint16(LEEMdown_DC);
LEEM_DC = LEEMup_DC + LEEMdown_DC;% imwrite cannot write 32-bit data
imwrite(SPLEEM_DC,'DC_SPLEEM.tif')imwrite(LEEMup_DC,'DC_LEEMup.tif')imwrite(LEEMdown_DC,'DC_LEEMdown.tif')imwrite(LEEM_DC,'DC_LEEM.tif')% t =Tiff('SPLEEM_DC.tiff','w');%%% Setup tags
%% Lots of info here:%% http://www.mathworks.com/help/matlab/ref/tiffclass.html% tagstruct.ImageLength =size(SPLEEM_DC,1);% tagstruct.ImageWidth =size(SPLEEM_DC,2);% tagstruct.Photometric = Tiff.Photometric.MinIsBlack;% tagstruct.BitsPerSample =32;% tagstruct.SamplesPerPixel =1;% tagstruct.RowsPerStrip =16;% tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;% tagstruct.Software ='MATLAB';% t.setTag(tagstruct)% t.write(SPLEEM_DC);% t.close();
end