OpenCV connecting dots - c++

I want to find the outer most corner of the dots in this image to use for a geometric transform. I intended to use hough to get the lines on the edge of the sheet and find where they intersect but it's turns out that is not possible because of the lack of lines. I would appreciate help finding alternate solutions to connect these dots.

Related

Determine Orientation of a Sketched Arrow with OpenCV

I've got a bunch of arrow sketches and I want to determine their orientation, the starting point coordinates, and the ending point coordinates.
I'm using goodFeaturesToTrack but I'm really clueless as to what to do next.
This is what I've got
There is no restriction on the shape of the arrows, they can be as windy as you want them or they could be composed of straight lines and right angles.
I'd really appreciate any ideas because I really am lost. The project is about transforming a sketch of an Finite State Machine into a VHDL code and I've got the circles and characters covered so this is the last component but I have no idea how to approach the problem.
Instead of using goodFeaturesToTrack you can use HoughLines which will give you all lines in an image so I suggest that you do some segmentation first to get each arrow separately. HoughLines gives you all lines in polar coordinates which is perfect for you case as you can know the orientation of the lines. You can read more here: https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html
For straight lines your task is easy you need to find 3 line segments that intersect in one point (http://answers.opencv.org/question/92164/how-to-find-coordinates-of-intersection-of-lines-after-using-houghlines/). This will be the end point of your arrow, you can find the start point using the equation of the middle line.
For curved lines, it is a bit more tricky as you may get several hough lines for the curved segment or none at all. So in this case I suggest you get the intersection of the the 2 lines as your end point and then use goodFeaturesToTrack then starting from the closest point to your end point start moving to the next closer until you reach the end and pick that as the starting point.
I do not know if this would be the best solution and I did not try it myself but I hope it helps you or give you some direction.

How extract lines from a messy background

Given the next canny edge result image:
I'm trying to extract the selected lines:
I did try several methods without success. For ex. I tried morphological operations but didn't work well because some times the lines are in angle or they are not completely vertical or horizontal...
I wonder if there's a method or if it is possible to extract them properly.
Thanks.
If you want to identify the longest lines try with finding contour and filter by its length.
You will get only the long connected lines irrespective of angle.
if you want that to match with line
then try houghlines
Hope it helps.

Detecting the locations of circles and crosses in an image

I'm new to OpenCV and was wondering if anybody could direct me to the most suitable algorithm(s) to tackle the challenge of identifying the locations of circles and crosses in images that look like the following . .
[
Sometimes there are lines connecting . .
[
They might even be hand drawn like this one . .
So far I have looked at the template matching example, but it is probably not the correct approach, and it doesn't scale the sizes of the templates to the images.
So given the following observations . . .
The crosses and circles may overlap.
If the diagram is in colour,
the colours will be identical for crosses and identical for circles.
Sometimes they will be joined by lines, sometimes not.
There may be other shape symbols in the plots
They symbols will be of similar size and shape, but might not be computer generated, so will not necessarily be identical.
Where should I begin my adventure?
Not an easy task.
For the colored case, you should start by separating the color planes. There is some chance that you can get the markers apart.
But for the b&w case, there is no escape, you must go deeper.
I would try to detect the grid lines first, for example using a Hough line detector, as accurately as possible. Then erase those lines.
Then try to find the crosses, which are short oblique line segments (most of the time broken by the previous operations).
The circles might be detected by a Hough circle detector, using a small range of radii.
Alternatively, a rige or edge detector can be used to get short segments and short curved arcs. You may have to add some filtering criteria to avoid the joining lines.
Like said, it's not a easy task.
One of possible way could be machine learning. I think of cascade classifier (aka Viola Jones method) that pretty nice for detection of object. It's pretty easy to implement with openCV but it need to understand how it works and a large amount of sample.
You can try to use couple of ideas:
1) FFT can help to remove grid. Something like this.
2) Markers (crosses and circles) are the objects with angle of gradient, that differs from right angles. It may help to localize them.

Approximating Lines to Floor plan contours

I am using OpenCV-C++ and 1) I want to approximate the detected contours using findContours by only horizontal or vertical lines, and not by curves, as in floor plans. So can you suggest a method for the same.
2) Is there a way to remove smaller contours like tree borders, which can automate the process for every image, since removing the smaller areas with findContours() can lead to elimination of walls with smaller dimensions.
http://property.magicbricks.com/microsite/buy/provident-welworth/floor-plan.html
On what sort of image do you use the find contours? I assume you did follow this example..
findContour example
if not, please clarify.
However, why not try to first find all horizontal and vertical edges with the corresponding filters? Afterwards you can still try to find contours with the findContours function. Or you can use the hough transform, also available in opencv. hough lines within the hough lines you can easily eliminate smaller line segments.
for 2) what do youi mean by tree borders? you mean the contours of a tree on an image? it would be very helpful if you could provide an example image.
Cheers

More accurate houghline OpenCV

I have been working with houghlines in OpenCV and I cant seem to get a more accurate line reading, sometimes there are two duplicate lines on top of each other. I have looked at tutorial on the opencv website but it gives a similar result.
To remove those duplicate lines, there are two things that may help you:
Double edges may appear that may lead to duplicate lines. A sequence of blurring/dilating the input image would solve these issues.
Close lines that have almost same slope can be removed by using lower angle resolutions for the theta argument of Hough Line method. For example using π/180 would result in finding lines that differ only by one degree in their slope. You may use 5*π/180 to find lines in 5 degree resolution.
As an example, the following lines are detected by using the raw image and a 1-degree resolution:
After a bit of blurring and using a 3-degree resolution you can get a result like the following:
By changing the threshold, you can get more or less lines.
About fitting curves you pointed in the comments section, yes you can fit curves, but not with hough lines method. You need to find a parametric definition of that shape and try to run the voting procedure in hough transform yourself. The only other shape that opencv helps you to find is circle.