How to find contours in an image in OpenCV? - c++

I need to find all contours in an image. I know the whole findcontours () and drawContours () thing, but its using the Canny edge detector that I am having trouble with. To use find contours, you either need to use canny edge detection or threshold the image. I cannot threshold the image because this would result in several edges getting blurred out ("merging" of the edges). So I decided to use Canny Edge detection. However, when I do use it instead of getting perfect edges, I get a variety of lines with gaps in them. This prevents me from getting good contours For example instead of getting the edges of a square, I would get 4 separate lines separated by small gaps resulting in me getting 4 contours instead of one. I tried dilating, opening, closing, Gaussian blurring and basically every morphological operator, but none of these are doing the job. Some do not merge the lines, while some merge the lines with non-relevant lines too. So I was wondering does anyone have a solution on how I can get actual contours from Canny Edge detection, or if not does someone have any alternatives to get all the contours from an image?

make blob, then contours come with it. :)
http://code.google.com/p/cvblob/

Related

Is there any way to "approximate" the rest of a circle in OpenCV?

I am trying to detect circles in some pictures but the circles aren't always perfect. This makes houghCircles very impractical to use because it seems to only support almost perfect circles. So now i am searching for a way to kind of "fix" my circles.
for reproduction purposes:
First i threshold the picture to get a binary picutre:
cv::threshold(input, output, threshvalue, 1, cv::THRESH_BINARY_INV);
After thresholding i detect the Hough Circles with :
std::vector<cv::Vec3f> circles;
HoughCircles(binarypicture,circles,cv::HOUGH_GRADIENT,1,100,30,26,45);

Detect edge from binary image using Canny edge detection from OpenCV

I'm trying to extract the contour of some traffic signs. The images pass through several steps in order to get a good segmented image. In the final step before applying Canny method I get this image:
After applying Canny I get this image:
The algorithm performs as expected except the top left image for which returns an open contour.
So, my question is, is there any way to use Canny in a manner that the top left image returns an closed contour?
If you already have a binary image, Canny does not give much more information. Canny is useful for finding edges in grayscale images (it decides if a gradient is an edge or not), if the image is black and white, Canny will return the edges that you already have,
I think that you need to use findContours(), which depending on the parameters will return only white or black objects, with or without the holes. It also handles the edges of the image.
Image boundary conditions are problematic in most cases. In this particular case, I would try padding the original image with some extra pixels around the image. Depending on how you add those padding pixels, you should get a complete contour around the signs.
You can use Morphological Transformations before and after applying Canny Edge Detection to manipulate the result.

opencv: How to detect rectangle with the not-in-order points in contours

In OpenCV we can use the approxPolyDP to find the contours of an object. However, sometimes the output contours could be quite different when there are holes/blur in between the lines. For example, the actual object of the following two graphs is a rectangle but the output of approxPolyDP generates the contours that are not-in-order. Is there any well-known algorithm that can process the following points and detect a rectangular shape? If not, what is the best approach to deal with this situation?

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

Is there a c/c++ function out there to detect circles in the edge map of an image

I have already tried OpenCV's HoughCircles (which takes grayscale images as input), which is not giving very favorable results. I want to findthe circles in the edge image because I could clean up the unnecessary edges thus reducing the scope for any confusion.
So is there a function that will find circles from the canny edges of the image?
Thanks.