Results 1 to 11 of 11

Thread: Line

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    103

    Line

    Hi all!
    I want to nake my program recognize straight line like an object.
    For exmaple: I drawn a line from (0, 0) to (300, 700) and after I insert this picture into my program and the program outputs the start and the end point of the line( (0, 0) and (300, 700) ).
    Please, help me. I really need it.
    Thanks!

  2. #2
    Addicted Member
    Join Date
    Jul 2002
    Location
    BC, Canada
    Posts
    152
    Before coding anything, I think it's best if you (and we) know what the problem is exactly.

    In your image (PictureBox?) is there ONLY straight lines (and pure white or pure black background)?

    If no, then you'll have to figure out how to identify a straight line.

    If yes, then it's not so bad. I'm not sure of any specific algorithm, but I'd base my code on the fact that a straight line is straight. By this, there are 4 main classifications of (I'm assuming just a 2D line in X-Y plane):

    - vertical
    - horizontal
    - positive slope (neither horizontal nor vertical)
    - negative slope (ditto)

    I would use a tic-tac-toe-like grid around a pixel you know is on the line, and check the outer sides for another pixel that is also on the line.

    I guess I should stop here and ask if it's possible for the grid to have the possible combination, where X is a pixel that is part of the line?
    PHP Code:
    -  X  X
    -  X  -
    X  X  
    There also might be some code around on the forums that does this - I thought I saw this once upon a time...

    Destined

  3. #3
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Use the MouseDown/MouseUp events.
    {Which you must be doing already, just to draw the line}

    Store each set of {X,Y} per each line.

    Best use something like:

    VB Code:
    1. Private Type LINE_ENDS
    2.     X_i As Integer
    3.     Y_i As Integer
    4.     X_s As Integer
    5.     Y_s As Integer
    6. End Type
    7. Dim MY_LINES() As LINE_ENDS
    8. Dim MY_LINE_COUNT As Long
    9. Dim TEMP_LINE As LINE_ENDS
    10.  
    11. Private Sub Form_Load()
    12.     MY_LINE_COUNT = 0
    13.     ReDim MY_LINES(1)
    14. End Sub
    15.  
    16. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    17.     TEMP_LINE.X_i = X
    18.     TEMP_LINE.Y_i = Y
    19. End Sub
    20.  
    21. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    22.     TEMP_LINE.X_s = X
    23.     TEMP_LINE.Y_s = Y
    24.     If (TEMP_LINE.X_s = TEMP_LINE.X_i) And (TEMP_LINE.Y_s = TEMP_LINE.Y_i) Then
    25.         Exit Sub
    26.     End If
    27.     MY_LINE_COUNT = MY_LINE_COUNT + 1
    28.     ReDim Preserve MY_LINES(MY_LINE_COUNT)
    29.     Picture1.Line (TEMP_LINE.X_i, TEMP_LINE.Y_i)-(TEMP_LINE.X_s, TEMP_LINE.Y_s)
    30.     MY_LINES(MY_LINE_COUNT).X_i = TEMP_LINE.X_i
    31.     MY_LINES(MY_LINE_COUNT).Y_i = TEMP_LINE.Y_i
    32.     MY_LINES(MY_LINE_COUNT).X_s = TEMP_LINE.X_s
    33.     MY_LINES(MY_LINE_COUNT).Y_s = TEMP_LINE.Y_s
    34.     Call WHERE_IS_THIS_LINE(MY_LINE_COUNT)
    35. End Sub
    36.  
    37. Private Sub WHERE_IS_THIS_LINE(ByVal WhichOne As Long)
    38.     MsgBox "This Line Starts at (" & MY_LINES(WhichOne).X_i & "," & MY_LINES(WhichOne).Y_i & ")" & Chr$(13) & "And it Ends at (" & MY_LINES(WhichOne).X_s & "," & MY_LINES(WhichOne).Y_s & ")"
    39. End Sub

  4. #4
    Addicted Member
    Join Date
    Jul 2002
    Location
    BC, Canada
    Posts
    152
    I thought (s)he needed a program to find the line, not to tell you it when you draw it. I thought drawing the line was for testing, whereas the program looks (with no knowledge beforehand) for the lines.

    Destined.

  5. #5
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by Destined Soul
    I thought (s)he needed a program to find the line, not to tell you it when you draw it. I thought drawing the line was for testing, whereas the program looks (with no knowledge beforehand) for the lines.

    Destined.
    True, I think you're right. After I posted My comment, I saw that
    you had gotten yours in before me, and after I read that, I
    tended to agree.

    However, Since he is useing a progie to write a line on an image,
    and then inputs that image to another progie later on to read
    where the lines are, I'd still use a variant of what I wrote, and
    tag the Line Info to the end of the Picture, as a readable
    resource. Place a chr$(0) at the end of the picture, then place the
    data as binary after that, then place a long indicating the length
    of the binary data preceeding it.

    Then, it becomes just a matter of reading binary data, instead of
    trying to discriminate a graphical line thru BitMap Byte Algorythms.



    -Lou

  6. #6
    Addicted Member
    Join Date
    Jul 2002
    Location
    BC, Canada
    Posts
    152
    True, NotLKH.

    idinkin, are lines created in such a way that you can save the data? As !LKH says, it's much easier to save it than to figure it all out from scratch.

    Not that it can't be done, though.

    Destined

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    103
    No, I have lines in picturebox, the lines were loaded into its picture property. So now I have to identify the endpoints of the line. The lines have can be in any direction, and the number of lines is infinite. The background is white and the lines' foreground is black. Any suggestions?

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    1) So the picture is only white, with black lines, right? Nothing Else?

    2) What do you have so far?

    3) Are we talking two different programs, 1 to draw, and the other to detect?

    -Lou

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    103
    1) Yes.
    2) Nothing.
    3) No. I have drawn the lines in paint and then I loaded the picture into a picture box and now I have to make my program detect the lines.

  10. #10
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Cool OK...

    I don't know how you would do it, but i would search for the "end-points", i.e the pixels with only one/two other black pixels in the eight around it. (if there are two, they must be next to eachother)
    I would then select every pair of end-points, and using a loop, check if every pixel between them is black. If so, they form a line.
    sql_lall

  11. #11
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    no need for every pixel, just check the mid points, or the midpoints of the midpoints are most likely enough (unless you want something thats 100% accurate -- but checking every pixel still won't be accurate). but however one question remains, how do you know you found the end-point pixel? what i would do, find any pixel which is black (assume ur line is single-pixel-wide, or else, find any unit (maybe 2x2 pixel) which is black) and start from the east, check the circumference of the circle with a radius x for another black pixel verify it (by checking the midpoint), and then calculate the slope. now you just follow the slope to find the end points.
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width