%To determine the territory of HC and quantify the territory area and the overlap between every two adjacent HCs %Images are numbered as time (e.g. 16) directly followed by 0 (whole image) or cell number (e.g. 2). %Examples: 30 means the time point 3 and the whole image; 161 means the time point 16 and the cell image 1. %Variable "result" is for each image; Variable "resultAll" accumulates all images. close all; clear variables; clc; %Input file directory and partial name (leaving the number part for looping purpose) directory = ; name1 = ; name2 = ; %Input a region as tissue background for background removal; Make sure this region does not include any HC during the whole movie backgroundDepthStart = ; backgroundDepthEnd = ; backgroundTransverseStart = ; backgroundTransverseEnd = ; %Input the number of HC and time lapse images numberHC = ; numberImage = ; %Input parameters for the territory radius in pixel; %Calculated based on pixel interval and the territory regin size; %Example: radius of 12 for a target of 10 um diameter disk region size and a 0.42 um pixel interval. territorySize = ; %Diameter; unit um pixelInterval = ; %Unit um radius = round(territorySize/pixelInterval/2); %Go through each image for imageNumber = 1:numberImage %Load the whole image containing all cells rawImageWhole = imread(strcat(directory,name1,int2str(imageNumber),'0',name2)); %Show the image figure(1); imshow(rawImageWhole,[]); title(strcat('Original Image Time',num2str(imageNumber))); %Set the background threshold as %95 CI value (mean+1.96std) backgroundRegion = rawImageWhole(backgroundDepthStart:backgroundDepthEnd,backgroundTransverseStart:backgroundTransverseEnd); backgroundRegionVector = double(reshape(backgroundRegion,[(backgroundDepthEnd-backgroundDepthStart+1)*(backgroundTransverseEnd-backgroundTransverseStart+1) 1])); backgroundThreshold = mean(backgroundRegionVector(backgroundRegionVector~=0))+1.96*std(backgroundRegionVector(backgroundRegionVector~=0)); %Get the size of the whole image [imageSizeY,imageSizeX] = size(rawImageWhole); %Initialize variables territoryImageAll(1:imageSizeY,1:imageSizeX,1:numberHC) = 0; territoryAreaIndividual(1:numberHC) = 0; territoryOverlapImage(1:imageSizeY,1:imageSizeX) = 0; territoryArea = 0; [columnsInImage,rowsInImage] = meshgrid(1:imageSizeX,1:imageSizeY); %Go through each cell that was manually selected and seperated using ImageJ for cellNumber = 1:numberHC %Initialize the variable territoryImage(1:imageSizeY,1:imageSizeX) = 0; %Load the particular cell image rawImage = imread(strcat(directory,name1,int2str(imageNumber),num2str(cellNumber),name2)); %Show the image figure(2+cellNumber-1+numberHC*(cellNumber-1));imshow(rawImage,[]); title(strcat('HC Cell No.',int2str(cellNumber),' Time',num2str(imageNumber))); %Background removal imageNoBackground = rawImage; imageNoBackground(imageNoBackground<=backgroundThreshold) = 0; %Filter the image to further remove tissue background filteredImage = medfilt2(imageNoBackground,[2 2]); figure(3+cellNumber-1+numberHC*(cellNumber-1)); imshow(filteredImage,[]); title(strcat('Background-Removed and Filtered HC Image',' HC Cell No.',int2str(cellNumber),' Time',num2str(imageNumber))); %Binarize the HC image binaryThreshold = ; %Input the threshold. This threshold is set to further remove the remaining tissue background; The value should be established based on filteredImage. binaryImage = filteredImage; binaryImage(:,:) = 0; binaryImage(filteredImage>binaryThreshold) = 1; figure(4+cellNumber-1+numberHC*(cellNumber-1)); imshow(binaryImage,[]); title(strcat('Binarized HC Image',' HC Cell No.',int2str(cellNumber),' Time',num2str(imageNumber))); %Generate the territory centered at the HC cell positive pixels for x = 1:imageSizeX for y = 1:imageSizeY if binaryImage(y,x) == 1 territoryImageTemp = (rowsInImage - y).^2 + (columnsInImage - x).^2 <= radius.^2; territoryImage = territoryImage | territoryImageTemp; end end end %Perform morphological closing with a dilation followed by an erosion se = strel('disk',radius); %using the same diameter disk. territoryImageClosed = imclose(territoryImage,se); %Show the territory image figure(5+cellNumber-1+numberHC*(cellNumber-1)); imshow(territoryImageClosed,[]); title(strcat('Territory Image',' HC Cell No.',int2str(cellNumber),' Time',num2str(imageNumber))); %Calculate the individual territory area and collect the territory image territoryAreaIndividual(cellNumber) = sum(sum(territoryImageClosed(:,:)==1)); territoryImageAll(:,:,cellNumber) = double(territoryImageClosed); end %Calculate the true area and store individual territory area result(1,1:numberHC) = territoryAreaIndividual*pixelInterval*pixelInterval; resultAll(imageNumber,1:numberHC) = result(1,1:numberHC); %Calculate the overlap region area and percentage between every two adjacent cells for i = 2:numberHC %Calculation and show the overlap image territoryAreaSum = sum(territoryAreaIndividual(i-1:i)); territoryOverlapImage = territoryImageAll(:,:,i-1) + territoryImageAll(:,:,i); territoryOverlapArea = sum(sum(territoryOverlapImage(:,:)==2)); overlapPercentage = territoryOverlapArea/territoryAreaSum; %Show the overlap image figure(5+numberHC+i-2+numberHC*(numberHC-1)); imshow(territoryOverlapImage,[0 2]); title(strcat('Overlap between',' HC Cell No.',int2str(i-1),' and',' HC Cell No.',int2str(i),' Time',num2str(imageNumber))); %Calculate the true area and store the results result(i,1) = territoryOverlapArea*pixelInterval*pixelInterval; result(i,2) = territoryAreaSum*pixelInterval*pixelInterval; result(i,3) = overlapPercentage; resultAll(imageNumber,(numberHC+1)+(3*(i-2)):numberHC+3+(3*(i-2))) = result(i,1:3); end end