Draw gradient around closed cubic spline - java

For an image editing application i'm trying to draw a gradient around a closed natural cubic spline.
The spline is drawn according to the algorithm and code explained here.
The outcome should look like this (created with GIMP using lots of gaussian blur).
As i could not find any suitable algorithm to determine the distance from the spline, i tought of the following algorithm:
Mark some key points on the spline
Grow or enlarge these points with reference to the center of the closed spline
Create a mesh of triangles between the inner points and the outer points. These
triangles will have the inner vertexes black, and the outer vertexes
white.
As you can see, this solution is quite complex (will probably have to rely on OpenGL) and hence suboptimal.
Can anybody come up with a simpler solution?
Thanks in advance.

It seems you need to build distance transform map outside the spline. Some code for doing it. It is implemented in OpenCV library too.

Related

Contour lines drawing in Java with a given Delaunay triangulation

I'm currently working on my master's thesis where I get:
A Delaunay triangulation drawn for me with given n points in (x, y, z) form.
My task is to use this triangulation and make contour lines at given z values.
I have been nearly successful at doing this by implementing the wikipedia spline interpolation : https://en.wikipedia.org/wiki/Spline_interpolation
My problem is that I get contour lines crossing each other when implementing the splines while of course the linear drawings doesn't cross.
Parametric cubic spline interpolated contour lines
If you look at the bottom part of the screen you see two contour lines crossing, I don't have enough reputation points to show that the linear drawings doesn't. You can also see that from point to point that the edges are way too rounded.
What I've tried is to interpolate more points between any pair of points to make more knot points along the lines, this to restrict them further, but to get non-crossing lines the splines look too much like a linear drawing which isn't satisfactory to the eye.
What I'd like to know, is not actual code implementation of the how, but maybe a pointer to how, readings and so forth.
(NB, I'm going to make this from scratch, no libraries).
Question: How to make a higher degree polynomial function which doesn't curve too much outside its linear counterpart. By too much I mean that a given contour at let's say 50 meters, that it doesn't cross a contour at 60 meters.
Any help is highly appreciated.
You can try a weighted delaunay triangulation. It's defined as the euklidian distance minus the weight.
Couples of years ago I solved similar task.
Here are some of my working notes. Probably it would help you.
Refer to XoomCode AcidMaps plugin, on github:
https://github.com/XoomCode/AcidMaps/tree/master/examples/isolines
Here is a demo:
http://ams.xoomcode.com/flex/index.html
Set, for example, the renderer type "Sparse" and interpolation strategy as "Linear", then press the "Update" button.
Refer to VividSolutions JTS Java library:
http://www.vividsolutions.com/jts/download.htm
http://mike.teczno.com/notes/curves-through-points.html
http://blog.csdn.net/xsolver/article/details/8913390

Draw circle using vertices or texture in OpenGL ES 2.0

I'm not sure if this question is appropriate for this site. I figured this would be the best place to ask. I need to draw 15+ circles on the screen and translate/move each of them per frame. I don't know whether I should use vertices to draw the circle, or simply draw a square and attach an image of a circle on it. I thought it would be more practical/professional to use vertices but I then thought it might be a lot to handle. If each circle had 1000 points(so its smooth), that means each circle has 1002 vertices. 15 of those make 15,030 vertices I'm multiplying by a model matrix per frame. I figured that would be a lot for a device to handle. So then I thought about simply a square with 5 vertices(using a triangle strip) and just attaching a circle image as a texture. This would only make for 75 vertices to update per frame - a lot less. I also would be guaranteed to have a smooth looking circle. I just feel like this way isn't as professional. So I came here hoping for people with experience. Which method should I use for drawing circles? Vertices or Texture mapping? Or another method perhaps? Again, I apologize if this is a dumb question or if it has already been asked.

java circle recognition from an arraylist of points

I currently have an arraylist of points from a freehand drawing on a canvas. I was wondering if there is a simple algorithm to detect if that shape represents a circle.I have already researched this a little and the main items I am pointed at are either the Hough transform or having bitmap images but both of these seem a little over the top for what I need it for. Any pointers to algorithms or implementation would be very helpful.
thanks in advance sansoms,
If I interpret your question correctly, you want to know if all the points are on a circle.
As illustrated in the picture, we pick three points A, B, C from the list and compute the origin O of the presumed circle. By checking the distance between O and each point from the list, we can conclude whether these points are on a circle.
If you do not know what the user wanted to draw (e.g., a circle, an ellipse, a line, or a rectangle), you could use some basic optimization algorithm to find the shape best matching the hand-drawn points.
for each basic shape (oval, rectangle, triangle, line, etc.), create a random instance of that shape and measure the error w.r.t. the given points
optimize each of the shapes (individually) until you have the oval best matching the given points, the rectangle best matching the points, the best triangle, etc.
pick the shape that has the lowest error and draw it
Maybe this answer can give you some ideas: https://stackoverflow.com/a/940041/12860
In short: calculate the second derivative. If it is quite constand, it is probably a circle.
Reading your comment, an easier method to draw a circle is for the user to click the center point, then drag the radius of the circle. It's a lot less calculation, and easier for the user to draw.
You can do the same thing with a rectangle, or any other convex polygon.

How to overlay one MATRIX over another? [duplicate]

Say you have a collection of points with coordinates on a Cartesian coordinate system.
You want to plot another point, and you know its coordinates in the same Cartesian coordinate system.
However, the plot you're drawing on is distorted from the original. Imagine taking the original plane, printing it on a rubber sheet, and stretching it in some places and pinching it in others, in an asymmetrical way (no overlapping or anything complex).
(source)
You know the stretched and unstretched coordinates of each of your set of points, but not the underlying stretch function. You know the unstretched coordinates of a new point.
How can you estimate where to plot the new point in the stretched coordinates based on the stretched positions of nearby points? It doesn't need to be exact, since you can't determine the actual stretch function from a set of remapped points unless you have more information.
other possible keywords: warped distorted grid mesh plane coordinate unwarp
Ok, so this sounds like image warping. This is what you should do:
Create a Delaunay triangulation of your unwarped grid and use your knowledge of the correspondences between the warped and unwarped grid to create the triangulation for the warped grid. Now you know the corresponding triangles in each image and since there is no overlapping, you should be able to perform the next step without much difficulty.
Now, to find the corresponding point A, in the warped image:
Find the triangle A lies in and use the transformation between the triangle in the unwarped grid and the warped grid to figure out the new position.
This is explained explicitly in detail here.
Another (much more complicated) method is the Thin Plate Spline (which is also explained in the slides above).
I understood that you have one-to-one correspondence between the wrapped and unwrapped grid points. And I assume that the deformation is not so extreme that you might have intersecting grid lines (like the image you show).
The strategy is exactly what Jacob suggests: Triangulate the two grids such that there is a one-to-one correspondence between triangles, locate the point to be mapped in the triangulation and then use barycentric coordinates in the corresponding triangle to compute the new point location.
Preprocess
Generate the Delaunay triangulation of the points of the wrapped grid, let's call it WT.
For every triangle in WT add a triangle between the corresponding vertices in the unwrapped grid. This gives a triangulation UWT of the unwrapped points.
Map a point p into the wrapped grid
Find the triangle T(p1,p2,p3) in the UWT which contains p.
Compute the barycentric coordinates (b1,b2,b3) of p in T(p1,p2,p3)
Let Tw(q1,q2,q3) be the triangle in WT corresponding to T(p1,p2,p3). The new position is b1 * q1 + b2 * q2 + b3 * q3.
Remarks
This gives a deformation function as a linear spline. For smoother behavior one could use the same triangulation but do higher order approximation which would lead to a bit more complicated computation instead of the barycentric coordinates.
The other answers are great. The only thing I'd add is that you might want to take a look at Free form deformation as a way of describing the deformations.
If that's useful, then it's quite possible to fit a deformation grid/lattice to your known pairs, and then you have a very fast method of deforming future points.
A lot depends on how many existing points you have. If you have only one, there's not really much you can do with it -- you can offset the second point by the same amount in the same direction, but you don't have enough data to really do any better than that.
If you have a fair number of existing points, you can do a surface fit through those points, and use that to approximate the proper position of the new point. Given N points, you can always get a perfect fit using an order N polynomial, but you rarely want to do that -- instead, you usually guess that the stretch function is a fairly low-order function (e.g. quadratic or cubic) and fit a surface to the points on that basis. You then place your new point based on the function for your fitted surface.

How to implement Hough Circle in Java

i want to find a circular object(Iris of eye, i have used Haar Cascase with viola Jones algorithm). so i found that hough circle would be the correct way to do it. can anybody explain me how to implement Hough circle in Java or any other easy implementation to find iris with Java.
Thanks,
Duda and Hart (1971) has a pretty clear explanation of the Hough transform and a worked example. It's not difficult to produce an implementation directly from that paper, so it's a good place for you to start.
ImageJ provides a Hough Circle plugin. I've been playing around with it several times in the past.
You could take a look at the source code if you want or need to modify it.
If you want to find an iris you should be straightforward about this. The part of the iris you are after is actually called a limbus. Also note that the contrast of the limbus is much lower than the one of the pupil so if image resolution permits pupil is a better target. Java is not a good option as programming language here since 1. It is slow while processing is intense; 2. Since classic Hough circle requires 3D accumulator and Java probably means using a cell phone the memory requirements will be tough.
What you can do is to use a fact that there is probably a single (or only a few) Limbuses in the image. First thing to do is to reduce the dimensionality of the problem from 3 to 2 by using oriented edges: extract horizontal and vertical edges that together represent edge orientation (they can be considered as horizontal and vertical components of edge vector). The simple idea is that the dominant intersection of edge vectors is the center of your limbus. To find the intersection you only need two oriented edges instead of three points that define a circle. Hence dimensionality reduction from 3 to 2.
You also don’t need to use a classical Hough circle transform with a huge accumulator and numerous calculations to find this intersection. A Randomized Hough will be much faster. Here is how it works (~ to RANSAC): you select a minimum number of oriented edges at random (in your case 2), find the intersection, then find all the edges that intersect at approximately the same location. These are inliers. You just iterate 10-30 times choosing a different random sample of 2 edges to settle in a set with maximum number of inliers. Hopefully, these inliers lie on the limbus. The median of inlier ray intersections will give you the center of the circle and the median distance to the inliers from the center is the radius.
In the picture below bright colors correspond to inliers and orientation is shown with little line segment. The set of original edges is shown in the middle (horizontal only). While original edges lie along an ellipse, Hough edges were transformed by an Affine transform to make those belonging to limbus to lie on a circle. Also note that edge orientations are pretty noisy.

Categories

Resources