COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### intersecting pixel of 2 lines

• Email
• Follow

```Good day everyone,

I have 2 lines on an image. These 2 lines intersect each other. I would like to find the pixel location at which they intersect. The solution I'm thinking about is to using improfile function , so that I can get the pixel lists for the 2 lines and find the intersecting values.

Is there any other more efficient way I can do this besides the search method I'm thinking about doing ?

Kurt
```
 0

See related articles to this posting

```"audley james" <audleyer@gmail.com> wrote in message <ik0vhj\$3in\$1@fred.mathworks.com>...
> Good day everyone,
>
> I have 2 lines on an image. These 2 lines intersect each other. I would like to find the pixel location at which they intersect. The solution I'm thinking about is to using improfile function , so that I can get the pixel lists for the 2 lines and find the intersecting values.
>
> Is there any other more efficient way I can do this besides the search method I'm thinking about doing ?
>
>
> Kurt

put the two lines in the format y-mx=b and solve the system

example 1*y-(-1)*x=1 and 1*y+0*x=2

b=[1 2]'
a=[1 -1; 1 0]
a\b

The result is the point where they intersect, x and y
```
 0
Reply paulojmdsilva (31) 2/22/2011 6:48:04 PM

```"audley james" <audleyer@gmail.com> wrote in message <ik0vhj\$3in\$1@fred.mathworks.com>...
> Good day everyone,
>
> I have 2 lines on an image. These 2 lines intersect each other. I would like to find the pixel location at which they intersect. The solution I'm thinking about is to using improfile function , so that I can get the pixel lists for the 2 lines and find the intersecting values.
>
> Is there any other more efficient way I can do this besides the search method I'm thinking about doing ?
>

doc bwmorph
```
 0
Reply sean.dewolski1 (106) 2/22/2011 6:49:05 PM

```ans=
2
1

The first value is the value of the first variable, in the example I used y first so the y is 2 and the x is 1.
```
 0

```Paulo,

thanks.

Please pardon my ignorance. Can you please let me exactly how is this done?

I have 2 intersecting image lines in the endpoint form [x1 y1 x2 y2]:

line1 --->  [93  388 120  354]
line2 ---> [ 102  355  124 377]

Can you please show me an example using these coordinates ? Sorry for this

--kurt james
```
 0

```"Paulo Silva" wrote in message <ik10d4\$mkl\$1@fred.mathworks.com>...
> "audley james" <audleyer@gmail.com> wrote in message <ik0vhj\$3in\$1@fred.mathworks.com>...
> > Good day everyone,
> >
> > I have 2 lines on an image. These 2 lines intersect each other. I would like to find the pixel location at which they intersect. The solution I'm thinking about is to using improfile function , so that I can get the pixel lists for the 2 lines and find the intersecting values.
> >
> > Is there any other more efficient way I can do this besides the search method I'm thinking about doing ?
> >
> >
> > Kurt
>
> put the two lines in the format y-mx=b and solve the system
>
> example 1*y-(-1)*x=1 and 1*y+0*x=2
>
> b=[1 2]'
> a=[1 -1; 1 0]
> a\b
>
> The result is the point where they intersect, x and y

This is much more work and makes the blind assumption that the lines are indeed first order.  Don't do it!
```
 0

```Sean,

can you suggest , the possible operation in bwmorph, I should use ?

-james
```
 0

```On Feb 22, 2:06=A0pm, "audley james" <audle...@gmail.com> wrote:
> Sean,
>
> can you suggest , the possible operation in bwmorph, I should use ?
>
> -james
-----------------------------------------------------------------------
james:
There's a branchpoints option to bwmorph.  But this depends on your
image being a binary image.  We don't know what you have (a binary
image, gray scale image, or color image), nor do we know what you have
defined as lines.  Are the lines something you plotted in the
overlay?  Are they graphics in the image (connected pixels of 255) or
are they just some gray level edge, like you have an image of a car
and want to know where the vertical door seam meets the horizontal
(uploadhouse.com) so we can stop guessing.
ImageAnalyst
```
 0

```For the data provided here's the script and solution

clf
hold on
l1=[93 388 120 354]
l2=[102 355 124 377]
line([l1(1) l1(3)],[l1(2) l1(4)])
line([l2(1) l2(3)],[l2(2) l2(4)])
ml1=(l1(4)-l1(2))/(l1(3)-l1(1))
ml2=(l2(4)-l2(2))/(l2(3)-l2(1))
bl1=l1(2)-ml1*l1(1)
bl2=l2(2)-ml2*l2(1)
b=[bl1 bl2]'
a=[1 -ml1; 1 -ml2]
Pint=a\b
plot(Pint(2),Pint(1),'ro')

%The point of intersection has the coordinates:
%x=364.5902
%y=111.5902
```
 0

```"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ik11d4\$rii\$1@fred.mathworks.com>...
> "Paulo Silva" wrote in message <ik10d4\$mkl\$1@fred.mathworks.com>...
> > "audley james" <audleyer@gmail.com> wrote in message <ik0vhj\$3in\$1@fred.mathworks.com>...
> > > Good day everyone,
> > >
> > > I have 2 lines on an image. These 2 lines intersect each other. I would like to find the pixel location at which they intersect. The solution I'm thinking about is to using improfile function , so that I can get the pixel lists for the 2 lines and find the intersecting values.
> > >
> > > Is there any other more efficient way I can do this besides the search method I'm thinking about doing ?
> > >
> > >
> > > Kurt
> >
> > put the two lines in the format y-mx=b and solve the system
> >
> > example 1*y-(-1)*x=1 and 1*y+0*x=2
> >
> > b=[1 2]'
> > a=[1 -1; 1 0]
> > a\b
> >
> > The result is the point where they intersect, x and y
>
> This is much more work and makes the blind assumption that the lines are indeed first order.  Don't do it!

They are, see the data provided in a reply above.
```
 0

```ImageAnalyst,

here is my image :

the 2 intersecting lines in endpoint form [x1 y1 x2 y2] are:

[216  135  286  132]
[257 118  256  166]

thanks
```
 0

```On Feb 22, 3:19=A0pm, "audley james" <audle...@gmail.com> wrote:
> ImageAnalyst,
>
> here is my image :http://www.uploadhouse.com/viewfile.php?id=3D8151866&PH=
PSESSID=3D7d81cf17...
>
> the 2 intersecting lines in endpoint form [x1 y1 x2 y2] are:
>
> [216 =A0135 =A0286 =A0132]
> [257 118 =A0256 =A0166]
>
> thanks

---------------------------------------------------------------------------=
------
Your color image has dozens of "lines" but it sounds like you somehow
found them and you have the endpoints for each line segment.  I don't
know *how* you found them, but you did.  So in that case, just do what
someone else suggested.  Use regular algebra to find the
intersections  Get the equations for each line
y1 =3D slope1 * x + offset1
y2 =3D slope2 * x + offset2
Then set y1 =3D to y2 and solve for x
slope1 * x + offset1 =3D slope2 * x + offset2
(slope1-slope2) x =3D (offset2 - offset1)
so x =3D (offset2 - offset1) / (slope1-slope2)
and the y value at the intersection =3D slope1 * x + offset1
```
 0

```I made a function with the code I wrote above (plus a few more lines) that finds the intersection points and submitted it into the File Exchange, will be available soon. I hope someone might find it useful.
```
 0

```Paulo,

Can you please send me an email for the function

thanks alot.
james
```
 0

```Here is a ready-in-the-can tool
http://www.mathworks.com/matlabcentral/fileexchange/27673-2d-polygon-edges-intersection

P1 = [93 120; 388 354]
P2 = [102 124; 355 377]
X = polyxpoly(P1,P2) % FEX

figure
plot(P1(1,:),P1(2,:))
hold on
plot(P2(1,:),P2(2,:))
plot(X(1,:),X(2,:),'or')

% Bruno
```
 0

```Bruno,

if 2 lines do not reach each other, that is, I mean touch each other , then no intersection point is computed?
```
 0

```"audley james" <audleyer@gmail.com> wrote in message <ik2eua\$gh4\$1@fred.mathworks.com>...
> Bruno,
>
> if 2 lines do not reach each other, that is, I mean touch each other , then no intersection point is computed?

Correct.

Bruno
```
 0

```This is good work Bruno. Its what i actually was trying to do. Can you suggest to me to the location of the Matlab script line number where you enforce this constraint of when lines don't meet so that no intersection is computed.

all the best,

james
```
 0
Reply audleyer (8) 2/23/2011 8:17:18 AM

```"audley james" <audleyer@gmail.com> wrote in message <ik2fqe\$jvu\$1@fred.mathworks.com>...
> This is good work Bruno. Its what i actually was trying to do. Can you suggest to me to the location of the Matlab script line number where you enforce this constraint of when lines don't meet so that no intersection is computed.

line #76 and line #87. Right after those lines, insert the commands
ind(:) = true;
to ignore intersection check.

Bruno
```
 0

```On Feb 22, 7:33=A0pm, "audley james" <audle...@gmail.com> wrote:
> Good day everyone,
>
> I have 2 lines on an image. These 2 lines intersect each other. I would l=
ike to find the pixel location at which they intersect. The solution I'm th=
inking about is to using improfile function , so that I can get the pixel l=
ists for the 2 lines and find the intersecting values.
>
> Is there any other more efficient way I can do this besides the search me=
thod I'm thinking about doing ?

The question is ill posed.

Consider the chess board, and the diagonal A1 - H8:

Q1) In what square (pixel) does the diagonal B8 - H2
intersect with A1 - H8?

A1) In E5, which belongs to both diagonals.

------

Q2) In which square (pixel) does the diagonal A8 - H1
intersect with A1 - H8?

A2) It doesn't. There are no squares (pixels) on the
chess board that belongs to both diagonals.

Rune
```
 0

```"audley james" <audleyer@gmail.com> wrote in message <ik2bok\$so0\$1@fred.mathworks.com>...
> Paulo,
>
> Can you please send me an email for the function
>
> thanks alot.
> james

http://www.mathworks.com/matlabcentral/fileexchange/30502
```
 0