Search code examples
matlabimage-processingmatrixtraversaledge-detection

MATLAB: Return array of values between two co-ordinates in a large matrix (diagonally)


If I explain why, this might make more sense

I have a logical matrix (103x3488) output of a photo of a measuring staff having been run through edge detect (1=edge, 0=noedge). Aim- to calculate the distance in pixels between the graduations on the staff. Problem, staff sags in the middle.

Idea: User inputs co-ordinates (using ginput or something) of each end of staff and the midpoint of the sag, then if the edges between these points can be extracted into arrays I can easily find the locations of the edges.

Any way of extracting an array from a matrix in this manner?

Also open to other ideas, only been using matlab for a month, so most functions are unknown to me.

edit: Link to image

It shows a small area of the matrix, so in this example 1 and 2 are the points I want to sample between, and I'd want to return the points that occur along the red line.

Cheers


Solution

  • Try this

    dat=imread('83zlP.png');
    
    figure(1)
    pcolor(double(dat))
    shading flat
    axis equal
    
    % get the line ends
    gi=floor(ginput(2))
    x=gi(:,1);
    y=gi(:,2);
    
    xl=min(x):max(x); % line pixel x coords
    yl=floor(interp1(x,y,xl)); % line pixel y coords
    
    
    pdat=nan(length(xl),1);
    for i=1:length(xl)
        pdat(i)=dat(yl(i),xl(i));
    end
    
    figure(2)
    plot(1:length(xl),pdat)
    
    peaks=find(pdat>40); % threshhold for peak detection
    bigpeak=peaks(diff(peaks)>10); % threshold for selecting only edge of peak
    
    hold all
    plot(xl(bigpeak),pdat(bigpeak),'x')
    meanspacex=mean(diff(xl(bigpeak)));
    meanspacey=mean(diff(yl(bigpeak)));
    meanspace=sqrt(meanspacex^2+meanspacey^2);
    

    The matrix pdat gives the pixels along the line you have selected. The meanspace is edge spacing in pixel units. The thresholds might need fiddling with, depending on the image.