14024411
(Martijn Peterse)
1
import cv2
import numpy as np
from matplotlib import pyplot as plt
frame = cv2.imread('Dice.jpg')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_,thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
lower_color = np.array([0,0,255])
upper_color = np.array([255,255,255])
mask = cv2.inRange(frame, lower_color, upper_color)
res = cv2.bitwise_and(frame,frame, mask = mask)
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_list = []
for contour in contours:
area = cv2.contourArea(contour)
if area > 2500:
contour_list.append(contour)
cv2.drawContours(frame, contour_list, -1, (0, 255, 0), 2)
cv2.imshow('thresh1', thresh1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Mathijs
(Mathijs van der Vegt)
2
Python contours OpenCV 3
import cv2
import numpy as np
from matplotlib import pyplot as plt
im = cv2.imread('Dice.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,200,255,0)
kernel = np.ones((5,5),np.uint8)
thresh = cv2.dilate(thresh,kernel,iterations = 2)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,255), 3)
cv2.imshow('Image', im)
cv2.waitKey(0)
cv2.destroyAllWindows()
Contour functions for reference