Home > Backend Development > Python Tutorial > Detailed explanation of simple drawing board function based on opencv

Detailed explanation of simple drawing board function based on opencv

coldplay.xixi
Release: 2020-08-07 15:51:45
forward
3462 people have browsed it

Detailed explanation of simple drawing board function based on opencv

OpenCV provides many simple statements to implement complex functions. Based on the basic statements of color and mouse interaction, we can build a simple drawing board. Although it is simple, there are no fewer steps to make the frame.

If you are interested, you can write it according to my steps, or directly copy the code in the main program and run it.

Related learning recommendations: python video tutorial

1. Function

Clear the drawing board function, It is convenient for subsequent programming.
Target functions: Adjustable colors, drawing area, and mouse input.

2. Framework construction

There are two main steps:

The first step:Establishment The color selection can be set using the OpenCV slider statement cv.createTrackbar. cv.createTrackbar has a total of five parameter settings, which are (Trackbar name, target window name, starting value - also the default value, maximum value, callback function).

#创建画布
img=np.zeros((300,512,3),np.uint8)
#给画布命名——需要提供给Trackbar
cv.namedWindow("image")
#创建RGB三种颜色的Trackbar,返回函数不需要做任何动作
cv.createTrackbar("R","image",0,255,nothing)
cv.createTrackbar("G","image",0,255,nothing)
cv.createTrackbar("B","image",0,255,nothing)
#由于OpenCv里面无按钮函数,所以用0/1来表示开关
switch="0:OFF\n1:ON"
cv.createTrackbar(switch,"image",0,1,nothing)
#回调函数,不需要做任何操作
def nothing(x):
 pass
Copy after login

Step 2:Capture the mouse action status, and draw graphics in the drawing area based on the mouse action. Common mouse operation statements can be viewed in python using the statement *events = [i for i in dir(cv) if ‘EVENT’ in i]*.

#设置一个是否开始画画的开关
drawing=False
#定义画画函数
def draw(event,x,y,flag,param):
 #将画画开关作为全局变量,以便于在每次动作以后改变它的值
 global drawing
 #使用函数cv.EVENT_LBUTTONDOWN,如果鼠标左键按下,画圆,同时drawing为真
 if event==cv.EVENT_LBUTTONDOWN:
  cv.circle(img,(x,y),3,(g,b,r),-1)
  drawing=True
 #如果鼠标移动,进行下面操作
 elif event==cv.EVENT_MOUSEMOVE:
  #如果drawing为真,开始画圆
  if drawing==True:
   cv.circle(img, (x, y), 3, (g, b, r), -1)
 #如果鼠标左键抬起,画最后一个圆,并将drawing设为False,此时就会停止画画
 elif event==cv.EVENT_LBUTTONUP:
  cv.circle(img,(x,y),3,(g,b,r),-1)
  drawing=False
Copy after login

3. Main function

The main function is to integrate the above two parts.

while(1):
 #展示画布
 cv.imshow("image",img)
 #设置终止按钮,为了保证在32/64位系统或者在不是ASCII编码的键盘上有正常的操作,使用&0xFF取低八位
 k=cv.waitKey(1)&0xFF
 #如果k为27,即键盘上Esc的ASCII编码序号,退出画板
 if k==27:
  break
 #获取每个Trackbar中的值
 r=cv.getTrackbarPos("R","image")
 g = cv.getTrackbarPos("G", "image")
 b = cv.getTrackbarPos("B", "image")
 s = cv.getTrackbarPos(switch, "image")
 #如果开关是0,清空画板并禁止画画,如果是1,则允许
 if s==0:
  img[:]=0
 else:
  cv.setMouseCallback("image", draw)
#最后不要忘记销毁窗口
cv.destroyAllWindows()
Copy after login

4. Running effect

##5. Summary

From this simple In the drawing board, we mainly learned and used the mouse function cv.setMouseCallback and cv.createTrackbar, two basic OpenCV functions, and explored the details and found out what needs attention.

6. General Procedure

import cv2 as cv
import numpy as np

def nothing(x):
 pass
img=np.zeros((300,512,3),np.uint8)
cv.namedWindow("image")
cv.createTrackbar("R","image",0,255,nothing)
cv.createTrackbar("G","image",0,255,nothing)
cv.createTrackbar("B","image",0,255,nothing)
switch="0:OFF\n1:ON"
cv.createTrackbar(switch,"image",0,1,nothing)

drawing=False
def draw(event,x,y,flag,param):
 global drawing
 if event==cv.EVENT_LBUTTONDOWN:
  cv.circle(img,(x,y),3,(g,b,r),-1)
  drawing=True
 elif event==cv.EVENT_MOUSEMOVE:
  if drawing==True:
   cv.circle(img, (x, y), 3, (g, b, r), -1)
 elif event==cv.EVENT_LBUTTONUP:
  cv.circle(img,(x,y),3,(g,b,r),-1)
  drawing=False

while(1):
 cv.imshow("image",img)
 k=cv.waitKey(1)&0xFF
 if k==27:
  break
 r=cv.getTrackbarPos("R","image")
 g = cv.getTrackbarPos("G", "image")
 b = cv.getTrackbarPos("B", "image")
 s = cv.getTrackbarPos(switch, "image")
 if s==0:
  img[:]=0
 else:
  cv.setMouseCallback("image", draw)
cv.destroyAllWindows()
Copy after login

The above is the detailed content of Detailed explanation of simple drawing board function based on opencv. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template