Search code examples
pythonmatlabnumpyimage-processinghough-transform

Convert X,Y floating point coordinates to binary matrix and then perform a Hough line transform


Is it possible to compute a hough line transform of array of xy floating points, similar to this matlab code in python?

BW=full(sparse(x,y,true));

the data looks like

enter image description here


Solution

  • Your example in MATLAB only works on integer (x,y) coordinates.

    For example

    % I use a 10x10 identity matrix to simulate a line of points
    % And scale the resulting x, y coordinates to be floating point 
    [X, Y] = find(eye(10));
    X = X * 0.1;
    Y = Y * 0.1;
    A = full(sparse(X, Y, true));
    

    Throws the error

    Error using sparse. Index into matrix must be an integer.

    If you want to convert floating point coordinates into a binary matrix, the only way I know of is to decimate your space.

    % Precision of the decimated grid
    scale = .01;
    
    % Scale the X, Y values to be integers greater than 1
    row_indices = round((Y - min(Y))/scale) + 1;    
    col_indices = round((X - min(X))/scale) + 1;
    
    % row values also need to be flipped 
    % i.e. y = 0 should be the maximum row in the matrix to maintain the same orientation of the coordinate system
    row_indices = max(row_indices) -  row_indices + 1;
    
    % Create matrix using your method
    A = full(sparse(row_indices, col_indices, true));
    
    % Each row and column in A corresponds to the value in these range vectors
    xrange = min(X):scale:max(X);
    yrange = max(Y):-scale:min(Y);
    

    To test whether these transformations produced the desired result. I plotted the matrix.

    figure; 
    subplot(1,2,1); imagesc(A);
    xticks(1:20:100); xticklabels(xrange(1:20:end));
    yticks(1:20:100); yticklabels(yrange(1:20:end));
    subplot(1,2,2); plot(X, Y, 'ko');
    

    And it's looking good.

    Binary matrix on the left, plotted points on the right

    A similar approach should be easy to implement using numpy.