Complete the code to draw a blue line on the image.
import cv2 import numpy as np img = np.zeros((200, 200, 3), dtype=np.uint8) cv2.line(img, (10, 10), (190, 10), [1], 3) cv2.imwrite('line.png', img)
OpenCV uses BGR color format. Blue is (255, 0, 0), which is option B. Option B (0, 0, 255) is red.
Complete the code to draw a filled green rectangle on the image.
import cv2 import numpy as np img = np.zeros((200, 200, 3), dtype=np.uint8) cv2.rectangle(img, (50, 50), (150, 150), [1], -1) cv2.imwrite('rectangle.png', img)
The color green in OpenCV BGR format is (0, 255, 0). The thickness -1 fills the rectangle.
Fix the error in the code to draw a red circle on the image.
import cv2 import numpy as np img = np.zeros((200, 200, 3), dtype=np.uint8) cv2.circle(img, (100, 100), 50, [1], 5) cv2.imwrite('circle.png', img)
The color red in OpenCV BGR format is (0, 0, 255). The code uses this color to draw the circle.
Fill both blanks to draw white text on the image at position (50, 180).
import cv2 import numpy as np img = np.zeros((200, 200, 3), dtype=np.uint8) cv2.putText(img, 'Hello', (50, 180), [1], 1, [2], 2) cv2.imwrite('text.png', img)
The font cv2.FONT_HERSHEY_SIMPLEX is a common readable font. The color white in BGR is (255, 255, 255).
Fill all three blanks to draw a green circle with thickness 3 and anti-aliased line.
import cv2 import numpy as np img = np.zeros((300, 300, 3), dtype=np.uint8) cv2.circle(img, (150, 150), 75, [1], [2], [3]) cv2.imwrite('circle_aa.png', img)
The green color is (0, 255, 0). Thickness is 3 pixels. cv2.LINE_AA enables smooth anti-aliased edges.