图片坐标和数组下标是相反的,坐标原点位于左上角
1 import numpy as np 2 import cv2 3 4 height, width = 150, 200 5 img = np.zeros((height, width, 3), np.uint8) 6 img[:, :] = [255, 255, 255] 7 8 # Pixel position to draw at 9 row, col = 20, 100 10 11 # Draw a square with position 20, 100 as the top left corner 12 for i in range(row, 30): 13 for j in range(col, 110): 14 img[i, j] = [0, 0, 255] 15 16 # Will the following draw a circle at (20, 100)? 17 # Ans: No. It will draw at row index 100 and column index 20. 18 cv2.circle(img,(row, col), 5, (0,255,0), -1) 19 20 cv2.imwrite("square_circle_opencv.jpg", img)