您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Python中利用opencv實現一個畫板功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
具體如下:
import cv2 import numpy as np drawing = False # true if mouse is pressed ix,iy = -1,-1 def nothing(x): pass # mouse callback function def draw_circle(event,x,y,flags,param): global ix,iy,drawing g = param[0] b = param[1] r = param[2] shape = param[3] if event == cv2.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv2.EVENT_MOUSEMOVE: if drawing == True: if shape == 0: cv2.rectangle(img,(ix,iy),(x,y),(g,b,r),-1) else: cv2.circle(img,(x,y),5,(g,b,r),-1) elif event == cv2.EVENT_LBUTTONUP: drawing = False if shape == 0: cv2.rectangle(img,(ix,iy),(x,y),(g,b,r),-1) else: cv2.circle(img,(x,y),5,(g,b,r),-1) # Create a black image, a window img = np.zeros((300,512,3), np.uint8) cv2.namedWindow('image') # create trackbars for color change cv2.createTrackbar('R','image',0,255,nothing) cv2.createTrackbar('G','image',0,255,nothing) cv2.createTrackbar('B','image',0,255,nothing) # create switch for ON/OFF functionality switch2 = '0 : OFF \n1 : ON' switch3 = '0: Rectangle \n1: Line ' cv2.createTrackbar(switch2, 'image',0,1,nothing) cv2.createTrackbar(switch3, 'image',0,1,nothing) while(1): cv2.imshow('image',img) k = cv2.waitKey(1) & 0xFF # get current positions of four trackbars if k == 27: break r = cv2.getTrackbarPos('R','image') g = cv2.getTrackbarPos('G','image') b = cv2.getTrackbarPos('B','image') shape = cv2.getTrackbarPos(switch3,'image') s = cv2.getTrackbarPos(switch2,'image') if s == 0: img[:] = 0 else: if k == 27: break cv2.setMouseCallback('image',draw_circle,(b,g,r,shape)) cv2.destroyAllWindows()
關于怎么在Python中利用opencv實現一個畫板功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。