function distance(filepath, ext) % if ~(isempty(filepath)) % centers = imread(filepath, ext); % % else % centers = imread ('/home/wrightc/program/images/center1', 'tiff'); % centers = imread ('/home/wrightc/program/images/image6center', 'tiff'); % centers = imread('/home/wrightc/matlab/dimages/error2010_2', 'tiff'); centers = imread('/home/wrightc/program/images/im4tf','tiff'); % end [imax, jmax] = size(centers); figure(1) imagesc(centers); colormap gray % assumes only one points per center. N = floor(bwarea(centers)); % number of sections in histogram. ratio = 1.5; x = sqrt( N / ratio ); % 1.5 is assumption about rows vs columns NUM = ceil( 4 * x ); NUM2 = ceil( 4 * ratio * x ); % finding y distance ------------------------------------------------------ XY = zeros (ceil (N*N / 2), 1); [y,x] = find(centers); n = 1; for i = 1:N yi = y(i); for j = 1: (i-1) XY(n) = abs( yi - y(j) ); n = n+1; end end yhist = hist( XY, NUM ); % find max points of histogram high = zeros(5,1); j = 1; for i= 2:NUM if ( yhist(i-1) < yhist(i) & yhist(i) > yhist(i+1) ) high(j) = i; j = j + 1; if j == 6 break end end end % compute average distance yave = 0; for i = 1:4 yave = yave + high(i+1) - high(i); end yave = yave/4; yave = yave * imax / NUM % finding x distance ------------------------------------------------------ % if a point is within a certain y range, computer its distance to the % other points. % reuse = X vector range = yave / 3; n = 1; for i = 1:N xi = x(i); yi = y(i); for j = 1:i-1 if ( abs(yi - y(j)) < range ) XY(n) = abs( xi - x(j) ); n = n+1; end end end xhist = hist( XY(1:n) , NUM2 ); % find max points of histogram high = zeros(5,1); j = 1; for i= 2:NUM2 if ( xhist(i-1) < xhist(i) & xhist(i) > xhist(i+1) ) high(j) = i; j = j + 1; if j == 6 break end end end % compute average distance xave = 0; for i = 1:4 xave = xave + high(i+1) - high(i); end xave = xave/4; xave = xave * jmax / NUM2 % Find connections -------------------------------------------------------- % method: look in range of anticipated connection figure(1) hold on plot(x,y,'g.') yshift = floor(yave); xshift = floor(xave); yrange = floor(2*yave/5); xrange = floor(2*xave/5); conn = zeros(N, 6); for n = 1:N % find points horizontal to the right --------------------------------- lookhere(1) = x(n) + xshift; lookhere(2) = y(n); t = find( x < lookhere(1) + xrange & x > lookhere(1) - xrange ); t2 = find( y(t(:)) < lookhere(2) + yrange & y(t(:)) > lookhere(2) - yrange, 1, 'last'); if ( ~isempty(t2) ) %horizontal and to the right connections hr = t(t2); conn(n, 1) = hr; conn(hr, 4) = n; plot( [x(n),x(hr)],[y(n), y(hr)],'b-' ) end t = []; t2 = []; % find points to the lower right -------------------------------------- lookhere(1) = x(n) + xshift/2; lookhere(2) = y(n) + yshift; t = find( x < lookhere(1) + xrange & x > lookhere(1) - xrange ); t2 = find( y(t(:)) < lookhere(2) + yrange & y(t(:)) > lookhere(2) - yrange, 1, 'last'); if ( ~isempty(t2) ) % down and to the right connections lr = t(t2); conn(n, 2) = lr; conn(lr, 5) = n; plot( [x(n),x(lr)],[y(n), y(lr)],'c-' ) end t = []; t2 = []; % find points to the lower left --------------------------------------- lookhere(1) = x(n) - xshift/2; lookhere(2) = y(n) + yshift; t = find( x < lookhere(1) + xrange & x > lookhere(1) - xrange ); t2 = find( y(t(:)) < lookhere(2) + yrange & y(t(:)) > lookhere(2) - yrange, 1, 'last'); if ( ~isempty(t2) ) %horizontal and to the right connections ll = t(t2); conn(n, 3) = ll; conn(ll, 6) = n; plot( [x(n),x(ll)],[y(n), y(ll)],'c-' ) end t = []; t2 = []; % stores connections % stores index of points and its 6 connections in a row % 1 2 6 should be larger than the row number % 3 4 5 should be smaller than the row number. % all connections stored twice, will reduce speed of processing when % walking through image. end % add / remove connections ------------------------------------------------ title 'LEFT click = add connection, RIGHT click = remove connection, CENTER = exit' disp( 'click on two tiles, then enter the direction of connection') disp( '[1] right' ) disp( '[2] lower right' ) disp( '[3] lower left' ) disp( '[4] left' ) disp( '[5] upper left' ) disp( '[6] upper right' ) figure(1) hold on button = 1; while button ~= 2 % first point [x1,y1,button] = ginput(1); if ( button == 2 ) break; end [y1,x1] = findclosest(centers, floor(y1), floor(x1)); temp = find( x == x1 ); temp2 = find ( y(temp) == y1); n1 = temp(temp2) plot(x1,y1,'go') type = input( 'choose connection type [1-6]: '); % add connection if button == 1 % second point [x2,y2] = ginput(1); x2 = round(x2); y2 = round(y2); [y2, x2] = findclosest(centers, y2, x2); temp = find( x == x2 ); temp2 = find ( y(temp) == y2); n2 = temp(temp2) plot(x2,y2,'go') if type > 3 temp = n1; n1 = n2; n2 = temp; type = type - 3; end conn(n1, type) = n2; conn(n2, type+3) = n1; plot( [x(n1),x(n2)],[y(n1), y(n2)],'g-' ) % remove connection elseif button == 3 n2 = conn(n1,type); if n2 == 0 disp( 'there was no connection to break') else conn(n1,type) = 0; if type < 4 conn(n2, type+3) = 0; else conn(n2, type-3) = 0; end plot( [x(n1),x(n2)],[y(n1), y(n2)],'r-' ) end end end hold off figure(2) imagesc (centers) plotconn( 2,x,y,conn, N ) pause; close all clear pack %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function plotconn( fig, x,y,conn, N ) figure(fig) hold on plot(x,y,'g.') for n = 1:N for i = 1:3 if conn(n,i) ~= 0 plot( [x(n), x(conn(n,i))], [y(n), y(conn(n,i))], 'c-'); end end end hold off %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [i2,j2] = findclosest( input_image, i , j ) if (input_image(i,j)) i2=i; j2=j; return; end %create image with only selected point in it point = zeros( size(input_image) ); point(i,j) = 1; % find distance from that point for all points point = bwdist( point ); % multiply by orig bw image so that only points already in image are counted point = point .* double(input_image); % find the point with the minimum distance from selected point indicies = find(point); [c,I] = min( point(indicies) ); I= indicies(I); [i2,j2] = ind2sub(size(point), I); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%