1 | initial version |
import cv2
import numpy as np
img = cv2.imread("im/kasten.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.asarray(gray < 127, dtype=np.uint8) # need to cast back to uint8
## careful, api change !
## 3.0
_, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
## 2.4
#contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours,-1, (0,0,255), 2)
for i in range(len(contours)):
print "Contour ", i , "points", contours[i]
print " - Contour ", i, "area :", cv2.contourArea(contours[i])
print
cv2.imshow('contours', img)
cv2.waitKey()
2 | No.2 Revision |
import cv2
import numpy as np
img = cv2.imread("im/kasten.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.asarray(gray < 127, dtype=np.uint8) # need to cast back to uint8
## careful, api change !
## 3.0
_, dead_img, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
## 2.4
#contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours,-1, (0,0,255), 2)
for i in range(len(contours)):
print "Contour ", i , "points", contours[i]
print " - Contour ", i, "area :", cv2.contourArea(contours[i])
print
cv2.imshow('contours', img)
cv2.waitKey()