Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmader/7dbf24ff2c1e91734f70 to your computer and use it in GitHub Desktop.
Save kmader/7dbf24ff2c1e91734f70 to your computer and use it in GitHub Desktop.
Segment and Analyze the Binarized Battery Images from http://onlinelibrary.wiley.com/doi/10.1002/aenm.201200932/suppinfo
%% setup the image
imgPath='/scratch/Volumes/DataStorage/DataArchive/NMC_90wt_0bar/binarized/'
imgSuffix='*.tif'
img_list=dir([imgPath imgSuffix])
cImgName=@(i) [imgPath img_list(i).name];
cImgData=@(i) imread(cImgName(i));
sliceDim=size(cImgData(1));
volumeDim=[sliceDim length(img_list)];
%% load the images
rawVolume=false(volumeDim,'logical');
parfor i=1:length(img_list)
rawVolume(:,:,i)=feval(cImgData,i)>0;
end
%% remove defects
% Defect removal: Holes (imfill) and cracks (imdilate, imreconstruct) in the white
% regions are filled to lower over-segmentation in consecutive process steps.
outVolume = ??
%% examine a single slice
cImg=outVolume(:,:,50);
cCL=bwlabel(cImg);
% for recoloring the CL image
clSizeDist=hist(cCL(:),1:max(cCL(:)));
cCLs=zeros(size(cCL));
for i=1:length(clSizeDist)
cCLs(find(cCL==i))=log(clSizeDist(i));
end
% for watershed
cDist=bwdist(~cImg);
cDist = -cDist;
cDist(~cImg) = -Inf;
cWS=double(watershed(cDist));
% for recoloring watershed image
wsSizeDist=hist(double(cWS(:)),1:max(cWS(:)));
cWSs=zeros(size(cWS));
for i=1:length(wsSizeDist)
cWSs(find(cWS==i))=log(wsSizeDist(i));
end
subplot(2,2,1)
imagesc(cImg);
subplot(2,2,2);
imagesc(cCLs);
title('Component Labeling Colored by LogSize')
subplot(2,2,3);
imagesc(cDist);
title('Distance Map')
subplot(2,2,4);
imagesc(cWSs);
title('Watershed')
%% downsample by a factor of 2 x 2 x 2
smVolume=imresize(outVolume,0.5);
%% examine a 40 slices
cImg=smVolume(:,:,40:80);
% _bwlabeln_ instead of _bwlabel_ so it works for 3D images
cCL=bwlabeln(cImg);
% for recoloring the CL image
clSizeDist=hist(cCL(:),1:max(cCL(:)));
cCLs=zeros(size(cCL));
for i=1:length(clSizeDist)
cCLs(find(cCL==i))=log(clSizeDist(i));
end
% for watershed
cDist=bwdist(~cImg);
cDist = -cDist;
cDist(~cImg) = -Inf;
cWS=double(watershed(cDist));
% for recoloring watershed image
wsSizeDist=hist(double(cWS(:)),1:max(cWS(:)));
cWSs=zeros(size(cWS));
for i=1:length(wsSizeDist)
cWSs(find(cWS==i))=log(wsSizeDist(i));
end
%% show the 10 slices
figure(2)
subplot(2,2,1)
imagesc(cImg(:,:,20));
title('Z Slice 5 of 40 slices')
subplot(2,2,2);
imagesc(cCLs(:,:,20));
title('Component Labeling Colored by LogSize')
subplot(2,2,3);
imagesc(cDist(:,:,20));
title('Distance Map')
subplot(2,2,4);
imagesc(cWSs(:,:,20));
title('Watershed')
%%
figure(3)
slice(1:size(cImg,1),1:size(cImg,2),1:size(cImg,3),double(cDist),[200],[200],[20])
set(findobj(gca,'Type','Surface'),'EdgeColor','none')
%% whole volume
%% examine a 40 slices
cImg=imresize(smVolume,0.5);
% _bwlabeln_ instead of _bwlabel_ so it works for 3D images
cCL=bwlabeln(cImg);
% for recoloring the CL image
clSizeDist=hist(cCL(:),1:max(cCL(:)));
cCLs=zeros(size(cCL));
for i=1:length(clSizeDist)
cCLs(find(cCL==i))=log(clSizeDist(i));
end
% for watershed
cDist=bwdist(~cImg);
cDist = -cDist;
cDist(~cImg) = -Inf;
cWS=double(watershed(cDist));
% for recoloring watershed image
wsSizeDist=hist(double(cWS(:)),1:max(cWS(:)));
cWSs=zeros(size(cWS));
for i=1:length(wsSizeDist)
cWSs(find(cWS==i))=log(wsSizeDist(i));
end
%% show the volume slices
figure(2)
subplot(2,2,1)
imagesc(cImg(:,:,100));
title('Z Slice 20 of subvolume')
subplot(2,2,2);
imagesc(cCLs(:,:,100));
title('Component Labeling Colored by LogSize')
subplot(2,2,3);
imagesc(cDist(:,:,100));
title('Distance Map')
subplot(2,2,4);
imagesc(cWSs(:,:,100));
title('Watershed')
%% run the shape analysis
[cl_table,col_names]=ellipsoid_analysis(cCL,'complabel.csv');
[ws_table,col_names]=ellipsoid_analysis(cWS,'watershed.csv');
%% compare the shape results
figure(1)
plot3(ws_table(:,getnameidx(col_names,'cov_x')),ws_table(:,getnameidx(col_names,'cov_y')),ws_table(:,getnameidx(col_names,'cov_z')),'r.')
hold on;
plot3(cl_table(:,getnameidx(col_names,'cov_x')),cl_table(:,getnameidx(col_names,'cov_y')),cl_table(:,getnameidx(col_names,'cov_z')),'b+')
hold off;
legend({'Component Label','Watershed'});
figure(2)
[cl_counts,cl_bins]=hist(cl_table(:,getnameidx(col_names,'volume')),20);
[ws_counts,ws_bins]=hist(ws_table(:,getnameidx(col_names,'volume')),cl_bins);
semilogx(cl_bins,cl_counts,'r-',ws_counts,ws_bins,'b-');
legend({'Component Label','Watershed'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment