How to do it...

  1. Import the modules:
import cv2import numpy as np
  1. Draw a test image:
img = np.zeros((500, 500), np.uint8)cv2.circle(img, (200, 200), 50, 255, 3)cv2.line(img, (100, 400), (400, 350), 255, 3)
  1. Detect lines using the probabilistic Hough transform:
lines = cv2.HoughLinesP(img, 1, np.pi/180, 100, 100, 10)[0]
  1. Detect circles using the Hough transform:
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 15, param1=200, param2=30)[0]
  1. Draw the detected lines and circles:
dbg_img = np.zeros((img.shape[0], img.shape[1], 3), np.uint8) for x1, y1, x2, y2 in lines:    print('Detected line: ({} {}) ({} {})'.format(x1, y1, x2, y2))    cv2.line(dbg_img, (x1, y1), (x2, y2), (0, 255, 0), 2) for c in circles: print('Detected circle: center=({} ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.