Created
June 19, 2019 16:38
-
-
Save pknowledge/af2cfb0753abe45c563188794773618f to your computer and use it in GitHub Desktop.
OpenCV Python Tutorial For Beginners - Find and Draw Contours with OpenCV in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import cv2 | |
| img = cv2.imread('baseball.png') | |
| imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| ret, thresh = cv2.threshold(imgray, 127, 255, 0) | |
| contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) | |
| print("Number of contours = " + str(len(contours))) | |
| print(contours[0]) | |
| cv2.drawContours(img, contours, -1, (0, 255, 0), 3) | |
| cv2.drawContours(imgray, contours, -1, (0, 255, 0), 3) | |
| cv2.imshow('Image', img) | |
| cv2.imshow('Image GRAY', imgray) | |
| cv2.waitKey(0) | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment