Created
October 25, 2018 15:30
-
-
Save jcasado/333c8b4b0625f16a70e226bfdda4ce1b to your computer and use it in GitHub Desktop.
TMA Dearraying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 1. Input using Bio-formats | |
% Read image into 3D array and resize to be easy to handle | |
filePath = "tma_big.tif"; | |
image = bfOpen3DVolume(filePath); | |
image= image{1}{1}; | |
nuclei = image(:,:,1); | |
imagesub = imresize(nuclei,0.02); | |
%% 2. Morphological operations | |
% Edge detection | |
imageEdge = edge(imagesub,'Canny'); | |
% Smooth - close gaps of 3px | |
mask = imclose(imageEdge,strel('disk',3)); | |
% Smooth - fill blanks from closed shapes | |
mask = imfill(mask,'holes'); | |
% Smooth - remove objects of less than 50 px | |
mask = bwareaopen(mask,50); | |
%% 3. Option A: Find circular objects | |
Rmin = 9; | |
Rmax = 14; | |
s = 0.95; | |
et = 0.05; | |
[centers, radii] = imfindcircles(mask,[Rmin Rmax],'ObjectPolarity','bright','Sensitivity',s,'EdgeThreshold',et); | |
% Plot bright circles in blue | |
imshowpair(imagesub,mask); | |
viscircles(centers, radii,'Color','b'); | |
% Or plot centroids | |
hold on; | |
plot(centers(:,1), centers(:,2), 'r*'); | |
% Create cropping areas as array of bounding boxes | |
coreSize = 28; | |
corners = centers-coreSize/2; | |
boundingBox = [corners coreSize+zeros(size(corners))]; | |
numCores = length(centers); | |
%% 3. Option B: Use regionprops | |
stats=regionprops(mask,'Centroid','Area','BoundingBox'); | |
centroids = cat(1, stats.Centroid); | |
% Plot centroids over image | |
imshowpair(imagesub,mask); | |
hold on; | |
plot(centroids(:,1), centroids(:,2), 'r*'); | |
% Create cropping areas as array of bounding boxes | |
boundingBox = stats(iTMA).BoundingBox | |
numCores = numel(stats) | |
%% 4. Save cores as Tiff files | |
myFactor = 50; | |
outputFolder = "tma_data/cores/" | |
filePrefix = "TMA" | |
for iTMA = 1:2 | |
TMA=[]; | |
for iChan=1:size(image,3) | |
TMA(:,:,iChan) = imcrop(image(:,:,iChan),boundingBox(iTMA,:)*myFactor); | |
end | |
bfsave(TMA,[outputFolder filePrefix int2str(iTMA) '.tif'],'BigTiff',true); | |
end |
Yu-AnChen
commented
Oct 29, 2018
•
%% Calculate cropping area
coreSize = 62;
corners = sorted_centers(:,1:2)-coreSize/2;
boundingBox = [corners coreSize+zeros(size(corners))];
numCores = length(sorted_centers(:,1:2));
r = bfGetReader(char("TMA5.ome.tif"));
%% Crop boundingBox sizes if out of image
boundingBox = uint16(boundingBox*myFactor);
maxX = r.getSizeX();
maxY = r.getSizeY();
cornerX = boundingBox(:,1) + boundingBox(:,3);
cornerY = boundingBox(:,2) + boundingBox(:,4);
cornerX(cornerX > maxX) = maxX;
cornerY(cornerY > maxY) = maxY;
boundingBox(:,3) = cornerX - boundingBox(:,1);
boundingBox(:,4) = cornerY - boundingBox(:,2);
outputFolder="D:\CAJ_AbVal10_TMA5_7\TMA5\cores\";
filePrefix="core_";
tic
for iCore = 48:numCores
core=[];
display(iCore)
for iChan=1:r.getImageCount()
iBox = num2cell(boundingBox(iCore,:));
core(:,:,iChan) =bfGetPlane(r, iChan, iBox{:});
end
bfsave(core,char(strcat(outputFolder, filePrefix,int2str(iCore),'.tif')),'BigTiff',true);
end
toc
I added one line in my comment because the minimum of boundingBox corners is 1 instead of 0.
boundingBox = uint16(boundingBox*myFactor);
boundingBox(boundingBox == 0) = 1;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment