Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
intersecting pixel of 2 lines

Subject: intersecting pixel of 2 lines

From: audley james

Date: 22 Feb, 2011 18:33:23

Message: 1 of 21

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 ?

Please let me know your suggestions.

Kurt

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 22 Feb, 2011 18:48:04

Message: 2 of 21

"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 ?
>
> Please let me know your suggestions.
>
> 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

Subject: intersecting pixel of 2 lines

From: Sean de

Date: 22 Feb, 2011 18:49:05

Message: 3 of 21

"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 ?
>
> Please let me know your suggestions.

doc bwmorph

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 22 Feb, 2011 18:55:15

Message: 4 of 21

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.

Subject: intersecting pixel of 2 lines

From: audley james

Date: 22 Feb, 2011 19:00:25

Message: 5 of 21

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

Subject: intersecting pixel of 2 lines

From: Sean de

Date: 22 Feb, 2011 19:05:08

Message: 6 of 21

"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 ?
> >
> > Please let me know your suggestions.
> >
> > 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!

Subject: intersecting pixel of 2 lines

From: audley james

Date: 22 Feb, 2011 19:06:05

Message: 7 of 21

Sean,

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

-james

Subject: intersecting pixel of 2 lines

From: ImageAnalyst

Date: 22 Feb, 2011 20:03:38

Message: 8 of 21

On Feb 22, 2:06 pm, "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
window bottom line. Please post your image somewhere
(uploadhouse.com) so we can stop guessing.
ImageAnalyst

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 22 Feb, 2011 20:10:20

Message: 9 of 21

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

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 22 Feb, 2011 20:12:03

Message: 10 of 21

"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 ?
> > >
> > > Please let me know your suggestions.
> > >
> > > 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.

Subject: intersecting pixel of 2 lines

From: audley james

Date: 22 Feb, 2011 20:19:04

Message: 11 of 21

ImageAnalyst,

here is my image :
http://www.uploadhouse.com/viewfile.php?id=8151866&PHPSESSID=7d81cf17d064bcb54b8c959a0a20b551


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

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

thanks

Subject: intersecting pixel of 2 lines

From: ImageAnalyst

Date: 22 Feb, 2011 20:39:34

Message: 12 of 21

On Feb 22, 3:19 pm, "audley james" <audle...@gmail.com> wrote:
> ImageAnalyst,
>
> here is my image :http://www.uploadhouse.com/viewfile.php?id=8151866&PHPSESSID=7d81cf17...
>
> the 2 intersecting lines in endpoint form [x1 y1 x2 y2] are:
>
> [216  135  286  132]
> [257 118  256  166]
>
> 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 = slope1 * x + offset1
y2 = slope2 * x + offset2
Then set y1 = to y2 and solve for x
slope1 * x + offset1 = slope2 * x + offset2
(slope1-slope2) x = (offset2 - offset1)
so x = (offset2 - offset1) / (slope1-slope2)
and the y value at the intersection = slope1 * x + offset1

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 22 Feb, 2011 23:53:04

Message: 13 of 21

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.

Subject: intersecting pixel of 2 lines

From: audley james

Date: 23 Feb, 2011 07:08:04

Message: 14 of 21

Paulo,

Can you please send me an email for the function

thanks alot.
james

Subject: intersecting pixel of 2 lines

From: Bruno Luong

Date: 23 Feb, 2011 07:43:04

Message: 15 of 21

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

Subject: intersecting pixel of 2 lines

From: audley james

Date: 23 Feb, 2011 08:02:18

Message: 16 of 21

Bruno,

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

Subject: intersecting pixel of 2 lines

From: Bruno Luong

Date: 23 Feb, 2011 08:07:05

Message: 17 of 21

"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

Subject: intersecting pixel of 2 lines

From: audley james

Date: 23 Feb, 2011 08:17:18

Message: 18 of 21

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

Subject: intersecting pixel of 2 lines

From: Bruno Luong

Date: 23 Feb, 2011 08:33:04

Message: 19 of 21

"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

Subject: intersecting pixel of 2 lines

From: Rune Allnor

Date: 23 Feb, 2011 08:41:06

Message: 20 of 21

On Feb 22, 7:33 pm, "audley james" <audle...@gmail.com> wrote:
> 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 ?

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

Subject: intersecting pixel of 2 lines

From: Paulo Silva

Date: 23 Feb, 2011 15:22:20

Message: 21 of 21

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us