projects:robots:autonomy

OpenCV vs matplotlib

data format

  1. both matplotlib and opencv express the image as a numpy array
  2. you can always manipulate the array directly using matrix math
  3. or, both matplotlib and opencv provide methods to do sophisticated operations
  4. there is considerable overlap between the two systems
  5. you can probably do whatever you want with either system
  6. and you can mix and match both systems, so long as you know the differences
  7. in the way they handle the array

color model

  1. by default, matplotlib uses RGB, opencv uses BGR

alpha channel

  1. by default, array shape is (y,x,3)
  2. can be changed to (y,x,4), for RGBA or BGRA
  3. example:
    1. y,x,d = myarray.shape
    2. numpy.dstack(myarray, numpy.zeros(y, x))

display

  1. by default, matplotlib produces a graph
  2. - with x,y,z axis, with tickmarks and scale
  3. - the graph is positioned with margins inside a resizeable window
  4. opencv gives a full-size image in a fixed-size window

animation

  1. both systems provide systems for animation and user-input handling
  2. matplotlib FuncAnimation allows for an incremental blit
  3. matplotlib allows you to change the data of objects already in the plot
  4. opencv requires you to rewrite the whole screen

user input

  1. both systems allow you to wait for a key press
  2. both systems provide an event-handler for keyboard and mouse events

tiling

  1. numpy hstack() and vstack() can be used to tile multiple images into one
  2. in addition, matplotlib uses the Figure→Axes→Plot heirarchy of subplots

how to overlay transparent plot on top of a photo

1. convert plot to image google: convert matplotlib figure to numpy array opencv https: www.autoscripts.net/convert-matplotlib-figure-to-cv2-image /

2. overlay transparent plot on top of image https: docs.opencv.org/3.4/d5/dc4/tutorial_adding_images.html

in cv2, use cv2.inRange() to make a mask use cv2.bitwise_and() to make masked image see ../sk8/visualcortex.py

in matplotlib,
use imshow() twice, where second, top, image has alpha channel
https://stackoverflow.com/questions/49025832/combine-picture-and-plot-with-matplotlib-with-alpha-channel
ax.imshow(bottom, interpolation=None)
ax.imshow(topimg, interpolation=None) # top image must have alpha channel