python opencv畫圖

import numpy as np
import cv2

def drawPic():
	img=np.zeros((512,512,3), np.uint8)
	
	# Draw a diagonal blue line with thickness of 5 px
	cv2.line(img,(0,0),(511,511),(255,0,0),5)
	cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
	cv2.circle(img,(447,63), 63, (0,0,255), -1)
	cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
	
	# 畫多邊形
	pts=np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
	# 這裏 reshape 的第一個參數爲 -1, 表明這一維的長度是根據後面的維度的計算出來的。
	# 如果第三個參數是 False,我們得到的多邊形是不閉合的(首尾不相連)。
	pts=pts.reshape((-1,1,2))
	#cv2.polylines(pts, (0,0,255), 3) 
	
	# 在圖片上添加文字
	font=cv2.FONT_HERSHEY_SIMPLEX
	cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2)
	
	# display image
	winname = 'example'
	cv2.namedWindow(winname)
	cv2.imshow(winname, img)
	cv2.waitKey(0)
	cv2.destroyWindow(winname)

def printEvent():
	events=[i for i in dir(cv2) if 'EVENT'in i]
	print(events)
	
	cv2_context = dir(cv2) # 返回的是一個列表
	print(cv2_context) 
	
	events = []
	for cont in cv2_context:
		if "EVENT" in cont:
			events.append(cont)
			
	print()
	print(events)		
	
def draw_circle(event,x,y,flags,param):
	#global img
	if event==cv2.EVENT_LBUTTONDBLCLK:
		cv2.circle(param,(x,y),100,(255,0,0),-1)
		cv2.imshow("image", param)
	
def drawPicMouseEvent():
	# 創建圖像與窗口並將窗口與回調函數綁定
	img=np.zeros((1080, 1920,3),np.uint8)
	cv2.namedWindow('image')
	cv2.setMouseCallback('image',draw_circle, img)
	
	# 一直閃爍
	#while 1: 
	#	cv2.imshow("image", img)
	#	if cv2.waitKey(20)&0xFF==27:
	#		break
	cv2.imshow("image", img)
	cv2.waitKey(0)
		
	cv2.destroyAllWindows()
	
# 當鼠標按下時變爲 True
drawing=False

# 如果 mode 爲 true 繪製矩形。按下 'm' 變成繪製曲線。
mode=True
ix,iy=-1,-1
colorImage=np.zeros((40, 100,3),np.uint8)

def drawR(x):
	global colorImage
	g=cv2.getTrackbarPos('G','image')
	b=cv2.getTrackbarPos('B','image')
	color = (b, g, x)
	colorImage[:] = color
	cv2.imshow("palette", colorImage)
	
def drawG(x):
	global colorImage
	r=cv2.getTrackbarPos('R','image')
	b=cv2.getTrackbarPos('B','image')
	color = (b, x, r)
	colorImage[:] = color
	cv2.imshow("palette", colorImage)
	
def drawB(x):
	global colorImage
	g=cv2.getTrackbarPos('G','image')
	r=cv2.getTrackbarPos('R','image')
	color = (x, g, r)
	colorImage[:] = color
	cv2.imshow("palette", colorImage)

# 創建回調函數
def draw_pic(event,x,y,flags,param):
	global ix,iy,drawing,mode
	
	if event==cv2.EVENT_LBUTTONDOWN:
		drawing=True
		ix, iy = x, y
	elif event == cv2.EVENT_MOUSEMOVE and  flags==cv2.EVENT_FLAG_LBUTTON:
		if drawing==True:
			r=cv2.getTrackbarPos('R','image')
			g=cv2.getTrackbarPos('G','image')
			b=cv2.getTrackbarPos('B','image')
			color = (b, g, r)
			
			if mode==True:
				cv2.rectangle(param,(ix,iy),(x,y), color,-1)
			else:
				cv2.circle(param,(x,y),3,color,-1)
	elif event==cv2.EVENT_LBUTTONUP:
		drawing==False

def drawPicMouseEvent1():
	img=np.zeros((1080, 1920,3),np.uint8)
	cv2.namedWindow('image')
	cv2.setMouseCallback('image',draw_pic, img)
	
	cv2.createTrackbar('R','image',0,255,drawR)
	cv2.createTrackbar('G','image',0,255,drawG)
	cv2.createTrackbar('B','image',0,255,drawB)
	
	cv2.imshow("palette", colorImage)
	global mode
	while True:
		cv2.imshow('image',img)
		k=cv2.waitKey(1)&0xFF
		if k==ord('m') or k == ord('M'):
			mode = not mode
		elif k == 27:
			break;
	
def main():
	#videoFile = 0
	#videoFile = "H:\\data\\video.avi"
	videoFile = "H:\\data\\opencv33.mp4"
	#playVideo(videoFile)
	#saveVideo(videoFile)
	
	#drawPic()
	#printEvent()
	
	drawPicMouseEvent()
	#drawPicMouseEvent1()

if __name__ == '__main__':
	main()

 

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