This example will be similar to Amro , but this is a slightly different implementation that should work for an arbitrary coordinate system assigned to the image ...
Suppose you have matrices with regular x and y coordinates that match the size of your image, so that the pixel coordinates (i,j) are given by the symbol (x(i,j),y(i,j)) . As an example, I will create a 5-by-5 โโsample of sets of integer coordinates using MESHGRID :
>> [xGrid,yGrid] = meshgrid(1:5) xGrid = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 yGrid = 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
Next, we can determine the line y = m*(x - a) + b passing through the coordinate system, selecting some values โโfor the constants and calculating y using the x coordinates of the grid:
>> a = 0; >> b = 1; >> m = rand m = 0.5469 >> y = m.*(xGrid(1,:)-a)+b y = 1.5469 2.0938 2.6406 3.1875 3.7344
Finally, we find y points in the grid that differ from the points calculated above less than the grid size:
>> index = abs(yGrid-repmat(y,size(yGrid,1),1)) <= yGrid(2,1)-yGrid(1,1) index = 1 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0
and use this index matrix to get the x and y coordinates for the pixels intersected by the line:
>> xCrossed = xGrid(index); >> yCrossed = yGrid(index);
source share