|
-
Aug 11th, 2002, 12:44 PM
#1
Thread Starter
Lively Member
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!
-
Aug 12th, 2002, 06:48 PM
#2
Addicted Member
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
-
Aug 12th, 2002, 06:50 PM
#3
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:
Private Type LINE_ENDS
X_i As Integer
Y_i As Integer
X_s As Integer
Y_s As Integer
End Type
Dim MY_LINES() As LINE_ENDS
Dim MY_LINE_COUNT As Long
Dim TEMP_LINE As LINE_ENDS
Private Sub Form_Load()
MY_LINE_COUNT = 0
ReDim MY_LINES(1)
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
TEMP_LINE.X_i = X
TEMP_LINE.Y_i = Y
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
TEMP_LINE.X_s = X
TEMP_LINE.Y_s = Y
If (TEMP_LINE.X_s = TEMP_LINE.X_i) And (TEMP_LINE.Y_s = TEMP_LINE.Y_i) Then
Exit Sub
End If
MY_LINE_COUNT = MY_LINE_COUNT + 1
ReDim Preserve MY_LINES(MY_LINE_COUNT)
Picture1.Line (TEMP_LINE.X_i, TEMP_LINE.Y_i)-(TEMP_LINE.X_s, TEMP_LINE.Y_s)
MY_LINES(MY_LINE_COUNT).X_i = TEMP_LINE.X_i
MY_LINES(MY_LINE_COUNT).Y_i = TEMP_LINE.Y_i
MY_LINES(MY_LINE_COUNT).X_s = TEMP_LINE.X_s
MY_LINES(MY_LINE_COUNT).Y_s = TEMP_LINE.Y_s
Call WHERE_IS_THIS_LINE(MY_LINE_COUNT)
End Sub
Private Sub WHERE_IS_THIS_LINE(ByVal WhichOne As Long)
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 & ")"
End Sub
-
Aug 12th, 2002, 08:35 PM
#4
Addicted Member
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.
-
Aug 12th, 2002, 09:21 PM
#5
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
-
Aug 13th, 2002, 04:10 AM
#6
Addicted Member
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
-
Aug 13th, 2002, 12:56 PM
#7
Thread Starter
Lively Member
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?
-
Aug 13th, 2002, 01:12 PM
#8
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
-
Aug 15th, 2002, 01:26 PM
#9
Thread Starter
Lively Member
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.
-
Aug 17th, 2002, 06:29 AM
#10
-
Aug 17th, 2002, 10:03 AM
#11
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|