Line detection with OpenCV - opencv

I'm trying to detect the presence of lines from a picture of a geometrical drawing. For example, there is a triangle, and I'm looking for the bisector of one of its angles. So I know exactly where and how long the line should be.
My approach so far is to detect all the lines with the Hough transform function and look for my line among those. However, this is rather slow and conceptually not very nice. Indeed, since I know the two extremities of the segment I'm looking for, it seems more natural to directly look for at this very place.
To do this, my first intuition was to use the result of Canny and loop through each pixel that should contain a line. Before I implement this, I was wondering if something similar already existed or if someone more expert in CV would recommend another approach. I've seen this but I'm looking for lines, not contour, so not sure this would work...

The Canny idea might work quite well. Since you were looking for other ideas, you might also consider trying corner detection. Assuming the geomerical shape is segmented you might try either the classic Harris corners or you might try some of the newer feature detectors like SURF, SIFT, FAST, etc. You may have to adjust the parameter sensitivities of these detectors in order to isolate the corners, but that might allow you to compute the lines based on a set of points. Also, a combination of Canny edge detection and feature detection might be more robust as well.
Just a thought :) Hope it helps!

Related

Most 'fitting' placement of shape in silhouette

I apologize in advance for the lack of code, but I have been looking at this problem for some days now and couldn't proceed much.
I am trying to find a method that, given a template shape and a figure, finds the most fitting (edges-wise) placement of such shape in the (binarized) figure.
For example, in the picture above, I've highlighted 2 placement for the triangle on the right (I show 2, but there are probably 5). The blue one covers more "perimeter" and thus should be preferred.
The rotations are limited to steps of 45 degrees and no scaling is involved, so I don't think I need some particularly rotation/size invariant methods.
For now, I have obtained some results using opencv template matching, by first extracting the edges with canny and matching the resulting contour, but it is far from precise and I am afraid this would not work for more complex figures. Also by using canny I lose information about the binarized foreground and background, while the placement must be only inside the figure.
Any idea would be appreciated because I really could not find much in the literature (also I am quite inexpert in the field and probably lacking the proper terminology for effective searches), thank you in advance

Avoid hough transform

This is my first question here!
I have found the Hough Transform method not very robust to noise. Can I totally avoid it and efficiently find lines in the image?
Thanks!
There are various edge detectors that provide more general output than lines, such as the Canny edge detector. With suitable processing, this can give you 'edge chains' made of 'edgels'. From there, you can do some analysis of the edge chains to split it into line segments (generally based on looking for 'kinks' in the chain to identify corners.) This can be a fairly noisy process too, though, and there are lots of different kinds of heuristic criteria you might use to refine your set of lines further.
In general, the Hough Transform is a first-pass that gives you a (noisy) set of candidate lines in the image and is suitable when you don't know where lines might be. From there, you need to do further analysis of your candidates to determine which match what you're actually looking for. Like most problems in computer vision, the details of how you actually apply these basic tools depends very much on what you're attempting to analyze.

OpenCV - Detect rough, hand-drawn circles with obstructions

I've been trying to extract hand-drawn circles from a document for a while now but every attempt I make doesn't have the level of consistency I need.
Process Album
The problem I keep coming up against is when 2 "circles" are too close they become a single contour, ruining my attempt to detect if a contour is curved. I'm sure there must be a better way to extract these circles, but their imperfection and inconsistency are really stumping me.
I've tried many other ways to single out the curves, the most accurate of which being:
Rather than use dilation to bridge the gap between the segmented contours, find the endpoints and attempt to continue the curve until it hits another contour.
Problem: I can't effectively find the turning points of the contour, otherwise this would be my preferable method
I apologize if this question is deemed "too specific", but I feel like Computer Vision stuff like this can always be applied elsewhere.
Thanks ahead of time for any and all help, I'm about at the end of my rope here.
EDIT: I've just realized the album wasn't working correctly, I think it should be fixed now though.
It looks like a very challenging problem so it is very likely that the things I am going to write wouldn't work very well in practice.
In order to ease the problem, I would probably try to remove as much of other stuff from the image as possible.
If the template of the document is always the same, it might be worth trying to remove horizontal and vertical lines along with grayed areas. For example, given the empty template, substract it from the document that you are processing. Probably, it might be possible to get rid of the text also. This would result in an image with only parts of hand drawn circles.
On such image, detecting circles or ellipses with hough transform might give some results (although shapes might be far from circles or ellipses).

how to find shapes that are slightly elongated oval / rectangle with curved corners / sometimes sector of a circle?

how to recognise a zebra crossing from top view using opencv?
in my previous question the problem is to find the curved zebra crossing using opencv.
now I thought that the following way would be much easier way to detect it,
(i) canny it
(ii) find the contours in it
(iii) find the black stripes in it, in my case it is slightly oval in shape
now my question is how to find that slightly oval shape??
look here for images of the crossing: www.shaastra.org/2013/media/events/70/Tab/422/Modern_Warfare_ps_v1.pdf
Generally speaking, I believe there are two approaches you can consider.
One approach is the more brute force image analysis approach, as you described. Here you are applying heuristics based on your knowledge of the problem in order to identify the pixels involved in the parts of the path. Note that 'brute force' here is not a bad thing, just an adjective.
An alternative approach is to apply pattern recognition techniques to find the regions of the image which have high probability of being part of the path. Here you would be transforming your image into (hopefully) meaningful features: lines, points, gradient (eg: Histogram of Oriented Gradients (HOG)), relative intensity (eg: Haar-like features) etc. and using machine learning techniques to figure out how these features describe the, say, the road vs the tunnel (in your example).
As you are asking about the former, I'm going to focus on that here. If you'd like to know more about the latter have a look around the Internet, StackOverflow, or post specific questions you have.
So, for the 'brute force image analysis' approach, your first step would probably be to preprocess the image as you need;
Consider color normalization if you are going to analyze color later, this will help make your algorithm robust to lighting differences in your garage vs the event studio. It'll also improve robustness to camera collaboration differences, though the organization hosting the competition provide specs for the camera they will use (don't ignore this bit of info).
Consider blurring the image to reduce noise if you're less interested in pixel by pixel values (eg edges) and more interested in larger structures (eg gradients).
Consider sharpening the image for the opposite reasons of blurring.
Do a bit of research on image preprocessing. It's definitely an explored topic but hardly 'solved' (whatever that would mean). There are lots of things to try at this stage and, of course, crap in => crap out.
After that you'll want to generate some 'features'..
As you mentioned, edges seem like an appropriate feature space for this problem. Don't forget that there are many other great edge detection algorithms out there other than Canny (see Prewitt, Sobel, etc.) After applying the edge detection algorithm, though, you still just have pixel data. To get to features you'll want probably want to extract lines from the edges. This is where the Hough transform space will come in handy.
(Also, as an idea, you can think about colorspace in concert with the edge detectors. By that, I mean, edge detectors usually work in the B&W colorspace, but rather than converting your image to grayscale you could convert it to an appropriate colorspace and just use a single channel. For example, if the game board is red and the lines on the crosswalk are blue, convert the image to HSV and grab the hue channel as input for the edge detector. You'll likely get better contrast between the regions than just grayscale. For bright vs. dull use the value channel, for yellow vs. blue use the Opponent colorspace, etc.)
You can also look at points. Algorithms such as the Harris corner detector or the Laplacian of Gaussian (LOG) will extract 'key points' (with a different definition for each algorithm but generally reproducible).
There are many other feature spaces to explore, don't stop here.
Now, this is where the brute force part comes in..
The first thing that comes to mind is parallel lines. Even in a curve, the edges of the lines are 'roughly' parallel. You could easily develop an algorithm to find the track in your game by finding lines which are each roughly parallel to their neighbors. Note that line detectors like the Hough transform are usually applied such that they find 'peaks', or overrepresented angles in the dataset. Thus, if you generate a Hough transform for the whole image, you'll be hard pressed to find any of the lines you want. Instead, you'll probably want to use a sliding window to examine each area individually.
Specifically speaking to the curved areas, you can use the Hough transform to detect circles and elipses quite easily. You could apply a heuristic like: two concentric semi-circles with a given difference in radius (~250 in your problem) would indicate a road.
If you're using points/corners you can try to come up with an algorithm to connect the corners of one line to the next. You can put a limit on the distance and degree in rotation from one corner to the next that will permit rounded turns but prohibit impossible paths. This could elucidate the edges of the road while being robust to turns.
You can probably start to see now why these hard coded algorithms start off simple but become tedious to tweak and often don't have great results. Furthermore, they tend to rigid and inapplicable to other, even similar, problems.
All that said.. you're talking about solving a problem that doesn't have an out of the box solution. Thinking about the solution is half the fun, and half the challenge. Everything I've described here is basic image analysis, computer vision, and problem solving. Start reading some papers on these topics and the ideas will come quickly. Good luck in the competition.

circular objects detection

please suggest me some algorithms for circular object detection,
for example a CD is placed under the book, and some part of it can be seen.
So the algorithm should be able to detect a circle from the part of the CD.
Or it can be a plates placed one above another and so on.
I tried opencv's Hough transform, but it do not always detects all of the circles and even sometimes detects false circels.
Hough Transform is still your best bet. Implement it yourself, it's really easy, then you will have better control over the hidden parameters (there always are some) and understanding of their influence. The errors you describe are usually solvable with some tweaking of all the constants and preprocessing. The explanation here is pretty good.
Also, make sure you place reasonable limits on the circle radius, otherwise you will detect both very small objects and large straight lines as circles. Start with simple cases (little noise, only a few circles, large part of each circle visible) and then move slowly towards your goal.

Resources