Wednesday 24 September 2014

Basic Drawing Examples

Basic Drawing Examples


Drawing a line:


cv2.line(img, Point pt1, Point pt2, color[,thickness[,lineType[,shift]]]) --> img
Parameters:
  • img – Image.
  • pt1 – First point of the line segment.
  • pt2 – Second point of the line segment.
  • color – Line color.
  • thickness – Line thickness.
  • lineType –
    Type of the line:
    • LINE_8 (or omitted) - 8-connected line.
    • LINE_4 - 4-connected line.
    • LINE_AA - antialiased line.
  • shift – Number of fractional bits in the point coordinates.
Example 1: Drawing a line:
--------------

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a blue line with thickness of 5 px
cv2.line(img,(15,20),(70,50),(255,0,0),5)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
--------------

Drawing a Circle:


cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → img
Parameters:
  • img – Image where the circle is drawn.
  • center – Center of the circle.
  • radius – Radius of the circle.
  • color – Circle color.
  • thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
  • lineType – Type of the circle boundary. 
  • shift – Number of fractional bits in the coordinates of the center and in the radius value.
Example 2: Drawing a Circle:
---------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a red closed circle
cv2.circle(img,(200,200), 40, (0,0,255), -1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
---------------

Drawing an Ellipse:


cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → img
cv2.ellipse(img, box, color[, thickness[, lineType]]) → img

Parameters:
  • img – Image.
  • center – Center of the ellipse.
  • axes – Half of the size of the ellipse main axes.
  • angle – Ellipse rotation angle in degrees.
  • startAngle – Starting angle of the elliptic arc in degrees.
  • endAngle – Ending angle of the elliptic arc in degrees.
  • box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
  • color – Ellipse color.
  • thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
  • lineType – Type of the ellipse boundary.
  • shift – Number of fractional bits in the coordinates of the center and values of axes.
Example 3: Drawing an Ellipse:
----------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw ellipse
cv2.ellipse(img,(200,200),(80,50),0,0,360,(0,255,0),-1)
cv2.ellipse(img,(200,200),(80,50),45,0,360,(0,0,255),1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
----------------

Drawing a Rectangle:


cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img

Parameters:
  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • rec – Alternative specification of the drawn rectangle.
  • color – Rectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
  • lineType – Type of the line..
  • shift – Number of fractional bits in the point coordinates.
Example 4: Drawing a Rectangle:
--------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw rectangle(not filled)
cv2.rectangle(img,(15,20),(70,50),(0,255,0),3)

# Draw filled rectangle
cv2.rectangle(img,(115,120),(170,150),(255,0,0),-1)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
--------------------

Drawing a Polygon:


cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img
Parameters:
  • img – Image.
  • pts – Array of polygonal curves.
  • npts – Array of polygon vertex counters.
  • ncontours – Number of curves.
  • isClosed – Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
  • color – Polyline color.
  • thickness – Thickness of the polyline edges.
  • lineType – Type of the line segments.
  • shift – Number of fractional bits in the vertex coordinates.
Example 4: Drawing a Polygon:
-----------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
-----------------------

Putting Text in image:


cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None

Parameters:
  • img – Image.
  • text – Text string to be drawn.
  • org – Bottom-left corner of the text string in the image.
  • font – CvFont structure initialized using InitFont().
  • fontFace – Font type. One of FONT_HERSHEY_SIMPLEXFONT_HERSHEY_PLAINFONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEXFONT_HERSHEY_TRIPLEXFONT_HERSHEY_COMPLEX_SMALL,FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC to get the slanted letters.
  • fontScale – Font scale factor that is multiplied by the font-specific base size.
  • color – Text color.
  • thickness – Thickness of the lines used to draw a text.
  • lineType – Line type. See the line for details.
  • bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
Example 5: Putting Text in image:
--------------------
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Write some Text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Hello World!',(10,500), font, 1,(255,255,255),2)

#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
---------------------

8 comments: