Wednesday 13 July 2016

TUT7: Basic Drawing - Line

In this tutorial, We are going to see how to draw a line using OpenCV, Python and Numpy.

Today we will learn to use two new functions:
  • np.zeros()
  • cv2.line()
So let's start:


import cv2                                                #Line 1
import numpy as np                                        #Line 2

img=np.zeros((640,480,3), np.uint8)                       #Line 3
cv2.line(img, (0,0), (480,640), (255, 0, 0), 2)           #Line 4

cv2.imshow('img',img)                                     #Line 5

cv2.waitKey(0)                                            #Line 6
cv2.destroyAllWindows()                                   #Line 7


Line 1: Import OpenCV package using "import cv2"

Line 2: Import numpy package using "import numpy as np". Here "np" is just a short name for                        numpy so if want to call a numpy function zeros() we can simply write np.zeros() instead of                numpy.zeros(). It will save your typing in longer codes.

Line 3: Here we have a new function np.zeros() which is the part of numpy package. This function                has the following syntax:


np.zeros((row,col,channel), dtype, order= 'C')
       
           It is used to create an empty black image of specified size(Row x Col). We need a surface to                draw lines, circle etc. and this function provides us that surface by creating an empty black                  surface where we can make drawings.
  •  row,col: specify row and col in terms of number of pixels for eg. (640,480)
  •  channel: specify the type of image. grayscale or color. We pass 3 for color.
  •  dtype(optional): datatype of pixel values. by default np.float64
  •  order(optional): row wise or column wise. by default 'C'           
Line 4: This is the code which actually draws a line on the black surface we made in previous line.
             The syntax is:


cv2.line(img, (start_co-ordinate), (end_co-ordinate), (color), thickness)
             
  •    img is the image that we just made using np.zeros. This specifies that we want to draw on          that image
  •    start_co-ordinate is the starting point of the line for eg. (0,0) i.e the top left of the screen
  •    end_co-ordinate is the end point of the line for eg. (480,640). Remember that 480 is the              column size and 640 is the row size. This is in contrast with what we wrote in np.zeros().
  •    color defines the color of the line you want to draw. For eg. (255,0,0) i.e. BLUE color line
  •    thickness defines the thickness of the line we want to draw. For eg. 2      
Line 5: Display the image using cv.imshow() method.

Line 6: To hold the screen infinitely we pass the argument to waitKey as 0.

Line 7: Closes all the open windows, if any.

OUTPUT:


As you can see, we are now able to draw a line using simple operations.

That's all for this tutorial, and in next tutorial we are going to learn how to draw a circle. 

Any queries, please ask in comments section below.

No comments:

Post a Comment