Skip to content

Commit a347569

Browse files
cclaussalalek
authored andcommitted
Merge pull request opencv#8150 from cclauss/patch-1
cv2.findContours() no longer modifies source image (opencv#8150) * cv2.findContours() no longer modifies source image Since OpenCV 3.2, cv2.findContours() no longer modifies the source image but returns a modified image as the first of three return parameters. ??? Do I have that correct ??? Also fixed up the code blocks to be valid markdown. * ```python --> @code{.py} Enables syntax highlighting in docs.
1 parent 7c3b415 commit a347569

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

doc/py_tutorials/py_imgproc/py_contours/py_contours_begin/py_contours_begin.markdown

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ detection and recognition.
1717

1818
- For better accuracy, use binary images. So before finding contours, apply threshold or canny
1919
edge detection.
20-
- findContours function modifies the source image. So if you want source image even after
21-
finding contours, already store it to some other variables.
20+
- Since OpenCV 3.2, findContours() no longer modifies the source image but returns a modified image as the first of three return parameters.
2221
- In OpenCV, finding contours is like finding white object from black background. So remember,
2322
object to be found should be white and background should be black.
2423

@@ -28,12 +27,12 @@ import numpy as np
2827
import cv2
2928

3029
im = cv2.imread('test.jpg')
31-
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
32-
ret,thresh = cv2.threshold(imgray,127,255,0)
33-
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
30+
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
31+
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
32+
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
3433
@endcode
3534
See, there are three arguments in **cv2.findContours()** function, first one is source image, second
36-
is contour retrieval mode, third is contour approximation method. And it outputs the contours and
35+
is contour retrieval mode, third is contour approximation method. And it outputs a modified image, the contours and
3736
hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a
3837
Numpy array of (x,y) coordinates of boundary points of the object.
3938

@@ -49,15 +48,15 @@ contours which should be passed as a Python list, third argument is index of con
4948
drawing individual contour. To draw all contours, pass -1) and remaining arguments are color,
5049
thickness etc.
5150

52-
To draw all the contours in an image:
51+
* To draw all the contours in an image:
5352
@code{.py}
5453
cv2.drawContours(img, contours, -1, (0,255,0), 3)
5554
@endcode
56-
To draw an individual contour, say 4th contour:
55+
* To draw an individual contour, say 4th contour:
5756
@code{.py}
5857
cv2.drawContours(img, contours, 3, (0,255,0), 3)
5958
@endcode
60-
But most of the time, below method will be useful:
59+
* But most of the time, below method will be useful:
6160
@code{.py}
6261
cnt = contours[4]
6362
cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

0 commit comments

Comments
 (0)