Skip to content

Instantly share code, notes, and snippets.

@oxyflour
Created September 30, 2015 02:39
Show Gist options
  • Save oxyflour/c577888bce9cbccd9f9b to your computer and use it in GitHub Desktop.
Save oxyflour/c577888bce9cbccd9f9b to your computer and use it in GitHub Desktop.
count particle statistics from image
% image file
FILE_NAME = 'p2.png';
% length / pixel
LENGTH_SCALE = 500 / 114;
% ignore regions if area small than
MIN_AREA = 10;
% filter to create binary image
FILTER_VALUE = 128;
im = imread(FILE_NAME);
if (size(im, 3) == 1)
bm = im;
else
bm = rgb2gray(im);
end
figure(1)
fi = imshow(bm);
ui = imcontrast;
msgbox(['[i] adjust the contrast to display particles clearly, ' ...
'then press "Adjust Data" and close the contrast window.'])
uiwait(ui);
mi = findobj(fi, 'Type', 'image');
bm = get(mi, 'Cdata');
bm = bm > 128;
ss = regionprops(bm, 'Basic');
px = arrayfun(@(s) s.Centroid(1), ss);
py = arrayfun(@(s) s.Centroid(2), ss);
pa = arrayfun(@(s) s.Area, ss);
ii = find(pa > MIN_AREA);
px = px(ii);
py = py(ii);
figure(2);
plot(px, py, '*');
tri = delaunay(px, py);
figure(3);
triplot(tri, px, py);
edges = [];
for ii = 1:length(tri)
es = tri(ii, :);
es2 = [es es(1)];
for jj = 1:length(es);
vi = es(jj);
vj = es2(jj + 1);
xi = px(vi); yi = py(vi);
xj = px(vj); yj = py(vj);
edges(end + 1) = sqrt((xi - xj)^2 + (yi - yj)^2);
end
end
figure(4);
x = [0:10:200];
n = histc(edges, x / LENGTH_SCALE);
bar(x, n / 2);
xlim([min(x) max(x)]);
xlabel('distance (nm)');
ylabel('count')
set(gcf, 'color', 'w');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment