Predict the starting and end point from a sequence of points [closed] - math

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a sequence of points ( not vertices's ) which makes a open shape but they are randomly arranged in a array, but to effectively recognize the shape what I need to do is to arrange the points in order the shape was made.
Is there any way in which I can recognize which is the start and end point?
Edit: The points are in a 2D plane and I have the boundingbox. The points are obtained from a image processing algorithm which gives me the points from top to bottom (not in the order shape was made) as the image is processed pixel by pixel.

Given a set of points, there are various ways to reconstruct a shape from them. If you only take the distance between two points into account, then this basically boils down to a classical traveling salesman problem: you're looking for a tour through all points which minimizes the length of said tour. You could add one mor atrificial point which has the same distance to all points in the plane. That would turn a cyclic tour into a linear one: the point after that artificial point would be the starting point, and the point just before it would be the end point. There might be TSP approximation algorithms out there which can do non-cyclic tours out of the box.
Only considering distance ignores any inertia inherent to the motion of the painting device. So I don't claim that this is the best possible model. But for most models, I would still think about a way to determine the order all the points, which automatically results in identifying the start and end point. I can't think of a way to reliably identify these two without a way to order all points in between as well.

EDIT: Seems like you want to implement image segmentation instead:
Paper
Scroll down to page 28 for the mathematical description. This will give you the contours. Does that get you to where you want to be?
What you are trying to do is implement
OCR
without the use of an external library. I wouldn't recommend it for production purposes, but think it would be a great problem to tackle.
My suggestion would be to build a simple Artificial Neural Network with the image surface providing the input signals. A feed-forward, backpropagation ANN is quite easy to code, but extremely tricky to get the results you want.
You are, after all, tackling a problem that has no perfect solution yet despite many attempts. I wish you the best of luck and please keep us updated with your progress!

Related

How to generate a mathematical formula by its graph? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Given a graph of a formula, how can I find its formula?
Do I need some OCR or something like that?
In general you need:
detect the graph position (bounding box) in image
you are usually looking for rectangle outlined (usually in black) area
extract/input graph scales
either input them by user or read via OCR (as you suggested). The scale values and units are usually along the x,y axises of the graph bounding box. You need to find at least 2 separate points per axis to form transformation from pixels to units. In case of non linear scales you need to have more points to detect log,etc scales ...
extract graph points
the graph curve is usually in different color or stroke width so it should be relatively easy to extract all points. Form a list of points (in case of function sorted by x).
apply curve fitting on the points from #3
there are many approaches ... can use piecewise interpolation, piecewise approximation BEZIER/SPLINE, interpolation polynomials (Lagrange...) or FFT/DCT (sin waves). For non complicated shapes approximation search minimizing distance between points and computed polynomial will be enough. This step mainly depends on what precision, output function form, runtime you need.
Hard to be more specific as you did not provide any details what so ever.
Finding the mathematical algorithm to which matches an input and output together might help a bit.

Least square method doesn't work as expected, or does it? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I tried to improve trilateration accuracy by doing least square method. For initial estimation, I get the average value of the cluster points. This value is then increased until the distance to the next estimation is small enough. The increment value is calculated using this formula,
My question is, why most of the times the final answer diverts quite significant from what it should be? The initial estimation is even better, although not that accurate.
Do I miss something here?
Edit
The formula is as explained here. I hope this picture explains better,
See the final point is even outside the intersection area.
I think I have a clear enough idea of the problem to offer an answer.
Basically the three-cornered region's interior consists entirely of points that undershoot the three estimated distances (inaccurate circle radii). So it is not surprising that an iterative improvement to the solution, seeking a minimum squared error approximation, will move the point outside that three-cornered region.
More about why the points inside the region give distances that are below the given estimates: These points are exactly those that are inside all three circles (if such an arrangement holds). Therefore the three distances from such a point to the circles' centers are all below their respective radii.
Using the average of the three corner points (is this what is meant by cluster points in the question?) is probably a pretty good way to start. If there is a simple place to improve the calculation, it might lie in using a weighted least squares criterion rather than an absolute least squares criterion.
What I mean by this is that if one radius is 10 yards, and the other two radii are much larger (say 200 and 300 yards for the sake of discussion), it probably doesn't make sense to assume the estimated distances all have errors of about equal size (which is what an absolute least squares fit looks for). Instead assuming the error in the estimated distances is roughly proportion to each distance (a relative error criterion) is more likely to produce a better solution, e.g. giving a greater weight to the shorter distance (because a proportional error in that would be less in absolute magnitude than the proportional error in the longer distances).
This is just a sketch of one idea you might want to incorporate in your solution. I take you have only the three data to work with (locations known to fairly good accuracy as centers of circles, greater uncertainty in the three radii). So it doesn't make sense to try and apply methods that are sophisticated in respect of accuracy, but rather to prefer methods that give a robust solution. I think the relative error criterion would move you in that direction.
Least squares minimizes the overall square of the errors, but it says nothing about how close an individual point will be to the true value. The coefficients are affected by all the points, not just a few.

How to determine all line segments from a list of points generated from a mouse gesture?

Currently I am interning at a software company and one of my tasks has been to implement the recognition of mouse gestures. One of the senior developers helped me get started and provided code/projects that uses the $1 Unistroke Recognizer http://depts.washington.edu/aimgroup/proj/dollar/. I get, in a broad way, what the $1 Unistroke Recognizer is doing and how it works but am a bit overwhelmed with trying to understand all of the internals/finer details of it.
My problem is that I am trying to recognize the gesture of moving the mouse downards, then upwards. The $1 Unistroke Recognizer determines that the gesture I created was a downwards gesture, which is infact what it ought to do. What I really would like it to do is say "I recognize a downards gesture AND THEN an upwards gesture."
I do not know if the lack of understanding of the $1 Unistroke Recognizer completely is causing me to scratch my head, but does anyone have any ideas as to how to recognize two different gestures from moving the mouse downwards then upwards?
Here is my idea that I thought might help me but would love for someone who is an expert or even knows just a bit more than me to let me know what you think. Any help or resources that you know of would be greatly appreciated.
How My Application Currently Works:
The way that my current application works is that I capture points from where the mouse cursor is while the user holds down the left mouse button. A list of points then gets feed to a the gesture recognizer and it then spits out what it thinks to be the best shape/gesture that cooresponds to the captured points.
My Idea:
What I wanted to do is before I feed the points to the gesture recognizer is to somehow go through all the points and break them down into separate lines or curves. This way I could feed each line/curve in one at a time and from the basic movements of down, up, left, right, diagonals, and curves I could determine the final shape/gesture.
One way I thought would be good in determining if there are separate lines in my list of points is sampling groups of points and looking at their slope. If the slope of a sampled group of points differed X% from some other group of sampled points then it would be safe to assume that there is indeed a separate line present.
What I Think Are Possible Problems In My Thinking:
Where do I determine the end of a line and the start of a separate line? If I was to use the idea of checking the slope of a group of points and then determined that there was a separate line present that doesn't mean I nessecarily found the slope of a separate line. For example if you were to draw a straight edged "L" with a right angle and sample the slope of the points around the corner of the "L" you would see that the slope would give resonable indication that there is a separate line present but those points don't correspond to the start of a separate line.
How to deal with the ever changing slope of a curved line? The gesture recognizer that I use handles curves already in the way I want it too. But I don't want my method that I use to determine separate lines keep on looking for these so called separate lines in a curve because its slope is changing all the time when I sample groups of points. Would I just stop sampling points once the slope changed more than X% so many times in a row?
I'm not using the correct "type" of math for determining separate lines. Math isn't my strongest subject but I did do some research. I tried to look into Dot Products and see if that would point me in some direction, but I don't know if it will. Has anyone used Dot Prodcuts for doing something like this or some other method?
Final Thoughts, Remarks, And Thanks:
Part of my problem I feel like is that I don't know how to compeletly ask my question. I wouldn't be surprised if this problem has already been asked (in one way or another) and a solution exist that can be Googled. But my search results on Google didn't provide any solutions as I just don't know exactly how to ask my question yet. If you feel like it is confusing please let me know where and why and I will help clarify it. In doing so maybe my searches on Google will become more precise and I will be able to find a solution.
I just want to say thanks again for reading my post. I know its long but didn't really know where else to ask it. Imma talk with some other people around the office but all of my best solutions I have used throughout school have come from the StackOverflow community so I owe much thanks to you.
Edits To This Post:
(7/6 4:00 PM) Another idea I thought about was comparing all the points before a Min/Max point. For example, if I moved the mouse downards then upwards, my starting point would be the current Max point while the point where I start moving the mouse back upwards would be my min point. I could then go ahead and look to see if there are any points after the min point and if so say that there could be a new potential line. I dunno how well this will work on other shapes like stars but thats another thing Im going to look into. Has anyone done something similar to this before?
If your problem can be narrowed down to breaking apart a general curve into straight or smoothly curved partial lines then you could try this.
Comparing the slope of the segments and identifying breaking points where it is greater then some threshold would work in a very simplified case. Imagine a perfectly formed L-shape where you have a right angle between two straight lines. Obviously the corner point would be the only one where the slope difference is above the threshold as long as the threshold is between 0 and 90 degrees, and thus a identifiable breaking point.
However, the vertical and horizontal lines may be slightly curved so the threshold would need to be large enough for these small differences in slope to be ignored as breaking points. You'd also have to decide how sharp a corner the algorithm should pick up as a break. is 90 deg or higher required, or is even 30 deg enough? This is an important question.
Finally, to make this robust I would not be satisfied comparing the slopes of two adjacent segments. Hands may shake, corners may be smoothed out and the ideal conditions to find straight lines and sharp corners will probably never occur. For each point investigated for a break I would take the average slope of the N previous segments and compare it to the average slope of the N following segments. This can be efficiently implemented using a running mean. By choosing a good sample number N (depending on the accuracy of the input, the total number of points, etc) the algorithm can avoid the noise and make better detections.
Basically the algorithm would be:
For each investigated point (beginning N points into the sequence and ending N points before the end.)
Compute average slope of the N previous segments.
Compute average slope of the N next segments.
If the difference of the averages is greater than the Threshold, mark current point as a breaking point.
This is quite off the top of my head. You'd have to try it in your application.
if you work with absolute angles, like upwards and downwards, you can simply take the absolute slope between two points (not necessarily adjacent) to determine if it's RIGHT, LEFT, UP, DOWN (if that is enough of a distinction)
the art is to find a distance between points so that the angle is not random (with 1px, the angle will be a multiple of 45°)
There is a firefox plugin for Navigation using mouse gestures that works very well. I think it's FireGestures, but I'm not sure. I guess you can get some inspiration from that one
Additional thought: If you draw a shape by connectiong successive points, then connecting back to the first point, the ratio between the area and the final line segment's length is also an indicator for the gesture's "edginess"
If you are just interested in up/down/left/right, a first approximation is to check 45 degree segments of a circle. This is easily done by checking the the horizontal difference between (successive) points against the vertical difference between points.
Say you have a greater positive horizontal difference than vertical difference, then that would be 'RIGHT'.
The only difficulty then comes for example, in distinguishing UP/DOWN from UP/RIGHT/DOWN. But this could be done by distances between points. If you determine that the mouse has moved RIGHT for less than 20 pixels say, then you can ignore that movement.

Programming math-based images for use in high-resolution artwork [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm interested in creating poster-sized images that contain repeating patterns, similar to the two (public domain) images below, the Flower of Life and a Penrose tiling:
My questions:
How do people usually create images like these on a computer? I'm hoping the answer isn't, "Open Adobe Illustrator and guess at intersection points," since such points can be defined mathematically. But I also imagine that not everyone with an interest in geometric patterns is also familiar with programming.
What is the best environment for creating such images? In particular, what's the best way to get high-resolution images out of Java, Python, Processing, etc? Or, is Mathematica the best tool?
Actually calculating the points and doing the math isn't the hard part, in my mind (at least, it's not the focus of this question). I'm interested in the best way to get a high-quality visual product out of a program.
The best way to create images like these is to learn to write PostScript. It's a clean language, easy to learn, and quite powerful once you know it well.
Bill Casselman's manuals are by far the best reference for high quality mathematical illustration.
Use a vector image format like SVG. This will scale perfectly to any resolution.
Inkscape is a great tool for creating these.
Once you have a vector image format, there are many options for using it in programming languages, depending on your language of choice.
For example -
.NET - SvgNet
ActionScript - svgweb
C++ - LeadTools
XAML for WPF/Silverlight - ViewerSVG (Converts SVG to XAML)
I don't know how those images were created, I would guess they were scanned from a book, but, in my work with fractals, I tend to start with just using the <canvas> tag, mainly so that I can change the size of the element and see it drawn more iterations, so I can get the highest resolution.
That is the problem with something like SVG is that you would need to pick a resolution and then create it, and it will scale well up and down, but if you developed it at one resolution, then you go to a higher resolution to demo, you may see more gaps than you would like.
If you want to just do it and save it as a static image then any GUI will work, as you are saving a GIF at that point, but if you want it, for example, on a web page, and have it look as good as it can on that browser then you may want to look at using javascript.
The math part isn't hard, and so drawing the image is fairly easy, once you derive the recursive algorithm that is needed. I tend to go to the next iteration until the size is below a threshold, for example, a radius of < 3, then it exits.
Well, #2 is going to be kind of a holy war so I'll address #1. :)
The key to images of this nature is recursion. Basically they are the same image repeated over and over in a controlled way to get an intersting result. Take the flower of life for example. You repeat the center petal six times (the method to do the petals is up to you). Then you create six more flowers using the petals tip as the center and overlapping one of the petals. You then recursively move outward. After a few "rounds" you stop and draw the containing circle. Basically the recusion simulates the stamp, move and rotate that would be required if you were doing it by hand.
When I have played around with these kinds of things I have always found that experimentation is the best way to get cool new things. Of course that could be just my lack of imagination. :)
I know I am not very math heavy in this answer but that is up to you and experimentation. Just remember that COS and SIN are your friends and there are 360 degrees in a cricle (or 2pi radians depending on your math package).
EDIT: Adding some math for the "Flower"
Starting with a center of (Xo, Yo) and a flower radius of r...
The tips of the petals (P0, P1, etc) are determined by...
X = Xo + (sin((n * pi)/3 + (pi / 6)) * r)
Y = Yo - (cos((n * pi)/3 + (pi / 6)) * r)
where n is the petal number (0..5)
Once you compute a petal tip, just draw the petal and then start a new flower at the tip. You would also set a bounding circle so that any point outside that circle would not be drawn.
I would try to create a PDF with iText in Java. PDF supports vector graphics, so it should scale without problems. I don't know how well iText scales w.r.t. performance when you have a really big number of graphic elements.
A1. You might want to look at turtle-graphics, l-systems, iterated function systems, space filling curves, and probably a lot of other approaches I'm not familiar with or haven't thought of yet.
A2. You can program any of these with any of the languages you suggest. I like Mathematica, but I know that not everyone has a copy of it and I have a copy 'cos I work in number-crunching and get to play with it for making pretty pictures. But Processing, which is free, was designed to be artist-friendly and might be a better starting point for you. Both Mathematica and Processing do the graphics right there and then, no calls to external libraries (or worrying which ones to use).
And, while I agree with everyone who says that vectors are the way to go, don't forget that the final productions step, onto paper or screen, is rendering so give some thought to how that will be done. This might, for example, lead you to Postscript or PDF for an output format.
Have fun
Mark
Well, I used to draw flowers of life with a compass, back then in junior school ... very simple actually ... but I don't think that's the answer you're looking for.
Basically it consists of drawing a circle of the same radius, from every point, until you encounter the big circle (limit).

How can I divide the globe in to small grids such that it allows me to assign each lat/long location to a grid? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In order to calculate the nearest locations that are represented by latitude/longitude, I was considering dividing the map into small grids, approximately 100x100 meter grids. Essentially each point would be assigned to a grid.
I understand that I could instead also use spatial indexes with MySQL etc, but am planning to use a non-relational database like Cassandra where it would be difficult to do indexing on spatial objects, and so some kind of grid approximation technique could be neat.
What would be the best way of creating such a grid system and mapping the 2-D spatial locations to it?
Edit1: It might be alright if the grids are not perfectly uniform, more so around the poles.
Mapping from the two-dimensional spatial coordinates to your spatial index / geohash is an interesting problem. You might look at this article on quadtrees, geohashes and Hilbert curves. The Hilbert curve is a space-filling curve that provides locality; for your purposes, that means that nearby items in the one-dimensional spatial index will be nearby in two-dimensional space.
The goal (as described by other responders) is to minimize the number of queries necessary to cover the space in question without requesting tons of unnecessary data from the server. How you do the mapping from 2-d space to a 1-d index will affect that goal.
Without knowing your exact application requirements Geohashing might be an appropriate technique: http://en.wikipedia.org/wiki/Geohash
"It is a hierarchical spatial data structure which subdivides space into buckets of grid shape.
Geohashes offer properties like arbitrary precision and the possibility of gradually removing characters from the end of the code to reduce its size (and gradually lose precision)."
Rectangular grids can be a reasonable estimation, but only over a relatively small area that isn't too close to the poles. A full-globe solution requires a different approach.
You can't create a rectangular grid which uniformly maps a globe. If the grid must be uniform, you must use triangles instead. But in general, I doubt that this will solve your issue. What you need is an 2D octree (this is a Google search link; check the images for an easy clue how this works) of some kind: You must divide your coordinates into hierarchies (for example north/south/east/west of the origin for the first level and then between 90 degrees, etc).
Then you can do a couple of selects which will quickly yield the smallest rectangle which does contain existing coordinates. Now, you can check the size of the rectangle. If it's < 100m, then you've found a solution. Otherwise, you'll have just a few positions to check against (usually one).
Google for "octree sql database" for implementations.

Resources