Drawing shapes¶

OpenCV has different cartoon functions to draw:

  • lines
  • circle
  • rectangle
  • ellipse
  • text

Using Numpy¶

Numpy is a very powerful math module for dealing with multi-dimensional information such every bit vectors and images. The OpenCV images are represented every bit Numpy arrays. At the get-go of a programme we import both:

                                    import                  cv2                  as                  cv                  import                  numpy                  every bit                  np                

To create an empty color paradigm nosotros create a 3D array of zeroes:

                                    img                  =                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  600                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  imshow                  (                  'RGB'                  ,                  img                  )                

When zooming, we can meet the iii color components.

../_images/draw1_rgb.png

To create an empty gray-scale image nosotros create a 2nd array of zeroes:

                                    gray_img                  =                  np                  .                  zeros                  ((                  100                  ,                  600                  ),                  np                  .                  uint8                  )                  cv                  .                  imshow                  (                  'Greyness'                  ,                  gray_img                  )                

The grayscale values for each pixel go from 0 to 255. In a black image all pixel values are 0.

../_images/draw1_gray.png

                                    import                  cv2                  as                  cv                  import                  numpy                  every bit                  np                  img                  =                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  three                  ),                  np                  .                  uint8                  )                  cv                  .                  imshow                  (                  'RGB'                  ,                  img                  )                  gray_img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ),                  np                  .                  uint8                  )                  cv                  .                  imshow                  (                  'Greyness'                  ,                  gray_img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

draw1.py

Define colors¶

Colors are defined by three base colors: Blue, Light-green and Carmine. All three put to zero gives black, all three at the maximum gives white:

                                    BLACK                  =                  (                  0                  ,                  0                  ,                  0                  )                  WHITE                  =                  (                  255                  ,                  255                  ,                  255                  )                

Different from the more common RGB ordering, OpenCV uses the ordering BGR:

                                    RED                  =                  (                  0                  ,                  0                  ,                  255                  )                  GREEN                  =                  (                  0                  ,                  255                  ,                  0                  )                  Blueish                  =                  (                  255                  ,                  0                  ,                  0                  )                

Mixing color components results in more colors:

                                    CYAN                  =                  (                  255                  ,                  255                  ,                  0                  )                  MAGENTA                  =                  (                  255                  ,                  0                  ,                  255                  )                  YELLOW                  =                  (                  0                  ,                  255                  ,                  255                  )                

Depict a line¶

The office cv.line() adds a line to an image:

                                    cv                  .                  line                  (                  epitome                  ,                  p0                  ,                  p1                  ,                  color                  ,                  thickness                  )                
  • prototype where the line is added
  • first point p0
  • end bespeak p1
  • line color
  • line thickness

Lets define iii points:

                                    p0                  =                  10                  ,                  10                  p1                  =                  300                  ,                  90                  p2                  =                  500                  ,                  10                

Now nosotros can draw 2 colored lines:

                                    cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Carmine                  ,                  2                  )                  cv                  .                  line                  (                  img                  ,                  p1                  ,                  p2                  ,                  Yellow                  ,                  5                  )                

../_images/line1_rgb.png

If the image is a gray-scale prototype, instead of the color triplet, a grayscale value from 0 (black) to 255 (white) is used:

                                    cv                  .                  line                  (                  gray_img                  ,                  p0                  ,                  p1                  ,                  100                  ,                  ii                  )                  cv                  .                  line                  (                  gray_img                  ,                  p1                  ,                  p2                  ,                  255                  ,                  5                  )                

../_images/line1_gray.png

                                    import                  cv2                  as                  cv                  import                  numpy                  every bit                  np                  RED                  =                  (                  0                  ,                  0                  ,                  255                  )                  Yellow                  =                  (                  0                  ,                  255                  ,                  255                  )                  p0                  ,                  p1                  ,                  p2                  =                  (                  x                  ,                  10                  ),                  (                  300                  ,                  xc                  ),                  (                  400                  ,                  10                  )                  img                  =                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  iii                  ),                  np                  .                  uint8                  )                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  RED                  ,                  2                  )                  cv                  .                  line                  (                  img                  ,                  p1                  ,                  p2                  ,                  Xanthous                  ,                  five                  )                  cv                  .                  imshow                  (                  'RGB'                  ,                  img                  )                  gray_img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ),                  np                  .                  uint8                  )                  cv                  .                  line                  (                  gray_img                  ,                  p0                  ,                  p1                  ,                  100                  ,                  ii                  )                  cv                  .                  line                  (                  gray_img                  ,                  p1                  ,                  p2                  ,                  255                  ,                  5                  )                  cv                  .                  imshow                  (                  'Gray'                  ,                  gray_img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line1.py

Select thickness with a trackbar¶

We can use a trackbar to set the thickness of the line. The trackbar callback function has ane statement - the line thickness. Trackbar arguments are e'er integer and they always start at 0. Even so, for lines the smallest line thickness is 1. Therefore we utilise the max function to make the thickness at least ane. To have a numeric feedback, nosotros display the thickness value likewise in the overlay:

                                    def                  trackbar                  (                  x                  ):                  x                  =                  max                  (                  1                  ,                  x                  )                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'thickness=                  {x}                  '                  )                

Next we have to redraw the line. Nosotros get-go past resetting the image to 0. And so we describe the line and display the new image:

                                    img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  RED                  ,                  x                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                

../_images/line2.png

                                    # Select line thickness with a trackbar                  import                  cv2                  every bit                  cv                  import                  numpy                  as                  np                  Blood-red                  =                  (                  0                  ,                  0                  ,                  255                  )                  p0                  ,                  p1                  =                  (                  100                  ,                  xxx                  ),                  (                  400                  ,                  90                  )                  def                  trackbar                  (                  x                  ):                  10                  =                  max                  (                  1                  ,                  10                  )                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'thickness=                  {x}                  '                  )                  img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Scarlet                  ,                  10                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  RED                  ,                  2                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  createTrackbar                  (                  'thickness'                  ,                  'window'                  ,                  2                  ,                  20                  ,                  trackbar                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line2.py

Select color with a trackbar¶

We tin use a trackbar to set the line color. The trackbar is used to select an index into a color listing which we define with 7 colors:

                                    colors                  =                  (                  Blood-red                  ,                  Greenish                  ,                  BLUE                  ,                  CYAN                  ,                  MAGENTA                  ,                  Xanthous                  ,                  WHITE                  )                

The trackbar is defined to return an integer value from 0 to six:

                                    cv                  .                  createTrackbar                  (                  'color'                  ,                  'window'                  ,                  0                  ,                  6                  ,                  trackbar                  )                

Inside the trackbar callback function nosotros apply the index to look up the color. To requite numeric feedback we brandish the colour RGB value in the overlay:

                                    def                  trackbar                  (                  x                  ):                  colour                  =                  colors                  [                  ten                  ]                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'colour=                  {color}                  '                  )                

../_images/line3.png

                                    # Select line color with a trackbar                  import                  cv2                  as                  cv                  import                  numpy                  as                  np                  Reddish                  =                  (                  0                  ,                  0                  ,                  255                  )                  GREEN                  =                  (                  0                  ,                  255                  ,                  0                  )                  BLUE                  =                  (                  255                  ,                  0                  ,                  0                  )                  CYAN                  =                  (                  255                  ,                  255                  ,                  0                  )                  MAGENTA                  =                  (                  255                  ,                  0                  ,                  255                  )                  YELLOW                  =                  (                  0                  ,                  255                  ,                  255                  )                  WHITE                  =                  (                  255                  ,                  255                  ,                  255                  )                  colors                  =                  (                  RED                  ,                  Light-green                  ,                  BLUE                  ,                  CYAN                  ,                  MAGENTA                  ,                  Yellow                  ,                  WHITE                  )                  p0                  ,                  p1                  =                  (                  100                  ,                  30                  ),                  (                  400                  ,                  ninety                  )                  def                  trackbar                  (                  x                  ):                  colour                  =                  colors                  [                  x                  ]                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'color=                  {color}                  '                  )                  img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  color                  ,                  10                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Ruddy                  ,                  ten                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  createTrackbar                  (                  'colour'                  ,                  'window'                  ,                  0                  ,                  six                  ,                  trackbar                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line3.py

Select stop point with the mouse¶

Let's apply the mouse for selecting the stop betoken of the line. The mouse callback function has the x and y coordinates every bit arguments. Nosotros are just interesed in the mouse movement when a button is pressed (flags==1). The current mouse coordinates volition be the new end point for the line. We brandish this coordinates in the overlay:

                                    def                  mouse                  (                  outcome                  ,                  10                  ,                  y                  ,                  flags                  ,                  param                  ):                  if                  flags                  ==                  one                  :                  p1                  =                  x                  ,                  y                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'p1=(                  {ten}                  ,                                    {y}                  )'                  )                

../_images/line4.png

                                    # Select cease point with the mouse                  import                  cv2                  as                  cv                  import                  numpy                  as                  np                  GREEN                  =                  (                  0                  ,                  255                  ,                  0                  )                  p0                  ,                  p1                  =                  (                  100                  ,                  30                  ),                  (                  400                  ,                  xc                  )                  def                  mouse                  (                  event                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  if                  flags                  ==                  1                  :                  p1                  =                  x                  ,                  y                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'p1=(                  {x}                  ,                                    {y}                  )'                  )                  img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  GREEN                  ,                  ten                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  GREEN                  ,                  10                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  setMouseCallback                  (                  'window'                  ,                  mouse                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line4.py

Draw a complete line¶

Let's now draw a complete line with the mouse. At present nosotros need to distinguish betwixt mouse down, mouse motion and mouse up events. When the mouse is down, we start a new line and set both points p0 and p1 to the current mouse position:

                                    if                  event                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  p0                  =                  x                  ,                  y                  p1                  =                  10                  ,                  y                

If the mouse moves (with the button pressed) or if the mouse button goes up, nosotros only ready p1 to the new mouse position:

                                    elif                  event                  ==                  cv                  .                  EVENT_MOUSEMOVE                  and                  flags                  ==                  1                  :                  p1                  =                  10                  ,                  y                  elif                  issue                  ==                  cv                  .                  EVENT_LBUTTONUP                  :                  p1                  =                  x                  ,                  y                

At the cease of the mouse callback role nosotros reset the epitome to nil (black), draw the line, display the new paradigm and show the 2 points in the overlay:

                                    img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  RED                  ,                  x                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'p0=                  {p0}                  , p1=                  {p1}                  '                  )                

../_images/line5.png

                                    # Depict a complete line with the mouse                  import                  cv2                  as                  cv                  import                  numpy                  as                  np                  Crimson                  =                  (                  0                  ,                  0                  ,                  255                  )                  p0                  ,                  p1                  =                  (                  100                  ,                  30                  ),                  (                  400                  ,                  ninety                  )                  def                  mouse                  (                  result                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  global                  p0                  ,                  p1                  if                  event                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  p0                  =                  x                  ,                  y                  p1                  =                  x                  ,                  y                  elif                  event                  ==                  cv                  .                  EVENT_MOUSEMOVE                  and                  flags                  ==                  i                  :                  p1                  =                  x                  ,                  y                  elif                  event                  ==                  cv                  .                  EVENT_LBUTTONUP                  :                  p1                  =                  x                  ,                  y                  img                  [:]                  =                  0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Scarlet                  ,                  x                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'p0=                  {p0}                  , p1=                  {p1}                  '                  )                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  setMouseCallback                  (                  'window'                  ,                  mouse                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line5.py

Draw multiple lines¶

How do we do to draw multiple lines to an image? Kickoff we need to accept a temporary copy img0 which contains the lines of the previous stage of the drawing:

                                    img0                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  img                  =                  img0                  .                  re-create                  ()                

When the mouse push button is downward, we set the two points p0 and p1 to the current mouse position:

                                    if                  effect                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  p0                  =                  x                  ,                  y                  p1                  =                  10                  ,                  y                

When the mouse moves, we reset the current image to the previous image img0 and draw a blue line of thickness ii:

                                    elif                  event                  ==                  cv                  .                  EVENT_MOUSEMOVE                  and                  flags                  ==                  1                  :                  p1                  =                  10                  ,                  y                  img                  [:]                  =                  img0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  BLUE                  ,                  two                  )                

When the mouse goes up, we reset the image to the previous image img0 , depict a red line of thickness 4, and save this new paradigm every bit img0 :

                                    elif                  consequence                  ==                  cv                  .                  EVENT_LBUTTONUP                  :                  img                  [:]                  =                  img0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  RED                  ,                  4                  )                  img0                  [:]                  =                  img                

../_images/line6.png

                                    # Draw multiple lines with the mouse                  import                  cv2                  as                  cv                  import                  numpy                  equally                  np                  Ruby                  =                  (                  0                  ,                  0                  ,                  255                  )                  BLUE                  =                  (                  255                  ,                  0                  ,                  0                  )                  p0                  ,                  p1                  =                  (                  100                  ,                  30                  ),                  (                  400                  ,                  xc                  )                  def                  mouse                  (                  consequence                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  global                  p0                  ,                  p1                  if                  issue                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  p0                  =                  x                  ,                  y                  p1                  =                  ten                  ,                  y                  elif                  event                  ==                  cv                  .                  EVENT_MOUSEMOVE                  and                  flags                  ==                  1                  :                  p1                  =                  x                  ,                  y                  img                  [:]                  =                  img0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Blue                  ,                  2                  )                  elif                  event                  ==                  cv                  .                  EVENT_LBUTTONUP                  :                  img                  [:]                  =                  img0                  cv                  .                  line                  (                  img                  ,                  p0                  ,                  p1                  ,                  Scarlet                  ,                  four                  )                  img0                  [:]                  =                  img                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  displayOverlay                  (                  'window'                  ,                  f                  'p0=                  {p0}                  , p1=                  {p1}                  '                  )                  img0                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  img                  =                  img0                  .                  copy                  ()                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  setMouseCallback                  (                  'window'                  ,                  mouse                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

line6.py

Describe a rectangle¶

The function cv.rectangle() adds a rectangle to an epitome:

                                    cv                  .                  rectangle                  (                  prototype                  ,                  p0                  ,                  p1                  ,                  colour                  ,                  thickness                  )                
  • paradigm where the rectangle is added
  • corner bespeak p0
  • corner point p1
  • ouline color
  • line thickness

If the line thickness is negative or cv.FILLED the rectangle is filled:

                                    cv                  .                  rectangle                  (                  img                  ,                  p0                  ,                  p1                  ,                  BLUE                  ,                  2                  )                  cv                  .                  rectangle                  (                  img                  ,                  p2                  ,                  p3                  ,                  Green                  ,                  cv                  .                  FILLED                  )                

../_images/rect1.png

                                    import                  cv2                  every bit                  cv                  import                  numpy                  as                  np                  BLACK                  =                  (                  0                  ,                  0                  ,                  0                  )                  WHITE                  =                  (                  255                  ,                  255                  ,                  255                  )                  RED                  =                  (                  0                  ,                  0                  ,                  255                  )                  Dark-green                  =                  (                  0                  ,                  255                  ,                  0                  )                  BLUE                  =                  (                  255                  ,                  0                  ,                  0                  )                  CYAN                  =                  (                  255                  ,                  255                  ,                  0                  )                  MAGENTA                  =                  (                  255                  ,                  0                  ,                  255                  )                  Xanthous                  =                  (                  0                  ,                  255                  ,                  255                  )                  p0                  =                  100                  ,                  10                  p1                  =                  200                  ,                  90                  p2                  =                  300                  ,                  20                  p3                  =                  450                  ,                  eighty                  img                  =                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  rectangle                  (                  img                  ,                  p0                  ,                  p1                  ,                  Blueish                  ,                  two                  )                  cv                  .                  rectangle                  (                  img                  ,                  p2                  ,                  p3                  ,                  Dark-green                  ,                  cv                  .                  FILLED                  )                  cv                  .                  imshow                  (                  'RGB'                  ,                  img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

rect1.py

Draw multiple rectangles¶

Now we combine thickness and color trackbar as welle as the mouse callback to create multple rectangles.

../_images/rect2.png

                                    import                  cv2                  as                  cv                  import                  numpy                  as                  np                  from                  draw                  import                  *                  def                  draw                  (                  x                  ):                  global                  p0                  ,                  p1                  d                  =                  cv                  .                  getTrackbarPos                  (                  'thickness'                  ,                  'window'                  )                  d                  =                  -                  1                  if                  d                  ==                  0                  else                  d                  i                  =                  cv                  .                  getTrackbarPos                  (                  'colour'                  ,                  'window'                  )                  color                  =                  colors                  [                  i                  ]                  img                  [:]                  =                  img0                  cv                  .                  rectangle                  (                  img                  ,                  p0                  ,                  p1                  ,                  color                  ,                  d                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  text                  =                  f                  'color=                  {colour}                  , thickness=                  {d}                  '                  cv                  .                  displayOverlay                  (                  'window'                  ,                  text                  )                  def                  mouse                  (                  event                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  global                  p0                  ,                  p1                  if                  issue                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  img0                  [:]                  =                  img                  p0                  =                  x                  ,                  y                  elif                  event                  ==                  cv                  .                  EVENT_MOUSEMOVE                  and                  flags                  ==                  1                  :                  p1                  =                  10                  ,                  y                  elif                  event                  ==                  cv                  .                  EVENT_LBUTTONUP                  :                  p1                  =                  x                  ,                  y                  draw                  (                  0                  )                  cv                  .                  setMouseCallback                  (                  'window'                  ,                  mouse                  )                  cv                  .                  createTrackbar                  (                  'colour'                  ,                  'window'                  ,                  0                  ,                  half dozen                  ,                  depict                  )                  cv                  .                  createTrackbar                  (                  'thickness'                  ,                  'window'                  ,                  0                  ,                  10                  ,                  draw                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

rect2.py

The common code such as color definitions and image cosmos has been placed in a separate file.

                                    import                  numpy                  equally                  np                  import                  cv2                  equally                  cv                  Blackness                  =                  (                  0                  ,                  0                  ,                  0                  )                  WHITE                  =                  (                  255                  ,                  255                  ,                  255                  )                  Reddish                  =                  (                  0                  ,                  0                  ,                  255                  )                  GREEN                  =                  (                  0                  ,                  255                  ,                  0                  )                  BLUE                  =                  (                  255                  ,                  0                  ,                  0                  )                  CYAN                  =                  (                  255                  ,                  255                  ,                  0                  )                  MAGENTA                  =                  (                  255                  ,                  0                  ,                  255                  )                  YELLOW                  =                  (                  0                  ,                  255                  ,                  255                  )                  colors                  =                  (                  Blood-red                  ,                  Light-green                  ,                  BLUE                  ,                  MAGENTA                  ,                  CYAN                  ,                  YELLOW                  ,                  WHITE                  )                  p0                  =                  p1                  =                  0                  ,                  0                  img0                  =                  np                  .                  zeros                  ((                  200                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  img                  =                  img0                  .                  copy                  ()                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                

describe.py

Describe an ellipse¶

The function cv.ellipes() adds an ellipse to an image:

                                    cv                  .                  ellipse                  (                  img                  ,                  center                  ,                  axes                  ,                  angle                  ,                  a0                  ,                  a1                  ,                  colour                  ,                  thickness                  )                
  • epitome where the ellipse is added
  • centre point
  • the 2 axes
  • the axis orientation angle
  • the get-go bending a0
  • the ending angle a1
  • ouline colour
  • line thickness

../_images/draw4.png

                                    import                  cv2                  as                  cv                  import                  numpy                  every bit                  np                  BLUE                  =                  (                  255                  ,                  0                  ,                  0                  )                  center                  =                  200                  ,                  50                  axes                  =                  100                  ,                  30                  angle                  =                  15                  img                  =                  img                  =                  np                  .                  zeros                  ((                  100                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  ellipse                  (                  img                  ,                  center                  ,                  axes                  ,                  angle                  ,                  0                  ,                  360                  ,                  Blueish                  ,                  2                  )                  cv                  .                  imshow                  (                  'RGB'                  ,                  img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

draw4.py

Draw a polygon¶

The polylines function expects a Numpy array for the point list:

                                    pts                  =                  [(                  50                  ,                  fifty                  ),                  (                  300                  ,                  190                  ),                  (                  400                  ,                  ten                  )]                  cv                  .                  polylines                  (                  img                  ,                  np                  .                  array                  ([                  pts                  ]),                  Truthful                  ,                  Ruddy                  ,                  5                  )                

../_images/polygon1.png

                                    import                  cv2                  as                  cv                  import                  numpy                  equally                  np                  Red                  =                  (                  0                  ,                  0                  ,                  255                  )                  pts                  =                  [(                  50                  ,                  50                  ),                  (                  300                  ,                  190                  ),                  (                  400                  ,                  10                  )]                  img                  =                  img                  =                  np                  .                  zeros                  ((                  200                  ,                  500                  ,                  3                  ),                  np                  .                  uint8                  )                  cv                  .                  polylines                  (                  img                  ,                  np                  .                  array                  ([                  pts                  ]),                  True                  ,                  RED                  ,                  5                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

polygon1.py

Depict a filled polygon¶

The polylines function expects a Numpy array for the point list:

                                    pts                  =                  [(                  50                  ,                  50                  ),                  (                  300                  ,                  190                  ),                  (                  400                  ,                  10                  )]                  cv                  .                  polylines                  (                  img                  ,                  np                  .                  array                  ([                  pts                  ]),                  True                  ,                  Scarlet                  ,                  v                  )                

../_images/polygon2.png

                                    import                  cv2                  as                  cv                  import                  numpy                  as                  np                  RED                  =                  (                  0                  ,                  0                  ,                  255                  )                  pts                  =                  [(                  50                  ,                  fifty                  ),                  (                  300                  ,                  190                  ),                  (                  400                  ,                  10                  )]                  img                  =                  img                  =                  np                  .                  zeros                  ((                  200                  ,                  500                  ,                  three                  ),                  np                  .                  uint8                  )                  cv                  .                  fillPoly                  (                  img                  ,                  np                  .                  array                  ([                  pts                  ]),                  RED                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

polygon2.py

Describe a polygon with the mouse¶

Combining the previous techniques, it is rather uncomplicated to draw a polygon only by clicking into the window. First nosotros define an empty list:

Each time we click with the mouse we append a point:

                                    def                  mouse                  (                  upshot                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  if                  event                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  pts                  .                  append                  ((                  x                  ,                  y                  ))                  draw                  (                  0                  )                

../_images/polygon3.png

                                    import                  cv2                  every bit                  cv                  import                  numpy                  as                  np                  from                  draw                  import                  *                  pts                  =                  []                  def                  draw                  (                  x                  ):                  d                  =                  cv                  .                  getTrackbarPos                  (                  'thickness'                  ,                  'window'                  )                  d                  =                  -                  1                  if                  d                  ==                  0                  else                  d                  i                  =                  cv                  .                  getTrackbarPos                  (                  'color'                  ,                  'window'                  )                  colour                  =                  colors                  [                  i                  ]                  img                  [:]                  =                  img0                  cv                  .                  polylines                  (                  img                  ,                  np                  .                  assortment                  ([                  pts                  ]),                  True                  ,                  color                  ,                  d                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  text                  =                  f                  'colour=                  {colour}                  , thickness=                  {d}                  '                  cv                  .                  displayOverlay                  (                  'window'                  ,                  text                  )                  def                  mouse                  (                  event                  ,                  x                  ,                  y                  ,                  flags                  ,                  param                  ):                  if                  effect                  ==                  cv                  .                  EVENT_LBUTTONDOWN                  :                  pts                  .                  append                  ((                  10                  ,                  y                  ))                  draw                  (                  0                  )                  cv                  .                  setMouseCallback                  (                  'window'                  ,                  mouse                  )                  cv                  .                  createTrackbar                  (                  'colour'                  ,                  'window'                  ,                  0                  ,                  6                  ,                  draw                  )                  cv                  .                  createTrackbar                  (                  'thickness'                  ,                  'window'                  ,                  2                  ,                  10                  ,                  draw                  )                  draw                  (                  0                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

polygon3.py

Draw text¶

../_images/text1.png

                                    import                  cv2                  as                  cv                  import                  numpy                  as                  np                  RED                  =                  (                  0                  ,                  0                  ,                  255                  )                  p0                  =                  (                  10                  ,                  100                  )                  font                  =                  cv                  .                  FONT_HERSHEY_SIMPLEX                  img                  =                  np                  .                  zeros                  ((                  200                  ,                  500                  ,                  three                  ),                  np                  .                  uint8                  )                  cv                  .                  putText                  (                  img                  ,                  'OpenCV'                  ,                  p0                  ,                  font                  ,                  iv                  ,                  RED                  ,                  two                  ,                  cv                  .                  LINE_AA                  )                  cv                  .                  imshow                  (                  'window'                  ,                  img                  )                  cv                  .                  waitKey                  (                  0                  )                  cv                  .                  destroyAllWindows                  ()                

text1.py

Reference: https://docs.opencv.org/four.2.0/d6/d6e/group__imgproc__draw.html