Connect the dots - connect the line between contour points - java

I have a gray scale image of 64x64. I found the dots of the contour by a simple algorithm:
find brightest spot (Example: 100)
divide by 2 (100/2 = 50)
define a band around the result (50-5=45 to 50+5=55)
mark all dots that have value in the band (between 45 to 55)
The question now is, how do I decide of the order of connecting the dots?
(All references will be accepted, links, thoughts, etc')

Your algorithm allows the entire image, save for one pixel, to be "the contour". I'm not sure that's exactly what you want; usually a contour is a border between two different regions. The problem with your method is that you can get huge blobs of pixels that have no particularly obvious traversal order. If you have a contour that is a single pixel thick, then the traversal order is much more obvious: clockwise or counterclockwise.
Consider the following image.
........
..%%%%..
.%%%%...
...%%%%.
....%...
........
Here, I've marked everything "dark" (<50, perhaps) as % and everything bright as .. Now you can pick any pixel that is on the border between the two regions (I'll pick the dark side; you can draw the contour on the light side also, or with a little more work, directly between the light and dark sides.)
........
..%%%%..
.*%%%...
...%%%%.
....%...
........
Now you try to travel along the outer edge of the dark region, one pixel at a time. First, you look in the direction of something bright (directly left, for example). Then you rotate around--counterclockwise, let's say--until you hit a dark pixel.
........
..%%%%..
1*5%%...
234%%%%.
....%...
........
Once you hit position 5, you see that it's dark. So, you mark it as part of the contour and then try find the next piece on the contour by sweeping around starting from the pixel you just came from
........
..%%%%..
.0*%%...
.123%%%.
....%...
........
Here 0 is where you came from--and you're not going back there--and then you try pixels 1 and 2 (both light, which is not okay), until you hit pixel 3, which is dark.
In this way, you can walk around the contour pixel by pixel--both identifying the contour and getting the order of pixels--until you collide with the same pixel you started with and will leave from it so that you hit the same pixel that you did the first time you left it. Then the contour is closed. In our example, where we're making an 8-connected contour (i.e. we look at 8 neighbors, not 4), we'd get the following (where # denotes a contour point).
........
..####..
.##%#...
...#%##.
....#...
........
(You have to have this two-in-a-row criterion or if you have a dark region a single pixel wide, you'll walk up it but then not be able to walk back down along it.)
At this point, you've covered one entire boundary. But there might be others. Keep looking for dark pixels next to light ones until you have drawn a contour on top of all of them. Now you've converted your two-level picture (dark & bright pixels) into a set of contours.
If the contours end up too noisy, consider blurring the image first. That will smooth the contours out. (Alternatively, you can find the contours first and then average the coordinates with a moving window.)

In general, a given set of points can be connected in multiple ways to make different shapes.
For example, consider a set of 5 points consisting of the corners of a square and its center. These points can be connected to form a square with one side "dented in" to the center. But which side? There is no unique answer.
Other shapes can be much more complicated, with no obvious way to connect the dots.
If you are allowed to reduce your set of points to a convex hull, then it would be much easier.

I have also tried to create an algorithm that will connect contour dots into the smooth curve. See my open-source project http://outliner.codeplex.com.
The idea is the same as proposed by FUZxxl but I do not understand his worries about complexity: the time of processing is proportional to the total length of all contour strokes.

I don't know if collecting those points will get you far. (I can come up with situations in which it's almost impossible to tell which order they should come in.)
How about going to the brightest point.
Go to the brightness point of, say, 360 points surrounding this point at a distance of, say, 5 pixels.
Continue from there, but make sure you don't go back where you came from :)

Maybe try:
Start at a
Find the nearest point b
Connect a with b
and so on.
Probably not good, as complexity is something like O(n²). You may simplify this by looking only for points near the start, as aioobe suggest. This algorithm is good, if the points are just like 2-3px away from each other, but may create very strange grids.

See also Flood fill
and the lovely applet under SO
mapping-a-branching-tile-path .

Related

How to extract a rectangular object from image in Java

I have a photo of a paper that I hold up to my webcam, and want to minimize the area of the photo to just the paper. This way, my OCR program will potentially be more accurate, as well as conceivably faster.
I have taken a couple steps thus far to isolate the paper from the background.
First, I use Canny Edge detection, set with high thresholds. This provides a two-color representation of the edges of my image. On it, I can see a rounded rectangle among some other artifacts that happen to have sharp edges in the background.
Next, I use a Hough transformation, to draw vectors with over 100 point hits in polar coordinates on a black background. The resulting image is as shown:
See that large (the largest), almost-rectangular figure right in the middle? That's the paper I'm holding. I need to isolate that trapezoid as a polygon, or somehow otherwise get the coordinates of its vertices.
I can use these coordinates on the original image to isolate a PNG of the paper and nothing else.
I would also highly appreciate if you could provide answers to any of these three sub-questions.
-How do you find the locations of the intersections of these lines on the image?
-How would I get rid of any lines that don't form the center trapezoidal polygon?
-With these points, is there anything better than convex hull that would allow me only to get the trapezoidal/rectangular shaped region of the image?
Here is another example, in which my program produced a better image:

Efficient way to find position of beacon image in a screenshot

I have a screenshot taken from java.awt.Robot as a java.awt.image.BufferedImage and know there will be a unique 10x10 solid red (same RGB) coloured square somewhere in that screenshot (more likely closer to the middle).
What's an efficient approach to finding its coordinates? Is JavaCV even the right library to use? I found a brute force approach in .net here: Bitmap Detection but I'm wondering if there's a better way.
The first question is, what does it take to recognize the color - is it an exact RGB value, where either the color is an exact match, or it isn't? And will that still be the case if the image is lossy-compressed, for example as a JPEG?
Assuming you can do that, probably you want to do a search that tries to minimize time spent searching areas which won't be fruitful: On an imaginary line between two opposite corners, test the value at each corner and the midpoint; if no match, try at the midpoint between the midpoints and the corners; if no match, divide the space in half vertically or horizontally and try again; once you find a pixel of the right color, have some code that walks the pixels in each direction to determine if it's really 10x10.
Really any sort of search pattern will work; what you probably don't want is a situation where you just start at 0,0 and go 0,1 0,2 ... 1,0 1,2 since that will make matching in the upper left fast and bottom right slow (assuming a coordinate space that starts at the top left).

fitting two ellipses to outline of a body

I'm trying to fit two ellipses to what happens to be the top view / outline of a body. For simplicity let's use the following example:
As you can see, this simple body is made up of a long core (blue) and a head (red). In reality this outline would be in one color, I'm just using two colors for visualization purposes here.
I know how to fit a single ellipse to either parts of that outline, but I do not know how to do the fit to two ellipses, given the constraint that these two ellipses are actually connected. In this particular case the constraint is that the two ellipses will never part and that there can only be a certain angle between ellipse 1 and ellipse 2.
I'm grateful for any pointers that tell me how to write a function, so that after calling magic_fitting_function(body_outline) the program returns to me the coordinates of the two underlying ellipses:
EDIT1:
what is are the minimal requirements that could make solving this problem easier? E.g. if I were given one point, two points, etc, how would that possibly simplify the problem?
EDIT2:
I'm looking for a programming language independent solution.
EDIT3:
any hints on how to formulate the constraint of these two ellipses being located in a certain relationship to each other programmatically? E.g.: I know that the small ellipse will always be located at one end of the major axis of the big ellipse. Plus the small ellipse can only rotate by +- 90 degrees relative to the big ellipse.
I have never solved this problem, so I'm just throwing out a suggestion.
First, generate a bounding ellipse for the entire figure in order to determine what are the topmost and the bottommost points. (This step may not be necessary if you have a better way of finding these points.)
Next, detect the location of the "neck" by using a modified binary search. (Here I'm assuming that your bounding ellipse has a vertical orientation, as though the figure were standing up or standing on its head.) Generate two sets of bounding ellipses: one with an ellipse from the top of the figure to the 1/4 point of the figure (meaning if you draw a line through the bounding ellipse then the 1/4 point is between the top-left point and the middle) and with an ellipse from the 1/4 point to the bottom of the figure, and one with an ellipse from the top of the figure to the 3/4 point and with an ellipse from the 3/4 point to the bottom of the figure; the set of ellipses with the smaller total area is the one that is better encapsulating the head. Continue the search (e.g. next test an ellipse from the top to the 1/8 point / 7/8 point, and/or from the top to the 3/8 point / 5/8 point) until you've minimized the total bounding area of the set of ellipses; the point at which the ellipses meet is the neck. (No need to be too precise with this, it probably doesn't make much difference if you put the neck at the 34/256ths point or at the 35/256ths point.)
To detect the neck you may want to use bounding boxes instead of bounding ellipses.
Finally, adjust the two bounding ellipses in order to meet their angle constraints, e.g. by moving their extreme points in 5% increments (so assuming that the head ellipse's extreme points are on the y-coordinates 0 and 50 and the body ellipse's extreme points are on the y-coordinates 50 and 200, adjust them so that their extreme y-coordinates are on 0 and 60 and on 40 and 200).
If you have the complete outline, you can find where the two ellipses intersect - just look for the two sharp corners where the first derivative of your outline becomes discontinuous. Then, draw a straight line between those corners.
Everything on one side of the line is in ellipse A, everything on the other side is in ellipse B. The sharp corners are in both ellipses. Now, just fit a single ellipse to each of the two ellipses you've found and recalculate the points where the fitted ellipses intersect.
You can try isolating ellipses using the Hough transform. There are some FEX tools out there that are worth trying, for example this Ellipse Detection Using 1D Hough Transform.
Find the two points that are furthest from each other. One will belong to Ellipse1, the other to Ellipse2.
Check the nearest neighbors of these points to get 5 points belonging to Ellipse1, and 5 to Ellipse2.
Check out Wikipedia, and choose your favorite equation of an ellipse.
Using junior high algebra, for each ellipse, plug in the points to get 5 simultaneous equations, and solve these to get the 5 parameters that define your ellipse.
EDIT
I didn't realize this was tagged "matlab". In that case, once you have identified some points for each ellipse, there are matlab functions for fitting the points to an ellipse.

Polygon "Fixing" Algorithm

I bring you a maybe complex question which i would love your help with. Allow me to go straight to the point:
I desire an algorithm or logic in which i draw a shape using my mouse (for example a square) and it becomes a perfect square, with all the 4 sides in straight lines and perfectly regular. A human-drawn square is hardly perfect, but i wish that after it goes through the "filter" of this algorithm ,it becomes such.
A fine example of what i wish is in the game Trine, where the Wizard works by a similar principle: You draw a shape in the screen and it becomes the closest shape, that is, if you draw something similar to a square it becomes a perfect square box, but if you draw a triangle it becomes a perfect triangular box. Its like it detects what kind of shape it is and then draws a better version of it.
I want this for a game, just so you know what is the goal of all this.
Please help me figure out either the algorithm or logic behind this, or at least tell me what is the name of this kind of action (:
P.S. i added a simple image so it becomes even more clear what i intend =)
If I had to implement this task, I would store the recognizable patterns, and would try to make a match for them.
Take the minX, maxX, minY, maxY values form the user-drawn points, that will help you to scale the pattern. Choose the scaling so that the aspect ratio for the pattern would be the average of the X and Y aspect ratios.
The patterns can consist of certain number of straight lines. The pattern matches if
There are no points outside of the threshold
There is at least one user-drawn point close to each key points in the pattern
If you have the pattern matched, you will have the key points for your pattern (calculating the center of your pattern, and the size/aspect ratio). Then you can replace the user-drawn points with your image - that may be totally different from the pattern used to match (imagine a circle).
There are many ways to do this. One way that you could do it is to create a neural net that recognizes these shapes. I would generate variations of circles, squares, lines, and triangles with random perturbations to replicate "hand-drawn" versions. Then you would want to represent this as a two-dimensional array (where locations that have been drawn on would be 1's and locations that haven't been drawn on, would contain 0's). You can then convert this two-dimensional array into an input vector of n x n elements. The output of the neural net would be a vector with four elements, each one representing either a line, circle, square, or triangle. You would then train this neural net using your randomly-perturbed images until you end up with a neural net that recognizes the input with an error that is under some error-threshold. This is actually quite similar to recognizing handwritten digits.
Other ways include:
Shape contexts.
k-means clustering
Support vector machines
You don't have just an arbitrary shape, you also have the shape's path. So try counting corners. Decide on a angle threshold that will represent a corner. For each point, sample the next consecutive x number of points. Measure the angle between the first half and second half. If the angle surpasses your threshold, consider it a corner. (Obviously select the point that give you the best angle with the least amount of error, not just the first one that surpasses the threshold.) Mark the location of the corners and draw your shape to match.
Ellipses & lines: if no angles are detected, sample a few segments. Measure the orientation. If they are very similar, then line. If very different, then ellipse. If ellipse, find the bounding box and draw inside.

Define if a contour is closed or not

I need a way to define if a contour represents a line or a closed shape. In Java, I have an object Shape which contains all the points which defines it again as separate objects. The object Point represents the coordinates of the point. I tried parsing the shapes with a recursion but for larger shapes, more then 150 points, the performance is very poor. I am attaching a picture of the shapes I want to parse to help better understand the question.
I am putting a image for better visualisation of the problem.
This is just showing all the shapes I got. I want to display just the two closed ones.
Thanks in advance.
Vassil Kossev
First idea: Use a suitable contour tracing algorithm to get an ordered contour. If your contour is closed you will get back to the first point eventually.
Second idea: Use a flood filling algorithm: if you get out of the bounding box of your object it is open, otherwise it is closed.
Third idea: use morphology. Remove lone pixels. Find all endpoints and branchpoints. Remove all branchpoints. Connected components with no endpoints are closed contours ("circles"), connected components with two endpoints are open contours ("lines"). Re-project connected components with no endpoints to the original image and keep only connected components that have common part with them. I think this could be implemented real-time, and the easiest to implement.
If you have contour lines of 1 pixel width, then you can count the number of neighbors for each point *. If every point of a given contour has 2 neighbors, then the contour is closed. If there are 2 points with only 1 neighbor each, then the contour is open.
If your contours are thicker, then you can apply a skeletonization algorithm to make them exactly 1 pixel thin. An interesting case is when you have side branches on a contour, but in this case there must be branching points with 3 neighbors, so similar situations can be handled easily.
* Counting neighbors is easy: use the original image! Choose one point of the contour randomly, check the neighboring 8 pixels, and count those which are part of the contour. Then repeat the neighbor-checking for these, and so on, until all pixels of the contour have been checked.
If the points are stored in order, you might be able to compare the first and last points. If they're spatially equal, the contour is closed.

Categories