Search code examples
matlabimage-processinggeometrycoordinate-systems

In Matlab, how to scan a binary image in 2D co-ordinate system that is 45 degrees rotated to actual co-ordinate system?


I have a binary image as in figure below,

enter image description here

the x-y co-ordinate system is the default co-ordinate system that comes with MATLAB. I am able to get the sum of pixels across each row and column in x-y co-ordinate system.

But I want the sum of pixels by stepping through u-v co-ordinate system. How can i get it?

My idea is to

1) Convert the x-y co-ordinate system to a continuous (real valued) co-ordinate system 2) Find the points in x-y coordinates corresponding to each point in u-v co-ordinate system. like (1,1) in u-v corresponds to (1.26,1.45) in x-y. 3) Get sum of rows and columns in u-v co-ordinate.

In this regard, 1) what are the methods to create a spatial co-ordinate system and convert pixel coordinate system to spatial coordinate system? 2) how to get the values of fractional pixels in the spatial co-ordinate system?

Thanks.


Solution

  • If you really only want the diagonals at exactly 45 degrees, and your pixels are square (safe assumption with most standard cameras), then you don't really need to do any coordinate transformation I don't think. You can use the fact that all of the points along the diagonals have the form e.g. I(ix, ix), I(1 + ix, ix). Working out the limits is a bit tricky. Try this for the "column" (diagonal from the top left to the bottom right) sums, starting at the bottom left, moving up the left edge, then across the top:

    I = eye(5, 4);
    I(4, 1) = 1;
    
    [nrows, ncols] = size(I);
    colsums = zeros(nrows + ncols - 1, 1);
    
    % first loop over each row in the original image except the first one
    for ix = nrows : -1 : 2,
        JX = [0 : min(nrows - ix, min(nrows-1, ncols-1))];
        for jx = JX,
            colsums(nrows - ix + 1) = colsums(nrows - ix + 1) + I(ix + jx, jx + 1);
        end
    end
    
    % then loop over each column in the original image 
    for ix = 1 : ncols,
        JX = [0 : min(nrows - ix - 1, min(nrows-1, ncols-1))];
        for jx = JX,
            colsums(nrows + ix - 1) = colsums(nrows + ix - 1) + I(1 + jx, ix + jx);
        end
    end
    

    Note that if the distance matters to you (kind of sounds like it doesn't), then the distances along these diagonals are sqrt(2)/2 longer.