Commit a3475693 authored by cclauss's avatar cclauss Committed by Alexander Alekhin

Merge pull request #8150 from cclauss/patch-1

cv2.findContours() no longer modifies source image (#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.
parent 7c3b415d
...@@ -17,8 +17,7 @@ detection and recognition. ...@@ -17,8 +17,7 @@ detection and recognition.
- For better accuracy, use binary images. So before finding contours, apply threshold or canny - For better accuracy, use binary images. So before finding contours, apply threshold or canny
edge detection. edge detection.
- findContours function modifies the source image. So if you want source image even after - Since OpenCV 3.2, findContours() no longer modifies the source image but returns a modified image as the first of three return parameters.
finding contours, already store it to some other variables.
- In OpenCV, finding contours is like finding white object from black background. So remember, - 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. object to be found should be white and background should be black.
...@@ -28,12 +27,12 @@ import numpy as np ...@@ -28,12 +27,12 @@ import numpy as np
import cv2 import cv2
im = cv2.imread('test.jpg') im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0) ret, thresh = cv2.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
@endcode @endcode
See, there are three arguments in **cv2.findContours()** function, first one is source image, second See, there are three arguments in **cv2.findContours()** function, first one is source image, second
is contour retrieval mode, third is contour approximation method. And it outputs the contours and 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 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. Numpy array of (x,y) coordinates of boundary points of the object.
...@@ -49,15 +48,15 @@ contours which should be passed as a Python list, third argument is index of con ...@@ -49,15 +48,15 @@ contours which should be passed as a Python list, third argument is index of con
drawing individual contour. To draw all contours, pass -1) and remaining arguments are color, drawing individual contour. To draw all contours, pass -1) and remaining arguments are color,
thickness etc. thickness etc.
To draw all the contours in an image: * To draw all the contours in an image:
@code{.py} @code{.py}
cv2.drawContours(img, contours, -1, (0,255,0), 3) cv2.drawContours(img, contours, -1, (0,255,0), 3)
@endcode @endcode
To draw an individual contour, say 4th contour: * To draw an individual contour, say 4th contour:
@code{.py} @code{.py}
cv2.drawContours(img, contours, 3, (0,255,0), 3) cv2.drawContours(img, contours, 3, (0,255,0), 3)
@endcode @endcode
But most of the time, below method will be useful: * But most of the time, below method will be useful:
@code{.py} @code{.py}
cnt = contours[4] cnt = contours[4]
cv2.drawContours(img, [cnt], 0, (0,255,0), 3) cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment