opencv for python 之 創建圖片繪製簡單幾何圖形

#create a image and test draw it

創建一張圖片,需要定義圖片各個屬性,包括大小,圖片像素類型(每個像素點用多少bits表示),通道數3(rgb)
import cv2.cv as cv

width = 200
height = 200
no_of_bits = 8
channels = 3

image = cv.CreateImage((width,height), no_of_bits, channels)

創建顯示窗口

win_name = "test"
cv.NamedWindow("test",cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage(win_name, image)

畫一條直線,只需定義兩個頂點座標和線的寬度以及顏色

線的屬性有顏色 , 寬度, 線類型(當線的寬度爲-1時,矩形與圓形將是實心,及顏色填充)

#draw a line
x = 10
y = 10
start_point = (x,y)
x1 = 100
y1 = 100
end_point = (x1,y1)
#line attribute
color = (0,255,0)
line_width = 4
line_type = 8
cv.Line(image, start_point, end_point, color, line_width, line_type)

畫一個矩形,只需定義矩形的左上頂點和右下頂點位置,以及線的顏色和寬度

x = 30
y = 30
rect_start = (x,y)
x1 = 90
y1 = 90
rect_end = (x1,y1)
cv.Rectangle(image, rect_start, rect_end, color, 1, 0)

畫一個圓,只需定義圓的中心點座標和圓的半徑,以及線的顏色和寬度

x = 100
y = 100
radius = 60
circle_center = (x,y)
cv.Circle(image, circle_center, radius, color)

cv.ShowImage(win_name, image)

cv.WaitKey()

示例運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章