Getting all pixel coordinates of a vector inside a image

前端 未结 2 1257
谎友^
谎友^ 2021-01-14 03:57

I have an intensity/greyscale image, and I have chosen a pixel inside this image. I want to send vectors starting from this pixel in all directions/angles, and I want to sum

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 04:53

    This example will be similar to Amro's, but it is a slightly different implementation that should work for an arbitrary coordinate system assigned to the image...

    Let's assume that you have matrices of regularly-spaced x and y coordinates that are the same size as your image, such that the coordinates of pixel (i,j) are given by (x(i,j),y(i,j)). As an example, I'll create a sample 5-by-5 set 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 define a line y = m*(x - a) + b passing through the coordinate system by selecting some values for the constants and computing 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 the y points in the grid that differ from the points computed above by 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 crossed by the line:

    >> xCrossed = xGrid(index);
    >> yCrossed = yGrid(index);
    

提交回复
热议问题