Update findContours parameter type

This commit is contained in:
Suleyman TURKMEN 2018-09-27 07:42:21 +03:00
parent 29e88e50ff
commit 8eb987e393
17 changed files with 25 additions and 25 deletions

View File

@ -23,7 +23,7 @@ import cv2 as cv
img = cv.imread('star.jpg',0) img = cv.imread('star.jpg',0)
ret,thresh = cv.threshold(img,127,255,0) ret,thresh = cv.threshold(img,127,255,0)
im2,contours,hierarchy = cv.findContours(thresh, 1, 2) contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0] cnt = contours[0]
M = cv.moments(cnt) M = cv.moments(cnt)

View File

@ -17,7 +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.
- Since OpenCV 3.2, findContours() no longer modifies the source image but returns a modified image as the first of three return parameters. - Since OpenCV 3.2, findContours() no longer modifies the source image.
- 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.
@ -29,11 +29,11 @@ import cv2 as cv
im = cv.imread('test.jpg') im = cv.imread('test.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY) imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0) ret, thresh = cv.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
@endcode @endcode
See, there are three arguments in **cv.findContours()** function, first one is source image, second 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 is contour retrieval mode, third is contour approximation method. And it outputs the contours and hierarchy.
hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a 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.
@note We will discuss second and third arguments and about hierarchy in details later. Until then, @note We will discuss second and third arguments and about hierarchy in details later. Until then,

View File

@ -39,7 +39,7 @@ import numpy as np
img = cv.imread('star.jpg') img = cv.imread('star.jpg')
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(img_gray, 127, 255,0) ret,thresh = cv.threshold(img_gray, 127, 255,0)
im2,contours,hierarchy = cv.findContours(thresh,2,1) contours,hierarchy = cv.findContours(thresh,2,1)
cnt = contours[0] cnt = contours[0]
hull = cv.convexHull(cnt,returnPoints = False) hull = cv.convexHull(cnt,returnPoints = False)
@ -93,9 +93,9 @@ img2 = cv.imread('star2.jpg',0)
ret, thresh = cv.threshold(img1, 127, 255,0) ret, thresh = cv.threshold(img1, 127, 255,0)
ret, thresh2 = cv.threshold(img2, 127, 255,0) ret, thresh2 = cv.threshold(img2, 127, 255,0)
im2,contours,hierarchy = cv.findContours(thresh,2,1) contours,hierarchy = cv.findContours(thresh,2,1)
cnt1 = contours[0] cnt1 = contours[0]
im2,contours,hierarchy = cv.findContours(thresh2,2,1) contours,hierarchy = cv.findContours(thresh2,2,1)
cnt2 = contours[0] cnt2 = contours[0]
ret = cv.matchShapes(cnt1,cnt2,1,0.0) ret = cv.matchShapes(cnt1,cnt2,1,0.0)

View File

@ -3885,12 +3885,12 @@ parent, or nested contours, the corresponding elements of hierarchy[i] will be n
contours are extracted from the image ROI and then they should be analyzed in the whole image contours are extracted from the image ROI and then they should be analyzed in the whole image
context. context.
*/ */
CV_EXPORTS_W void findContours( InputOutputArray image, OutputArrayOfArrays contours, CV_EXPORTS_W void findContours( InputArray image, OutputArrayOfArrays contours,
OutputArray hierarchy, int mode, OutputArray hierarchy, int mode,
int method, Point offset = Point()); int method, Point offset = Point());
/** @overload */ /** @overload */
CV_EXPORTS void findContours( InputOutputArray image, OutputArrayOfArrays contours, CV_EXPORTS void findContours( InputArray image, OutputArrayOfArrays contours,
int mode, int method, Point offset = Point()); int mode, int method, Point offset = Point());
/** @example samples/cpp/squares.cpp /** @example samples/cpp/squares.cpp

View File

@ -1874,7 +1874,7 @@ cvFindContours( void* img, CvMemStorage* storage,
return cvFindContours_Impl(img, storage, firstContour, cntHeaderSize, mode, method, offset, 1); return cvFindContours_Impl(img, storage, firstContour, cntHeaderSize, mode, method, offset, 1);
} }
void cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours, void cv::findContours( InputArray _image, OutputArrayOfArrays _contours,
OutputArray _hierarchy, int mode, int method, Point offset ) OutputArray _hierarchy, int mode, int method, Point offset )
{ {
CV_INSTRUMENT_REGION(); CV_INSTRUMENT_REGION();
@ -1939,7 +1939,7 @@ void cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours,
} }
} }
void cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours, void cv::findContours( InputArray _image, OutputArrayOfArrays _contours,
int mode, int method, Point offset) int mode, int method, Point offset)
{ {
CV_INSTRUMENT_REGION(); CV_INSTRUMENT_REGION();

View File

@ -10,8 +10,8 @@ class shape_test(NewOpenCVTests):
a = self.get_sample('samples/data/shape_sample/1.png', cv.IMREAD_GRAYSCALE) a = self.get_sample('samples/data/shape_sample/1.png', cv.IMREAD_GRAYSCALE)
b = self.get_sample('samples/data/shape_sample/2.png', cv.IMREAD_GRAYSCALE) b = self.get_sample('samples/data/shape_sample/2.png', cv.IMREAD_GRAYSCALE)
_, ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
_, cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS)
hd = cv.createHausdorffDistanceExtractor() hd = cv.createHausdorffDistanceExtractor()
sd = cv.createShapeContextDistanceExtractor() sd = cv.createShapeContextDistanceExtractor()

View File

@ -31,7 +31,7 @@ def find_squares(img):
bin = cv.dilate(bin, None) bin = cv.dilate(bin, None)
else: else:
_retval, bin = cv.threshold(gray, thrs, 255, cv.THRESH_BINARY) _retval, bin = cv.threshold(gray, thrs, 255, cv.THRESH_BINARY)
bin, contours, _hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE) contours, _hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
for cnt in contours: for cnt in contours:
cnt_len = cv.arcLength(cnt, True) cnt_len = cv.arcLength(cnt, True)
cnt = cv.approxPolyDP(cnt, 0.02*cnt_len, True) cnt = cv.approxPolyDP(cnt, 0.02*cnt_len, True)

View File

@ -54,7 +54,7 @@ if __name__ == '__main__':
img = make_image() img = make_image()
h, w = img.shape[:2] h, w = img.shape[:2]
_, contours0, hierarchy = cv.findContours( img.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours0, hierarchy = cv.findContours( img.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = [cv.approxPolyDP(cnt, 3, True) for cnt in contours0] contours = [cv.approxPolyDP(cnt, 3, True) for cnt in contours0]
def update(levels): def update(levels):

View File

@ -41,7 +41,7 @@ def main():
bin = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 31, 10) bin = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 31, 10)
bin = cv.medianBlur(bin, 3) bin = cv.medianBlur(bin, 3)
_, contours, heirs = cv.findContours( bin.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE) contours, heirs = cv.findContours( bin.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
try: try:
heirs = heirs[0] heirs = heirs[0]
except: except:

View File

@ -91,7 +91,7 @@ cv.imshow('Peaks', dist)
dist_8u = dist.astype('uint8') dist_8u = dist.astype('uint8')
# Find total markers # Find total markers
_, contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for the watershed algorithm # Create the marker image for the watershed algorithm
markers = np.zeros(dist.shape, dtype=np.int32) markers = np.zeros(dist.shape, dtype=np.int32)

View File

@ -16,7 +16,7 @@ def thresh_callback(val):
## [findContours] ## [findContours]
# Find contours # Find contours
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
## [findContours] ## [findContours]
## [allthework] ## [allthework]

View File

@ -16,7 +16,7 @@ def thresh_callback(val):
## [findContours] ## [findContours]
# Find contours # Find contours
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
## [findContours] ## [findContours]
# Find the rotated rectangles and ellipses for each contour # Find the rotated rectangles and ellipses for each contour

View File

@ -13,7 +13,7 @@ def thresh_callback(val):
canny_output = cv.Canny(src_gray, threshold, threshold * 2) canny_output = cv.Canny(src_gray, threshold, threshold * 2)
# Find contours # Find contours
_, contours, hierarchy = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, hierarchy = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Draw contours # Draw contours
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)

View File

@ -13,7 +13,7 @@ def thresh_callback(val):
canny_output = cv.Canny(src_gray, threshold, threshold * 2) canny_output = cv.Canny(src_gray, threshold, threshold * 2)
# Find contours # Find contours
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Find the convex hull object for each contour # Find the convex hull object for each contour
hull_list = [] hull_list = []

View File

@ -17,7 +17,7 @@ def thresh_callback(val):
## [findContours] ## [findContours]
# Find contours # Find contours
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
## [findContours] ## [findContours]
# Get the moments # Get the moments

View File

@ -21,7 +21,7 @@ for i in range(6):
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3) cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)
# Get the contours # Get the contours
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Calculate the distances to the contour # Calculate the distances to the contour
raw_dist = np.empty(src.shape, dtype=np.float32) raw_dist = np.empty(src.shape, dtype=np.float32)

View File

@ -81,7 +81,7 @@ _, bw = cv.threshold(gray, 50, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
## [contours] ## [contours]
# Find all the contours in the thresholded image # Find all the contours in the thresholded image
_, contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE) contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
for i, c in enumerate(contours): for i, c in enumerate(contours):
# Calculate the area of each contour # Calculate the area of each contour