Contour
Last updated
Was this helpful?
Was this helpful?
$ cv2.drawContours(image, cnts, contourIdx, color, thickness)import numpy as np
import cv2
import imutils
image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
cnts = cv2.findContours(gray.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()
cv2.drawContours(clone, cnts, -1, (0, 255, 0), 2)
cv2.imshow("All Contours", clone)
cv2.waitKey(0)import numpy as np
import cv2
import imutils
image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)
cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()
for c in cnts:
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.circle(clone, (cX, cY), 10, (0, 255, 0), -1)
cv2.imshow("All Contours", clone)
cv2.waitKey(0)import numpy as np
import cv2
import imutils
image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)
cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(clone,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("All Contours", clone)
cv2.waitKey(0)import numpy as np
import cv2
import imutils
image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)
cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()
for c in cnts:
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(clone,[box],0,(0,0,255),2)
cv2.imshow("All Contours", clone)
cv2.waitKey(0)import numpy as np
import cv2
import imutils
image = cv2.imread("/path/your/image.jpg")
image = imutils.resize(image,width = 400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original", image)
(T, threshInv) = cv2.threshold(gray, 65, 255, cv2.THRESH_BINARY)
cnts = cv2.findContours(threshInv.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
clone = image.copy()
for c in cnts:
(x,y),radius = cv2.minEnclosingCircle(c)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(clone,center,radius,(0,255,0),2)
cv2.imshow("All Contours", clone)
cv2.waitKey(0)