Pixels between two intersecting lines

I need to find the pixel values ​​that are between the intersection of two lines. The following figure shows the points I want, namely the brown area.

These 4 coordinates are subject to change and are not necessarily angular.

enter image description here

What is the fastest way to get pixel values? Is there any function that can give me the necessary mask.

+4
source share
3 answers

You must calculate for each point, whether above the line or below. If a line is given in its form of the equation Ax+By+C , then it is as simple as calculating the sign of this expression for your point (x,y) . If your lines are in any other form, you must first calculate the form above. (Here here and here )

Let L1 be the set of all points below the first line, and L2 be the set of all points below the second line. Then your set X = Xor(L1,L2)

[ enter image description here ] Xor [ enter image description here ]

Equally:

enter image description here

Here is the Matlab code that solves the problem for corner points based on the solution you described. You can customize linear equations in your code.

 function CreateMask() rows = 100; cols = 200; [X,Y] = ndgrid(1:cols,1:rows); belowFirstLine = X*(1/cols) + Y*(-1/rows) + 0 < 0; belowSecondLine = X*(-1/cols) + Y*(-1/rows) + 1 < 0; figure;imshow( transpose(xor(belowSecondLine,belowFirstLine))); end 
+6
source

Here is a geometric, not an analytical solution.

First you need to create a mask image, initially filled with all zeros. Then you should draw both lines using the Breshenem algorithm . Matlab does not have a default implementation, but you can select it in Matlab Central . I assume that you have the coordinates of the intersections of the lines with the borders of the images.

After that, your image will be divided into four areas, and you need to fill in two of them using bwfill . And now you have a mask.

+1
source

You can start by creating two matrices with x and y coordinates:

  1 2 3 4 5 1 1 1 1 1 1 2 3 4 5 vs. 2 2 2 2 2 sized as the region 1 2 3 4 5 3 3 3 3 3 

Then you need 4 linear equations that convert x * a + y * b <c into 4 masks: the diagonals should be XORED and the upper / lower masks ANDED
or without logical expressions: mask = mod (diag1 + diag2,2) * top_mask * bot_mask;

The line width can be controlled by adding half the line width to 'c', assuming that a and b are normalized.

0
source

Source: https://habr.com/ru/post/1444255/


All Articles