Showing posts with label draw in openCV. Show all posts
Showing posts with label draw in openCV. Show all posts

Monday, 15 August 2016

TUT10: Drawing TEXT on canvas

Hello guys,
          In this tutorial, we are going to see how can we actually write a text like this one below on a canvas(drawing window).



Here's the code:


import cv2
import numpy as np
img=np.zeros((500,500,3), np.uint8)
font= cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'ankit',(100,400), font, 4, (255,0,255), 2, cv2.LINE_AA)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation

Line1-2: import necessary packages(see previous tutorials for info on them)

Line 3: Create a canvas using zeros function 

Line 4: We need to specify a font we wish to draw in. I encourage you to see list of fonts available in              cv2 module to try with other fonts too.

Line 5: puttext() is the function that takes in various parameters as follows:
           1) canvas 
           2) String which you want to draw
           3) starting co-ordinate of the string
           4) font type which we stored in variable font
           5) font size
           6) color
           7) thickness of font(dont confuse with size)
           8) line type(click to know more)

Line 6: Show the image

Line 7-8: Hold the screen using waitKey(0) and exit all windows after the canvas is closed


Output:


That's it for this tutorial. See you in next tutorial which is going to be little interesting.

Any queries, please ask in the comment section below.

Tuesday, 2 August 2016

TUT9: Drawing a POKER Face

Hello guys,
         
In this tutorial, we are going to see how to make a simple poker face ( Something like this → `_`).

Here's the code:


import cv2
import numpy as np

img=np.zeros((500,500,3), np.uint8)

cv2.circle(img, (100,100), 70, (255,0,255), 3)
cv2.circle(img, (400,100), 70, (255,0,255), 3)
cv2.circle(img, (250,250), 40, (0,255,255), 3)
cv2.line(img, (100,450), (400,450), (255,255,0), 3)

cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Line 1-2: Import necessary libraries cv2 and numpy.

Line 3: Create a black surface using np.zeros(). Here, 500 is the height and width of window                           created which will support 3 color channels i.e. RGB.

Line 4: Draw a circle with center (100,100) and 70px radius. (255, 0, 255) will give pink color and 3                specifies the thickness of the circle. This circle is going to be the left eye of the poker face.

Line 5: Draw another circle with center (400,100) and 70px radius. This will be the right eye of the                 face.

Line 6: Draw another circle with center (250,250) and 40px radius. This will be the nose of the                        face.

Line 7: Draw a straight line from (100,450) to (400,450) co-ordinates.

Line 8: Display image using imshow() function.

Line 9-10: Hold image infinitely using waitKey(0) and close any open windows using                                           destroyAllWindows().


OUTPUT:


This will be generated when you run the code. 

That's it for this tutorial. In the next tutorial, we are going to perform some operations on image.

For any queries, use comment section below.

Sunday, 17 July 2016

TUT8: Basic Drawing - Circle

In this tutorial, we are going to see how can we draw a circle using Python, OpenCV and Numpy.

Code:


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

img=np.zeros((500,500,3), np.uint8)                 #Line 3 

cv2.circle(img, (200,200), 150, (255, 0, 0), 2)     #Line 4

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

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

Line 1-2: Import necessary packages

Line 3: Create a blank image using np.zeros().   #see previous tutorial to understand how it works.

Line 4: Use function cv2.circle(image, (x, y), r, color, thickness ) to create a circle on the blank                        image
  • image is the name of the blank image on which we will draw our circle
  • (x,y) is the center of the circle. 
  • r is the radius of the circle
  • color defined as (B, G, R). B, G, R represent the intensity of blue, green and red  color in your circle. Max intensity is 255 for each color. You can experiment with other value too for different color circles.
  • thickness represents the thickness of the circumference line of the circle.
Line 5: Display the picture using imshow() function. 

Line 6: Hold screen infinitely until user closes it.

Line 7: Close any open windows. 

OUTPUT:



So that's it for this tutorial. Hope you are learning to use OpenCV with Python and Numpy. See you in my next tutorial where we will learn to draw more complex drawings which will involve use of both circle and line.

Any queries , please ask in comments section below. :)

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.