Sunday, 15 July 2018

Basic Drawing Examples in Image using OpenCV & Python


Goal

Meccanismo-Complesso-OpenCV-draw-circle-and-ellipses
  • Learn to draw different geometric shapes with OpenCV
  • You will learn these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse() etc.

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)
Running the program you will see the blue line.
line
————–

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), 3)

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

cv2.waitKey(0)
Running the program you will see red color circle
 circle
—————

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)
Running the program you will see ellipse
 ellipse
—————-

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)
Running the program you will see rectangle
 rectangle
——————–

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)
Running the program you will see polygon
polygon


No comments:

Post a Comment