C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
OpenCV Drawing FunctionsWe can draw the various shapes on an image such as circle, rectangle, ellipse, polylines, convex, etc. It is used when we want to highlight any object in the input image. The OpenCV provides functions for each shape. Here we will learn about the drawing functions. Drawing CircleWe can draw the circle on the image by using the cv2.circle() function. The syntax is the following: cv2.circle(img, center, radius, color[,thickness [, lineType[,shift]]]) Parameters:
Consider the following example: import numpy as np import cv2 img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) cv2.circle(img,(80,80), 55, (0,255,0), -1) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() Output: Drawing RectangleThe OpenCV provides a function to draw a simple, thick or filled up-right rectangle. The syntax is following: cv2.rectangle(img, pt1, pt2, color[, thickness[,lineType[,shift]]]) Parameters:
Consider the following example: import numpy as np import cv2 img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) cv2.rectangle(img,(15,25),(200,150),(0,255,255),15) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() Output: Drawing EllipseWe can draw an ellipse on an image by using the cv2.ellipse() function. It can draw a simple or thick elliptic arc or can fill an ellipse sector. cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) cv2.ellipse(img, box, color[, thickness[, lineType]]) Parameters:
Consider the following example: import numpy as np import cv2 img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) cv2.ellipse(img, (250, 150), (80, 20), 5, 0, 360, (0,0,255), -1) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() Output: There are two functions to draw the ellipse. The first function is used to draw the whole ellipse, not an arc bypassing startAngle=0 and endAngle = 360. The second function of an ellipse is used to draw an ellipse outline, a filled ellipse, an elliptic arc, or a filled ellipse sector. Drawing linesOpenCV provides the line() function to draw the line on the image. It draws a line segment between ptr1 and ptr2 points in the image. The image boundary clips the line. cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) Parameters:
Consider the following example: import numpy as np import cv2 img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) cv2.line(img,(10,0),(150,150),(0,0,0),15) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() Output: Write Text on ImageWe can write text on the image by using the putText() function. The syntax is given below. cv2.putText(img, text, org, font, color) Parameters:
Consider the following example. import numpy as np import cv2 font = cv2.FONT_HERSHEY_SIMPLEX # Create a black image. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) cv2.putText(img,'Hack Projects',(10,500), font, 1,(255,255,255),2) #Display the image cv2.imshow("image",img) cv2.waitKey(0) Output: Drawing PolylinesWe can draw the polylines on the image. OpenCV provides the polylines() function, that is used to draw polygonal curves on the image. The syntax is given below: cv2.polyLine(img, polys, is_closed, color, thickness=1, lineType=8, shift=0) Parameters:
Consider the following program to draw polylines in image: import numpy as np import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\forest.jpg',cv2.IMREAD_COLOR) #defining points for polylines pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32) # pts = pts.reshape((-1,1,2)) cv2.polylines(img, [pts], True, (0,255,255), 3) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() Output:
Next TopicOpenCV Blob Detection
|