Skip to content

Commit 23dc47d

Browse files
Added README.md
1 parent cb694d7 commit 23dc47d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Contour-Detection/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Contour Detection
2+
3+
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.
4+
5+
- For better accuracy, use binary images. So before finding contours, apply threshold or canny edge detection.
6+
7+
- Since OpenCV 3.2, `findContours()` no longer modifies the source image but returns a modified image as the first of three return parameters.
8+
9+
- In OpenCV, finding contours is like finding white object from black background. So remember, object to be found should be white and background should be black.
10+
11+
</br>
12+
13+
Let's see how to find contours of a binary image:
14+
15+
```
16+
def getContours(img,imgContour):
17+
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
18+
```
19+
20+
See, there are three arguments in `cv.findContours()` function, first one is source image, second is contour retrieval mode, third is contour approximation method. And it outputs a modified image, the contours and hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.
21+
22+
</br>
23+
24+
## How to draw the contours?
25+
26+
To draw the contours, `cv.drawContours` function is used. It can also be used to draw any shape provided you have its boundary points.
27+
28+
Its first argument is source image, second argument is the contours which should be passed as a Python list, third argument is index of contours (useful when drawing individual contour. To draw all contours, pass -1) and remaining arguments are color, thickness etc.
29+
30+
- To draw all the contours in an image:
31+
```
32+
cv.drawContours(img, contours, -1, (0,255,0), 3)
33+
```
34+
35+
- To draw an individual contour, say 4th contour:
36+
```
37+
cv.drawContours(img, contours, 3, (0,255,0), 3)
38+
```
39+
40+
</br>
41+
42+
## Contour Approximation Method
43+
44+
I have used this method in `getContours()` function.
45+
What does it denote actually?
46+
47+
We know that contours are the boundaries of a shape with same intensity. It stores the (x,y) coordinates of the boundary of a shape. But does it store all the coordinates ? That is specified by this contour approximation method.
48+
49+
If you pass `cv.CHAIN_APPROX_NONE`, all the boundary points are stored. But actually do we need all the points? For eg, you found the contour of a straight line. Do you need all the points on the line to represent that line? No, we need just two end points of that line. This is what `cv.CHAIN_APPROX_SIMPLE` does. It removes all redundant points and compresses the contour, thereby saving memory.
50+
51+
Below image of a rectangle demonstrate this technique. Just draw a circle on all the coordinates in the contour array (drawn in blue color). First image shows points I got with `cv.CHAIN_APPROX_NONE` (734 points) and second image shows the one with `cv.CHAIN_APPROX_SIMPLE` (only 4 points). See, how much memory it saves!!!
52+
53+
</br>
54+
55+
![contour](https://user-images.githubusercontent.com/73488906/111815073-40cbc200-8901-11eb-82ac-a6d21000ae48.jpg)
56+
57+
</br>
58+
59+
Note: In this script, I have used `cv2.CHAIN_APPROX_NONE`.

0 commit comments

Comments
 (0)