function border_rows = border_finder(image, threshold) %function to find the top and bottom edge of a binary image of a chromosome % Todd Fallesen, Feb 10th, 2022, Calm Facility, The Francis Crick Institute ImBW = image; im_length = size(ImBW,1); %threshold=0.7; widths = []; for k=1:im_length widths(k,1) = nnz(ImBW(k,:)); widths(k,2) = k; end width_mean = mean(widths,1); %zero rows that have non_zero elements on the borders for k=1:im_length if sum(ImBW(k,1:3)) > 0 widths(k,1)=0; end if sum(ImBW(k,end-2:end)) > 0 widths(k,1) = 0; end %zero the rows that are below the threshold %Could vectorize this, to make it faster. for j=1:im_length if widths(j,1) < width_mean(1)*threshold widths(j,1) = 0; end end %find first non-zero row first_row = widths(min(find(widths(:,1))),2); %this is a convoluted way to do it, but instead of using the row index as the row number, I've placed row numbers in the second column, so that if we decide to pull rows earlier for % other analysis reasons, % we have a record of rows % still last_row = widths(max(find(widths(:,1))),2); border_rows = [first_row, last_row]; end