Skip to Content
OpenCV 3 Computer Vision with Python Cookbook
book

OpenCV 3 Computer Vision with Python Cookbook

by Aleksei Spizhevoi, Aleksandr Rybnikov
March 2018
Beginner to intermediate
306 pages
9h 54m
English
Packt Publishing
Content preview from OpenCV 3 Computer Vision with Python Cookbook

How to do it...

The steps for this recipe are as follows:

  1. First, load an image and make its copy:
import cv2, numpy as npimage = cv2.imread('../data/Lena.png')image_to_show = np.copy(image)
  1. Now, define some variables to store the mouse state:
mouse_pressed = Falses_x = s_y = e_x = e_y = -1
  1. Let's implement a handler for mouse events. This should be a function that takes four arguments, as follows:
def mouse_callback(event, x, y, flags, param):    global image_to_show, s_x, s_y, e_x, e_y, mouse_pressed    if event == cv2.EVENT_LBUTTONDOWN:        mouse_pressed = True        s_x, s_y = x, y        image_to_show = np.copy(image)    elif event == cv2.EVENT_MOUSEMOVE:        if mouse_pressed:            image_to_show = np.copy(image)            cv2.rectangle(image_to_show, (s_x, s_y), (x, y), (0, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning OpenCV 3 Computer Vision with Python (Update)

Learning OpenCV 3 Computer Vision with Python (Update)

Joe Minichino, Joseph Howse
OpenCV 4 with Python Blueprints - Second Edition

OpenCV 4 with Python Blueprints - Second Edition

Dr. Menua Gevorgyan, Michael Beyeler (USD), Arsen Mamikonyan, Michael Beyeler

Publisher Resources

ISBN: 9781788474443Supplemental Content