Opencv Python

Opencv has a lot of useful functions.

Show image/video stream

1
2
3
while True:
cv2.imshow(img)
cv2.waitKey(1)

Color Space Convert

1
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Histogram and Backprojection

1
2
hist = cv2.calcHist([img], [0, 1], None, [180, 256], [0, 180, 0, 256])
mask = cv2.calcBackProject([target], [0, 1], hist, [0, 180, 0, 256], 1)

Otsu threshold

1
_, mask = cv2.threshold(img[:, :, 1], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

Geometric

Contour of images

1
_, contours, _ = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

Convex Hull and Convex Defect

1
2
hull = cv2.convexHull(contour, returnPoints=False)
defects = cv2.convexityDefects(contour, hull)