How to process the result of OpenCV:: Canny Edge Detection - c++

I try the Canny edge detection algorithm to find the edge of a simple circle.
cv:: Canny()
The function returns a
cv::Mat edge
But I do not know how to use these "edge".
My goal is the draw the "edge" back to the original image and read the information within that edge (in this case, the edge is a circle)
I look through the cv:: read function and only find the drawContour function which is not an edge.
And I also do not how to find the coordinate of the edge so that I can do something about the inner part of the circle edge.
I am new to openCV, any suggestion is appreciated.

edges is array (Mat) of the same size as source picture, and it contains zero pixels and max value (255) pixels at edges that Canny function found
You can emphasize edges at source image (by white color), making bitwise_or operation with edges (in my). Or transform edges to color image to draw color edges. Here is example of using edges as mask.
Edges is raster result. To get set of segments, use findContours function on edges, then you can use drawContours
Note that this information is well-googlable.

You need learn how to traverse a cv::Mat object.
http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way
Besides, I suggest u read first few chapters of the book learning opencv to master the basic usage of this library, now there is a third edition. You can also find many examples in "InstallPath\opencv\sources\samples" and the official tutorial: http://docs.opencv.org/2.4/doc/tutorials/tutorials.html

Related

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.

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.

How to stablize the circle from video stream using opencv?

I've started using OpenCV few days back, My aim is to detect a circle and its centre, I've used hough transform, I'm using a webcam of resolution 640x480, It is working but the circle keeps on changing its position, to better explain it I posted a screen grab on youtube https://www.youtube.com/watch?v=6EGePHkGrok
Here is the code http://pastebin.com/zRG4Yfzy ,yes I know its a bit messy.
First the full video is shown, when the camera stabilizes I press ESC, then the processing begins on the ROI 250x250.
I've added few trackbars to change to parameters of hough transform and amount of blur, changing the blur amount doesn't solve the problem
How to stabilize the circle? Also the camera will not move so no tracking is needed.
Or should I adopt a completely new method of doing this?
According to my understanding I need to apply some sort of filter.
The object has many circular contours, but all have the same centre, so any of the circular contour is detected its fine.
PS:I'm no Image Processing expert, I patched up the code from various sites and books
Hough transforms are known to be error prone.
For your case, you may find contours in your image and filter them by their circularity.
1- grayscale
2- low pass filter (gaussian blur)
3- canny edge detection
4- find contours and list their areas.
5- draw min enclosing circles to your contours.
6- select the contour which has min enclosing circle area closest to contour area.
7- find center of mass of the contour using moments F3 type "mass centers"

Extending a contour in OpenCv

i have several contours that consist of several black regions in my image. Directly adjacent to these black regions are some brighter regions that do not belong to my contours. I want to add these brighter regions to my black region and therefor extend my contour in OpenCv.
Is there a convenient way to extend a contour? I thought about looking at intensity change from my gradient-image created with cv::Sobel and extend until the gradient changes again, meaning the intensity of pixel is going back to the neither black nor bright regions of the image.
Thanks!
Here are example images. The first picture shows the raw Image, the second the extracted Contour using Canny & findContours, the last one the Sobel-Gradient intensity Image of the same area.
I want to include the bright boundaries in the first image to the Contour.
Update: Now i've used some morphological operations on the Sobelgradients and added a contour around them (see Image below). Next step could be to find the adjacent pair of purple & red contours, but it seems very much like a waste of procession time to actually have to search for directly adjacent contours. Any better ideas?
Update 2: My solution for now is to search for morphed gradient (red) contours in a bounding box around my (purple) contours and pick the one with correct orientation & size. This works for gradient contours where the morphological operation closes the "rise" and "fall" gradient areas like in Figure 3. But it is still a bad solution for cases in which the lighted area is wider then in the image above. Any idea is still very much appreciated, thanks!
What you're trying to do is find two different features and merge them. It's not terribly difficult but you have to use multiple copies of the image to make it happen.
Make one copy, and threshold it for the dark portion
Make another copy and threshold it for the light portion
Merge both thresholded images into a new image
Apply a morphological operation like opening or closing (depending on how you threshold) This will connect nearby components
Find contours in the resultant image
Use those contours on your original image. This will work since all the images are the same size and all based off of the original.