python opencv3 寫字畫圈畫矩形


python opencv練習

  • 自定義一張[512, 512, 3]的圖像
  • 在上面寫寫字,畫畫圈和矩形
  • 顯示

代碼為:

 1 import cv2  2 import numpy as np  3 
 4 img = np.zeros([512, 512, 3], dtype=np.uint8)  5 for i in range(512):  6     for j in range(512):  7         img[i, j, :] = [i % 256, j % 256, (i + j) % 256]  8 
 9 cv2.rectangle(img, (200, 200), (400, 400), (0, 0, 255), 3) 10 cv2.circle(img, (300, 300), 100, (0, 255, 255), 3) 11 
12 info = 'Hello World'
13 font_face = cv2.FONT_HERSHEY_COMPLEX 14 font_scale = 2
15 thickness = 2
16 text_size = cv2.getTextSize(info, font_face, font_scale, thickness) 17 print(text_size) 18 p_center = (int(512 / 2 - text_size[0][0] / 2), int(512 / 2 - text_size[0][1] / 2)) 19 cv2.putText(img, info, p_center, font_face, font_scale, (0, 0, 0), thickness) 20 
21 cv2.imshow('demo', img) 22 cv2.waitKey(0) 23 cv2.destroyAllWindows()

 顯示如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM