a. Laplace algorithm
I’m not going to explain how works the Laplace algorithm, but the only thing you need to know is that this algorithm is the first step for line and edge detection. This is also used by more complex algorithm included into OpenCV.
The following example show how to apply the Laplace algorithm on both a gray picture and a colour picture. As we can see lines and edges start appearing on resulting images.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Additional informations:
- The aperture size used is the default value. This is the kernel size, it is better to keep this value unless you know what you are doing.
- To apply Laplace on a colour image we should split all the channels and then merge it back.
- As we can see on the results the difference between the gray image and the colour image is not important. So it is more interesting to work on grayscale picture than colour images.
Original picture:
Laplace gray image:
Laplace colour image:
b. Sobel
Sobel is a well-known algorithm used for contour detection. For more information about this algorithm check the wikpedia page. The small example below use the Sobel algorithm to put in evidence the contours of the build which works quite well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
c. Yet another algorithm
The last basic function that will be presented for basic edge/contours detection is the cv.MorphologyEx. The documentation available here explain how works every arguments but the one we are interested in here is CV_MOP_GRADIENT that do dilate and substract the result to an erode. Apply this filtersin this specific order has to effect to release all the contours and edges on a picture. This is based on the fact that comparison of an image and an eroded will mostly differ at edges location (where the intensity of neighboor vary more). As argument we can also provide a structuring element like a cross a diamond on which apply filters and also the number of iterations. More iterations can give more precise contours but can also erase some.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
d. Line detection with Canny
Canny is an algorithm made for edge detection. This is the base algorithm for any line edge or contour detection for his accuracy and his ease to use. The example presented below will show how to detect lines into an image with the canny algorithm. Note that the canny algoirthm use the sobel algorithm in the background. To detect lines on the image we will use the cv.HoughLines2 that do the job of find lines from a “cannied” image. The example show the result using the standard HoughLines and the probabilistic way. Take a look at the documentation for more details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Original image:
Canny image:
Standard HoughLines:
Probablistic HoughLines:
e. Contours detection
To do contours detection OpenCV provide a function called FindContours which intent to find contours in the image. Of course to some treatment should be applied to the picture in order to get a good contours detection. In the example below we first use the MorphologyEx function with CV_MOP_OPEN method and CV_MOP_CLOSE to release contours. Then we apply the FindContours function to find contours and print them on the colour image even though we work on a grayscale version of the image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Additional informations:
- The image is first converted in gray because we used to work on grayscale picture
- A simple threshold is applied to roughly get contours.
- The OPEN and CLOSE trick is applied to get more smooth contours
- The FindContours function is called and a list of contours is retrieved
- Contours a finaly drawn on the colour as if we were working on it.
Original image:
After the first threshold:
After MorphologyEx filters and the second Threshold:
Contours drawn:
f. Edges detection
The last feature we are interested in is the edge detection that will be put in action with the Harris algorithm. For more technicals details about this algorithm check the wikipedia page at this address. In this example we will apply the cv.CornerHarris to the image which return roughly the corners. Then various filters are applied to reduce raw corners to keep only on pixel for each corner from which we will retrieve the coordinates and then draw them on the original image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
After Harris Corner Detection:
Single points Corners:
Corners drawn: