clc; clear; close all; img = imread('stplot.tif'); doflip = true; step = 32; wndsz = 64; fps = 800; if doflip img = fliplr(img); imwrite(img, '_img.tif'); else imwrite(img, '_img.tif'); end clear doflip; a = 1 : step : size(img,1); b = wndsz : step : size(img,1); a = a(1:length(b)); TrImg = uint8(zeros(size(img))); invalid = []; for i = 1 : 1 : length(a) % enhance trace st = img(a(i):b(i),:); m = mean(st,2); m = [find(m==0); find(m==255)]; if ~isempty(m) invalid = [invalid, a(i):b(i)]; continue; end [ste, ang] = TraceEnhance(st); % find hough peaks th = graythresh(ste); bw = imbinarize(ste, th); [H,T,R] = hough(bw, 'Theta',[-89:-1]); P = houghpeaks(H, wndsz); % remove false hough peaks (wrong angle) th = 3; y = P(:,1); x = P(:,2); k = abs(ang+T(x)); k = find(k>th); P(k,:) = []; y = P(:,1); x = P(:,2); % remove false hough peaks (out of 2x SD ) rx = [mean(x)-2*std(x), mean(x)+2*std(x)]; k = [find(xrx(2))]; k = sort(unique(k)); P(k,:) = []; % fit hough peaks, linear fitting [xData, yData] = prepareCurveData( P(:,1), P(:,2) ); [fitresult, gof] = fit( xData, yData, fittype( 'poly1' ) ); y = 1 : size(H,1); x = round(fitresult.p1.*y + fitresult.p2); z = zeros(1,length(y)); for j = 1 : length(z) if(x(j)>89) continue; end z(j) = H(y(j),x(j)); end % find hough peaks on the fitted line th = 3; z(z z(s(2)) n(j) = s(2); elseif z(s(1)) < z(s(2)) n(j) = s(1); else n(j) = s(2); end n(j) = find(xpeak==n(j)); end end xpeak(n) = []; % find hough lines y = y(xpeak); x = x(xpeak); P = [y',x']; lines = houghlines(bw,T,R,P, 'FillGap',wndsz, 'MinLength',5); p1 = zeros(length(lines), 2); p2 = zeros(length(lines), 2); for j = 1 : length(lines) p1(j,:) = lines(j).point1; p2(j,:) = lines(j).point2; end % extend hough lines to image size p = cell(1,2); p{1} = zeros(size(p1)); p{2} = zeros(size(p2)); k = zeros(1, size(p1,1)); d = zeros(1, size(p1,1)); for j = 1 : size(p1,1) y = [p1(j,2), p2(j,2)]; x = [p1(j,1), p2(j,1)]; [xData, yData] = prepareCurveData( x, y ); [fitresult, gof] = fit( xData, yData, fittype( 'poly1' ) ); x = [1 size(ste,2)]; y = round(fitresult(x)); p{1}(j,1) = x(1); p{1}(j,2) = y(1); p{2}(j,1) = x(2); p{2}(j,2) = y(2); k(j) = fitresult.p1; d(j) = fitresult.p2; end xi = 1; yi = 2; % remove false hough lines [~,n] = sort(p{1}(:,yi)); p{1} = p{1}(n,:); p{2} = p{2}(n,:); k = k(n); d = d(n); n = []; m = mean(k); for j = 1 : size(p{1},1)-1 if ( ( p{2}(j,yi)>=p{2}(j+1,yi) ) && ( p{2}(j+1,yi)<=size(ste,1) ) ) if abs(k(j)-m) >= abs(k(j+1)-m) n = [n, j]; else n = [n, j+1]; end end end p{1}(n,:) = []; p{2}(n,:) = []; k(n) = []; d(n) = []; % generate trace image tr = uint8(zeros(size(ste))); for j = 1 : size(p{1},1) % x -> y x = 1 : size(ste,2); y = k(j) .* x + d(j); y = round(y); n = [find(y<1), find(y>size(ste,1))]; y(n) = []; x(n) = []; x1 = x; y1 = y; for t = 1 : length(x1) tr(y1(t),x1(t)) = 255; end % y -> x y = 1 : size(ste,1); x = (y - d(j)) ./ k(j); x = round(x); n = [find(x<1), find(x>size(ste,2))]; y(n) = []; x(n) = []; x2 = x; y2 = y; for t = 1 : length(x2) tr(y2(t),x2(t)) = 255; end end TrImg(a(i)+wndsz/4 : b(i)-wndsz/4, :) = tr(wndsz/4+1 : wndsz-wndsz/4, :); end imwrite(TrImg, '_tr.tif'); valid = [a(1)+wndsz/4, b(i)-wndsz/4]; invalid = unique(invalid); save('info.mat', 'step', 'wndsz', 'fps', 'valid', 'invalid') clear all; tr = imread('_tr.tif'); load('info.mat'); timeseq = 0 : 1/fps : (size(tr,1)-1)/fps; cellnum = nan(1, size(tr,1)); for i = valid(1) : valid(2) if isempty(find(invalid==i)) p = [0, tr(i,:), 0]; p = single(p); [~, n] = findpeaks(p); cellnum(i) = length(n); end end cellnum(invalid) = nan; cnt = cellnum(valid(1):valid(2)); c = cnt(~isnan(cnt)); m = mean(c); s = std(c); r = round([m-2*s, m+2*s]); n2 = [find(cntr(2))]; n2 = sort(n2); n2 = unique(n2); r = round([m-s, m+s]); n1 = [find(cntr(2))]; n1 = sort(n1); n = find(ismember(n1,n2)); n1(n) = []; n = []; if n1(1)+1 ~= n1(2) n = [n, n1(1)]; end for i = 2 : length(n1)-1 if ( (n1(i)-1 ~= n1(i-1)) && (n1(i)+1 ~= n1(i+1)) ) n = [n, n1(i)]; end end if n1(end)-1 ~= n1(end-1) n = [n, n1(end)]; end n = [n, n2]; n = unique(n); n = sort(n); if n(end) == length(cnt) cnt(end) = cnt(end-1); n(end) = []; end if n(1) == 1 cnt(1) = cnt(2); n(1) = []; end x = 0 : length(cnt)-1; y = cnt; x([n, find(isnan(cnt))]) = []; y([n, find(isnan(cnt))]) = []; [xData, yData] = prepareCurveData( x, y ); ft = 'linearinterp'; [fitresult, gof] = fit( xData, yData, ft ); x = 0 : length(cnt)-1; y = fitresult(x); y = round(y'); cnt(n) = y(n); cellnum(valid(1):valid(2)) = cnt; cellnum(invalid) = nan; cnt([n, find(isnan(cnt))]) = []; m = mean(cnt); s = std(cnt); CellNum = [timeseq; cellnum]; save CellNum CellNum; n = cellnum; n(isnan(n)) = []; m = []; for i = min(n) : max(n) m = [m, length(find(n==i))]; end m = m./(sum(m)); bar([min(n):max(n)], m); xlabel('Lineal density'); ylabel('Frequency'); function [StEn, Ang] = TraceEnhance(st) g = single(st); gt = mean(g,2); gd = mean(g,1); gm = mean(g(:)); gi = gt*gd/gm; r = g./gi; r = r - mean(r(:)); Theta = [1 : 0.1 : 89]; [phi,~] = radon(r, Theta); sn = rms(phi,1) / rms(phi(:)); [~,ta] = max(sn); Ang = Theta(ta); m = mean(st); st = double(st); for i = 1 : size(st,1) st(i,:) = st(i,:)./m; end wavelength = 7; orientation = [Ang]; sfb = 1.75; sar = 0.25; g = gabor(wavelength,orientation, 'SpatialFrequencyBandwidth',sfb, 'SpatialAspectRatio',sar); m = imgaborfilt(st,g); r = m.*m; % g = single(r); % gt = mean(g,2); % gd = mean(g,1); % gm = mean(g(:)); % gi = gt*gd/gm; % r = g./gi; % r = r - mean(r(:)); r = (255 * (r - min(r(:))) / (max(r(:)) - min(r(:)))); se = strel('disk', 1, 4); r = imerode(r, se); sigmas = [0.25 : 0.25 : 1]; spacing = [2;2]; tau = 1; brightondark = true; r = vesselness2D(r, sigmas, spacing, tau, brightondark); StEn = (r - min(r(:))) / (max(r(:)) - min(r(:))); clear st g gt gd gm gi r Theta phi sn ta m wavelength orientation se sfb sar sigmas spacing tau brightondark i; end