Wednesday, August 5, 2020

Jul 25, 2020 日誌

學習如何使用opencv。Chapter 4 插入圖型及文字。

Chapter 4 插入圖型及文字

np.zeros :

產生一個用零填滿的矩陣。我們用來產生一個黑色的畫布(圖片),之後會在上面插入圖型及文字。

  • line : 指定兩點畫一直線。

  • rectangle : 指定兩點畫出一矩形框。

  • circle : 指定圓心位置,再給定半徑大小。

  • putText : 輸入要顯示的文字,指定文字的基準位置(左下角)的座標。

import cv2
import numpy as np

img = np.zeros((512,512,3),np.uint8) #zero: black . (weith,height,channel)

#------Part 1--area

print(img)
#img[:] = 255,0,0 #a blue square full the black square

img[0:100,200:300] = 255,0,0 #blue square[y1:y2,x1:x2] = B,G,R

#------Part 2--line

#cv2.line(img,(10,10),(300,400),(0,0,255),2) #draw a line : (image,start_point(x0,y0),end_point(x1,y1),color(B,G,R),thickness,line_type)

cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(0,255,0),3)

#------Part 3--rectangle

cv2.rectangle(img,(5,5),(150,240),(180,60,255),2) #a rectangle box:(image,start_point(x0,y0),end_point(x1,y1),color(B,G,R),thickness,line_type)
cv2.rectangle(img,(125,125),(250,460),(255,60,180),cv2.FILLED) #a rectangle shape

#------Part4--circle

cv2.circle(img,(256,256),100,(60,180,255),2) #draw a circle: (image,center_point(x,y),radius,color(B,G,R),thickness,line_type)

#------Part5--text

cv2.putText(img,"Opencv",(200,256),cv2.FONT_ITALIC ,1,(180,180,180),2) #show text: (image,"text",org,font_type ,font_scale,color(B,G,R),thickness,line_type) org:Bottom-left corner of the text

cv2.imshow("Image",img)

cv2.waitKey(0)

No comments:

Post a Comment

協同設計

2022 一開始, 將計算機程式與電腦輔助設計實習課程的期末報告導向 Github 倉儲的協同, 倉儲分別在 https://github.com/mdecourse/cp2021_final 與 https://github.com/mdecourse/cad2021_fin...