%These scripts use both fixed threshold and adaptive threshold to index the axon images. %The fixed threshold is pre-determined by manually testing against several sample axon images. %The adaptive threshold was based on the adaptivethresh.m %(http://www.csse.uwa.edu.au/~pk/research/matlabfns/Spatial/adaptivethresh.m). %The parameters were chosen by testing against sample axon images. %Although the results obtained from the adpative and fixed threshold method are not big. %the adaptive threshold method works better and correctly indexes axons with both strong and weak intensities. %The scripts automatically open all tif formatted images in a chosen folder, %output thresholded images and save calculated results as Excel spreadsheets. %Gang Peng 2011 %Free to use and to modify. load_folder = pwd; dirListing = dir(strcat(load_folder,'\*.tif')); manual_threshold = 30; % enter the threshold number here threshold_level = manual_threshold/255; axon_sheet = (0:255)'; % initiate axon_sheet to hold raw histogram data axon_label = {'lumi'}; % initiate header line to store specimen name specimen_label = {}; % initiate specimen name label specimen_result = []; % initiate sheet to hold pixel sum data auto_specimen_result = []; % initiate sheet to hold auto threshold result for d = 1:length(dirListing) raw_image = imread(dirListing(d).name); % read in image image_name = dirListing(d).name; image_name = image_name(1:end-4); axon_label = [axon_label, image_name, 'pixel_sum', 'empty']; specimen_label = [specimen_label; image_name]; axon_image = raw_image(:,:,1); % take the layer contain data axon_hist = imhist(axon_image); % get histogram of the data point_value = (0:255)'.*axon_hist; sum_value = sum(point_value); pixel_sum = zeros(256,1); for i = 1:256 pixel_sum(i)= sum_value - point_value(i); sum_value = pixel_sum(i); end axon_sheet = [axon_sheet, axon_hist, pixel_sum, zeros(256,1)]; specimen_result = [specimen_result; pixel_sum(manual_threshold)]; %fixed threshold axon_thresholded = im2bw(axon_image, threshold_level); %automatic adaptive threshold method auto_bw_axon = adaptivethresh(axon_image, 10, -20, 'gaussian', 'fixed'); axon_auto_thresholded = sum(axon_image(auto_bw_axon)); auto_specimen_result = [auto_specimen_result; axon_auto_thresholded]; % write thresholded images axon_bw_name = strcat('bw_', dirListing(d).name); axon_auto_bw_name = strcat('autobw_', dirListing(d).name); imwrite(axon_thresholded, axon_bw_name, 'tif'); imwrite(auto_bw_axon, axon_auto_bw_name, 'tif'); end %% prepare and write excel output % final result split_m = ones(1, length(dirListing)); result_cell = mat2cell(specimen_result, split_m, 1); specimen_cell = [specimen_label, result_cell]; auto_result_cell = mat2cell(auto_specimen_result, split_m, 1); auto_specimen_cell = [specimen_label, auto_result_cell]; % raw calculation results [m,n] = size(axon_sheet); m_divide = ones(1,m); n_divide = ones(1,n); sheet_cell = mat2cell(axon_sheet, m_divide, n_divide); axon_cell = [axon_label; sheet_cell]; xlswrite('threshold_results.xls', specimen_cell); xlswrite('auto_threshold_results.xls', auto_specimen_cell); xlswrite('axon_raw_data.xls', axon_cell);