Monday 22 August 2016

TUT11: Let's draw the OpenCV logo

Hello guys,
In this tutorial, we are going to see how to make a logo of OpenCV. Before starting I would like you to try to make logo yourself. You can make logo of some other things like brands or anthing you like.

Here's the code


import cv2
import numpy as np

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

cv2.ellipse(img, (250,100), (70,70), 135, 0, 270, (0,0,255),50)
cv2.ellipse(img, (150,280), (70,70), 10, 0, 270, (0,255,0),50)
cv2.ellipse(img, (350,280), (70,70), 315, 0, 270, (255,0,0),50)

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(75,450), font, 3,(255,255,255),2,cv2.LINE_AA)

cv2.imshow('img',img)

cv2.waitKey(0)
cv2.destroyAllWindows()


Line1-2 : Import necessary packages cv2 and numpy

Line 3 : Create a canvas using np.zeros()

Line 4-6 : Create an ellipse using ellipse function provided by OpenCV. The parameters of ellipse are                  as follows:
                   1) Image or canvas
                   2) Center
                   3) Half of the size of the ellipse main axes a and b given as (a, b).
                   4) Ellipse rotation angle in degrees.
                   5) Starting angle of the ellipse arc in degrees
                   6) Ending angle of the ellipse arc in degrees.
                   7) color
                   8) thickness

                For more info on ellipse(), click here.

Line 7 : Specify the font as hershey_simplex. You can try other fonts too here

Line 8 : If you read the previous tutorial, you may be familiar with putText() finction. It puts                            whatever text you want to write on canvas. Click here if you do not know how putText()
             works.

Line 9 : Displays the image 

Line 10 : Holds screen indefinitely until user closes the window

Line 11 : Closes any open windows.


OUTPUT:



For any queries, Please ask in comment section below.

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.