Get diameter of circle in bitmap - c#

My problem is following. I need precisely measure diameter of circles in bitmap.
I have Bitmap with several circles. Some of them are concentric. I need values of their diameters.
I tried OpenCV and EmguCV and their method HoughCircles. But this method find circles on the places where is are no circles (I tried a lot of combinations of input parameters). Ad if it finds them there is no case, when it found exatly same circle as in the bitmap. Their centers and diameters are different then circles on the original picture. So this method is only for some kind of game. Not for my purpose(precise measuring for industry).
Do you know some way or algorithm how to do it? (I prefer C#, but if it will be in pseudocode or different langueage, I will rewrite it)
Thanks in advance.

If you could detect circles, you may benefits from this opencv function findContours() in order to get all circles as contours, then you will be able easily to calculate their areas
Then, use this formula Area = pi*r^2 to calculate r.
diameter = 2*r

You are asking for an answer to a very hard problem. The hough algorithm is not a toy solution, but it is not appropriate for all machine visions circle detection situations. Human eyes are very good at such thing (if a bit imprecise). You basically need to know a lot more about your data to approach a robust solution.
Take a look at this dicussion about Hough Circle detection as well as this paper Hough Circle Transform for a deeper understanding of the limitations
You might also want to review this paper on the ant system for ideas on a different approach.
You also might want to read up on Morpological thinning as a possible pre-preprocessing step before Houghton
Best of luck

Related

How to get intersection area of two polygons

Apologies for the lack of example code, I'm currently in the brainstorming phase of the problem and having trouble finding a proper solution.
As I have stated in my title, I want to find out what the intersection area of two polygon are.
To be more specific, I have two ARPlane's that may overlap each other on the x-z plane but be on different y-levels (imagine stairs with an overhang). I can get the area boundaries of these ARPlanes easily. My first idea to simplify the process is to remove the y-component so as to have them on the same plane and turn this into a 2D problem.
From here onward, I'm unsure of how to proceed. I could not find any methods that calculated the intersection areas of two polygons. I have a few solutions that look promising if I can get the planes aligned neatly (such that the +x direction points from the center of one of the planes to the other), but I cannot move them in any way so I must modify what the local "forward" for a plane is. Even then, I don't think the ARPlane has a direction vector in the first place as they are not GameObjects, so I am unsure if this is a viable option as a path to follow. ARPlane class for quick reference.
One other way is to turn the planes so that they're in alignment with world x axis. This looks promising over the other methods but as I previously stated, I cannot turn the actual ARPlanes. I must make a copy of them and turn the copies while keeping their relative rotations and positions the same.
So far these have been the methods I could come up with but could not develop fully due to unity restrictions. My question, then, is whether there is a way to get around the issues of these problems; failing that, whether there is an alternative solution to the issue that can be recommended.
Below is an example use case of the tool. As can be seen, some stair threads have an overhang that covers a portion of the previous thread's surface (second and third figure). Each stair thread will be scanned and then processed to find their usable surface. The area covered by the overhang is not a usable surface. This usable area is defined by the placements of a staircase thread (A), and the very next thread right above it (B); so then the usable area will be surface_area_of_A - xz_crossSection_of_AB

Slicing a 3D model?

I want to slice a 3D model relative to an infinite plane(In WPF). I'm checking if edges intersect with the infinite plane. If true, I'll create a new point at the intersection position, so I'm getting a couple of points that I want to generate a cap on so that the model is closed after slicing. For example, if this is the cross section, the result would be as follows:
Note: The triangulation ain't important. I just need triangles.
I also need to detect the holes as follows(holes are marked in red):
If it is impossible to do it the way I think(It seems to be so), the how should I do it? How do developers cap an object after being sliced?
There is also too much confusion. For example, The first picture's result may be:
What am I missing??
EDIT:
After some research, I knew one thing that I am missing:
The input is now robust, and I need the exact same output. How do I accomplish that??
In the past, I have done this kind of thing using a BSP.
Sorry to be so vague, but its not a a trivial problem!
Basically you convert your triangle mesh into the BSP representation, add your clipping plane to the BSP, and then convert it back into triangles.
As code11 said already you have too few data to solve this, the points are not enough.
Instead of clipping edges to produce new points you should clip entire triangles, which would give you new edges. This way, instead of a bunch of points you'd have a bunch of connected edges.
In your example with holes, with this single modification you'd get a 3 polygons - which is almost what you need. Then you will need to compute only the correct triangulation.
Look for CSG term or Constructive Solid Geometry.
EDIT:
If the generic CSG is too slow for you and you have clipped edges already then I'd suggest to try an 'Ear Clipping' algorithm.
Here's some description with support for holes:
https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
You may try also a 'Sweep Line' approach:
http://sites-final.uclouvain.be/mema/Poly2Tri/
And similar question on SO, with many ideas:
Polygon Triangulation with Holes
I hope it helps.
Building off of what zwcloud said, your point representation is ambiguous. You simply don't have enough points to determine where any concavities/notches actually are.
However, if you can solve that by obtaining additional points (you need midpoints of segments I think), you just need to throw the points into a shrinkwrap algorithm. Then at least you will have a cap.
The holes are a bit more tricky. Perhaps you can get away with just looking at the excluded points from the output of the shrinkwrap calculation and trying to find additional shapes in that, heuristically favoring points located near the centroid of your newly created polygon.
Additional thought: If you can limit yourself to convex polygons with only one similarly convex hole, the problem will be much easier to solve.

How to cover a polygon with triangles

I'm using slimdx in c#, and my problem is a follows:
I have a list of vertices that forms a polygon, in linestrip format, and I need to transform it to a trianglestrip that covers the polygon.
I started with a center-of-mass calculation, however it only covers convex ones, and I need a general solution.
The final result should look smth like this:
Does anyone happens to know any algorithms for the issue?
Thanks.
There's ear clipping algorithm that is quite nice for your use case, an example can be found here:
Ear clipping c#

Polygon Triangulation c#

I need to calculate triangles of a polygon. Polygon can contain holes. And Req an efficient way.
So I think I need Constrained Delaunay Triangulation.
I must do that in c#, only need calculation not drawing or something.
poly2tri seems good but idk its not working for me :S
Anyway I need help. How can I calculate that triangles?
(If your best offer is poly2tri, i can explain my problem on it)
Delaunay was not designed for this, use Ear Clipping instead.
I suppose my simple solution on github:gist (but it's rather old and probably not optimal).

Finding a pattern in latitude & longitude

I have a series of Lat/Long points in a SQL Server database. I would like to be able to find shapes. By that I mean if in the mess of coordinates there are 8 coordinates making a perfect circle, or 7 coordinates making a triangle I would like to know.
I'd be surprised if there is already something out there which does this already, especially in C# (the language I'm using). But My question is really, how should I approach this?
I probably have 200k, but their timestamped, so I should only be working with maybe 1k at a time...
What you're trying to do is called least squares fitting.
Basically, you pick a shape. Let's pick a straight line for now.
You calculate the sum of the squares of the offsets ("the residuals") of the points from the line. You do this with different lines until you've minimized the sum of the squares.
I have no idea how you would automate this for several types of shapes.
You need to find a library, or develop your self, a way to calculate Least Squares over shapes.
If the error margin is over a threshold level of R2 then you do not have that "Shape". You will need to define a formula for the shape you test against (For example a circle: x2+y2=r2).
For things that do not have curves (triangle,square, ect.) it will be harder to do as they do not have a "Formula". You can use the least square for finding each side of the shape for a line (y=mX+b) and then building those lines together to make shapes.

Categories